2013-02-05 14:16:51 -05:00
|
|
|
class UserEmailObserver < ActiveRecord::Observer
|
|
|
|
observe :notification
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
class EmailUser
|
|
|
|
attr_reader :notification
|
2013-07-21 12:50:01 -04:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def initialize(notification)
|
|
|
|
@notification = notification
|
|
|
|
end
|
2013-08-07 13:02:49 -04:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def mentioned
|
|
|
|
enqueue :user_mentioned
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def posted
|
|
|
|
enqueue :user_posted
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def quoted
|
|
|
|
enqueue :user_quoted
|
|
|
|
end
|
|
|
|
|
|
|
|
def replied
|
|
|
|
enqueue :user_replied
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_message
|
|
|
|
enqueue_private :user_private_message
|
|
|
|
end
|
2013-02-27 15:38:44 -05:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def invited_to_private_message
|
|
|
|
enqueue :user_invited_to_private_message
|
|
|
|
end
|
|
|
|
|
2015-03-30 14:36:47 -04:00
|
|
|
def invited_to_topic
|
|
|
|
enqueue :user_invited_to_topic
|
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def enqueue(type)
|
2015-08-31 01:54:37 -04:00
|
|
|
return unless (notification.user.email_direct? && notification.user.active?)
|
2013-08-26 00:55:35 -04:00
|
|
|
|
|
|
|
Jobs.enqueue_in(delay,
|
2013-08-24 07:21:39 -04:00
|
|
|
:user_email,
|
|
|
|
type: type,
|
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_private(type)
|
2015-08-31 01:54:37 -04:00
|
|
|
return unless (notification.user.email_private_messages? && notification.user.active?)
|
2015-06-09 03:35:26 -04:00
|
|
|
|
2013-08-26 00:55:35 -04:00
|
|
|
Jobs.enqueue_in(delay,
|
2013-08-24 07:21:39 -04:00
|
|
|
:user_email,
|
|
|
|
type: type,
|
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id)
|
|
|
|
end
|
2013-08-26 00:55:35 -04:00
|
|
|
|
|
|
|
def delay
|
2013-08-26 21:52:09 -04:00
|
|
|
SiteSetting.email_time_window_mins.minutes
|
2013-08-26 00:55:35 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def after_commit(notification)
|
2014-02-17 11:44:28 -05:00
|
|
|
transaction_includes_action = notification.send(:transaction_include_any_action?, [:create])
|
2013-08-24 07:21:39 -04:00
|
|
|
|
|
|
|
delegate_to_email_user notification if transaction_includes_action
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
private
|
|
|
|
|
2013-08-26 00:55:35 -04:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def extract_notification_type(notification)
|
|
|
|
Notification.types[notification.notification_type]
|
2013-08-07 13:02:49 -04:00
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def delegate_to_email_user(notification)
|
|
|
|
email_user = EmailUser.new(notification)
|
|
|
|
email_method = extract_notification_type notification
|
|
|
|
|
|
|
|
email_user.send(email_method) if email_user.respond_to? email_method
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|