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