2016-12-22 12:29:34 +08:00
|
|
|
class NotificationEmailer
|
2013-02-06 03:16:51 +08:00
|
|
|
|
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
|
|
|
|
|
2016-02-17 01:29:23 +08:00
|
|
|
def linked
|
|
|
|
enqueue :user_linked
|
|
|
|
end
|
|
|
|
|
2016-07-08 00:23:19 +08:00
|
|
|
def watching_first_post
|
|
|
|
enqueue :user_watching_first_post
|
|
|
|
end
|
|
|
|
|
2013-08-24 19:21:39 +08:00
|
|
|
def private_message
|
2016-02-01 17:14:30 +08:00
|
|
|
enqueue_private(:user_private_message)
|
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
|
2016-02-01 17:14:30 +08:00
|
|
|
enqueue(:user_invited_to_private_message, private_delay)
|
2013-08-24 19:21:39 +08:00
|
|
|
end
|
|
|
|
|
2015-03-31 02:36:47 +08:00
|
|
|
def invited_to_topic
|
2016-02-01 17:14:30 +08:00
|
|
|
enqueue(:user_invited_to_topic, private_delay)
|
2015-03-31 02:36:47 +08:00
|
|
|
end
|
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
def self.notification_params(notification, type)
|
|
|
|
post_id = (notification.data_hash[:original_post_id] || notification.post_id).to_i
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
type: type,
|
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
notification_type: Notification.types[notification.notification_type],
|
|
|
|
}
|
|
|
|
|
|
|
|
hash[:post_id] = post_id if post_id > 0
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2013-08-24 19:21:39 +08:00
|
|
|
private
|
|
|
|
|
2016-01-19 08:39:57 +08:00
|
|
|
EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]]
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def enqueue(type, delay = default_delay)
|
2019-03-15 22:55:11 +08:00
|
|
|
return if notification.user.user_option.email_level == UserOption.email_level_types[:never]
|
2016-02-02 02:12:10 +08:00
|
|
|
perform_enqueue(type, delay)
|
2013-08-24 19:21:39 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def enqueue_private(type, delay = private_delay)
|
2018-01-12 12:43:40 +08:00
|
|
|
|
|
|
|
if notification.user.user_option.nil?
|
2018-01-15 14:02:31 +08:00
|
|
|
# this can happen if we roll back user creation really early
|
|
|
|
# or delete user
|
|
|
|
# bypass this pm
|
2018-01-12 12:43:40 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-03-15 22:55:11 +08:00
|
|
|
return if notification.user.user_option.email_messages_level == UserOption.email_level_types[:never]
|
2016-02-02 02:12:10 +08:00
|
|
|
perform_enqueue(type, delay)
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_enqueue(type, delay)
|
2016-06-17 00:55:47 +08:00
|
|
|
user = notification.user
|
|
|
|
return unless user.active? || user.staged?
|
2017-08-29 05:32:07 +08:00
|
|
|
return if SiteSetting.must_approve_users? && !user.approved? && !user.staged?
|
2016-06-17 00:55:47 +08:00
|
|
|
|
2016-02-02 02:12:10 +08:00
|
|
|
return unless EMAILABLE_POST_TYPES.include?(post_type)
|
2015-06-09 15:35:26 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
Jobs.enqueue_in(delay, :user_email, self.class.notification_params(notification, type))
|
2013-08-24 19:21:39 +08:00
|
|
|
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
|
2016-01-27 09:19:49 +08:00
|
|
|
|
2016-02-01 17:14:30 +08:00
|
|
|
def private_delay
|
2018-01-31 14:07:10 +08:00
|
|
|
SiteSetting.personal_email_time_window_seconds
|
2016-02-01 17:14:30 +08:00
|
|
|
end
|
|
|
|
|
2016-02-02 02:12:10 +08:00
|
|
|
def post_type
|
|
|
|
@post_type ||= begin
|
|
|
|
type = notification.data_hash["original_post_type"] if notification.data_hash
|
|
|
|
type ||= notification.post.try(:post_type)
|
|
|
|
type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-12-22 12:29:34 +08:00
|
|
|
def self.disable
|
|
|
|
@disabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enable
|
|
|
|
@disabled = false
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
def self.process_notification(notification)
|
2016-12-22 12:29:34 +08:00
|
|
|
return if @disabled
|
|
|
|
|
2013-08-24 19:21:39 +08:00
|
|
|
email_user = EmailUser.new(notification)
|
2016-01-27 09:19:49 +08:00
|
|
|
email_method = Notification.types[notification.notification_type]
|
2016-02-02 02:12:10 +08:00
|
|
|
|
2013-08-24 19:21:39 +08:00
|
|
|
email_user.send(email_method) if email_user.respond_to? email_method
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2016-01-27 09:19:49 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|