2013-02-05 13:16:51 -06:00
|
|
|
class UserEmailObserver < ActiveRecord::Observer
|
|
|
|
observe :notification
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
class EmailUser
|
|
|
|
attr_reader :notification
|
2013-07-21 11:50:01 -05:00
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def initialize(notification)
|
|
|
|
@notification = notification
|
|
|
|
end
|
2013-08-07 12:02:49 -05:00
|
|
|
|
2015-11-30 00:03:47 -06:00
|
|
|
def group_mentioned
|
|
|
|
enqueue :group_mentioned
|
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def mentioned
|
|
|
|
enqueue :user_mentioned
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def posted
|
|
|
|
enqueue :user_posted
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def quoted
|
|
|
|
enqueue :user_quoted
|
|
|
|
end
|
|
|
|
|
|
|
|
def replied
|
|
|
|
enqueue :user_replied
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_message
|
2015-11-27 12:09:35 -06:00
|
|
|
enqueue_private(:user_private_message, 0)
|
2013-08-24 06:21:39 -05:00
|
|
|
end
|
2013-02-27 14:38:44 -06:00
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def invited_to_private_message
|
2015-11-27 12:09:35 -06:00
|
|
|
enqueue(:user_invited_to_private_message, 0)
|
2013-08-24 06:21:39 -05:00
|
|
|
end
|
|
|
|
|
2015-03-30 13:36:47 -05:00
|
|
|
def invited_to_topic
|
2015-11-27 12:09:35 -06:00
|
|
|
enqueue(:user_invited_to_topic, 0)
|
2015-03-30 13:36:47 -05:00
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
private
|
|
|
|
|
2016-01-18 18:39:57 -06:00
|
|
|
EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]]
|
|
|
|
|
2015-11-27 12:09:35 -06:00
|
|
|
def enqueue(type, delay=default_delay)
|
2016-01-18 18:39:57 -06:00
|
|
|
return unless notification.user.email_direct?
|
|
|
|
return unless notification.user.active? || notification.user.staged?
|
|
|
|
return unless EMAILABLE_POST_TYPES.include? notification.post.try(:post_type)
|
2013-08-25 23:55:35 -05:00
|
|
|
|
|
|
|
Jobs.enqueue_in(delay,
|
2016-01-18 18:39:57 -06:00
|
|
|
:user_email,
|
|
|
|
type: type,
|
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id)
|
2013-08-24 06:21:39 -05:00
|
|
|
end
|
|
|
|
|
2015-11-27 12:09:35 -06:00
|
|
|
def enqueue_private(type, delay=default_delay)
|
2016-01-18 18:39:57 -06:00
|
|
|
return unless notification.user.email_private_messages?
|
|
|
|
return unless notification.user.active? || notification.user.staged?
|
|
|
|
return unless EMAILABLE_POST_TYPES.include? notification.post.try(:post_type)
|
2015-06-09 02:35:26 -05:00
|
|
|
|
2013-08-25 23:55:35 -05:00
|
|
|
Jobs.enqueue_in(delay,
|
2013-08-24 06:21:39 -05:00
|
|
|
:user_email,
|
|
|
|
type: type,
|
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id)
|
|
|
|
end
|
2013-08-25 23:55:35 -05:00
|
|
|
|
2015-11-27 12:09:35 -06:00
|
|
|
def default_delay
|
2013-08-26 20:52:09 -05:00
|
|
|
SiteSetting.email_time_window_mins.minutes
|
2013-08-25 23:55:35 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def after_commit(notification)
|
2014-02-17 10:44:28 -06:00
|
|
|
transaction_includes_action = notification.send(:transaction_include_any_action?, [:create])
|
2015-12-18 09:32:53 -06:00
|
|
|
delegate_to_email_user(notification) if transaction_includes_action
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
private
|
|
|
|
|
2013-08-25 23:55:35 -05:00
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def extract_notification_type(notification)
|
|
|
|
Notification.types[notification.notification_type]
|
2013-08-07 12:02:49 -05:00
|
|
|
end
|
|
|
|
|
2013-08-24 06:21:39 -05:00
|
|
|
def delegate_to_email_user(notification)
|
|
|
|
email_user = EmailUser.new(notification)
|
2015-12-18 09:32:53 -06:00
|
|
|
email_method = extract_notification_type(notification)
|
2013-08-24 06:21:39 -05:00
|
|
|
|
|
|
|
email_user.send(email_method) if email_user.respond_to? email_method
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|