2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-21 23:29:34 -05:00
|
|
|
class NotificationEmailer
|
2013-08-24 07:21:39 -04:00
|
|
|
class EmailUser
|
2021-01-07 11:49:49 -05:00
|
|
|
attr_reader :notification, :no_delay
|
2013-07-21 12:50:01 -04:00
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
def initialize(notification, no_delay: false)
|
2013-08-24 07:21:39 -04:00
|
|
|
@notification = notification
|
2021-01-07 11:49:49 -05:00
|
|
|
@no_delay = no_delay
|
2013-08-24 07:21:39 -04:00
|
|
|
end
|
2013-08-07 13:02:49 -04:00
|
|
|
|
2015-11-30 01:03:47 -05:00
|
|
|
def group_mentioned
|
|
|
|
enqueue :group_mentioned
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2022-12-13 18:22:26 -05:00
|
|
|
def watching_category_or_tag
|
|
|
|
enqueue :user_posted
|
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def quoted
|
|
|
|
enqueue :user_quoted
|
|
|
|
end
|
|
|
|
|
|
|
|
def replied
|
|
|
|
enqueue :user_replied
|
|
|
|
end
|
|
|
|
|
2016-02-16 12:29:23 -05:00
|
|
|
def linked
|
|
|
|
enqueue :user_linked
|
|
|
|
end
|
|
|
|
|
2016-07-07 12:23:19 -04:00
|
|
|
def watching_first_post
|
|
|
|
enqueue :user_watching_first_post
|
|
|
|
end
|
|
|
|
|
2021-04-12 11:08:23 -04:00
|
|
|
def post_approved
|
|
|
|
enqueue :post_approved
|
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def private_message
|
2016-02-01 04:14:30 -05:00
|
|
|
enqueue_private(:user_private_message)
|
2013-08-24 07:21:39 -04:00
|
|
|
end
|
2013-02-27 15:38:44 -05:00
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
def invited_to_private_message
|
2016-02-01 04:14:30 -05:00
|
|
|
enqueue(:user_invited_to_private_message, private_delay)
|
2013-08-24 07:21:39 -04:00
|
|
|
end
|
|
|
|
|
2015-03-30 14:36:47 -04:00
|
|
|
def invited_to_topic
|
2016-02-01 04:14:30 -05:00
|
|
|
enqueue(:user_invited_to_topic, private_delay)
|
2015-03-30 14:36:47 -04:00
|
|
|
end
|
|
|
|
|
2016-01-26 20:19:49 -05:00
|
|
|
def self.notification_params(notification, type)
|
|
|
|
post_id = (notification.data_hash[:original_post_id] || notification.post_id).to_i
|
2021-04-12 11:08:23 -04:00
|
|
|
notification_type = Notification.types[notification.notification_type]
|
2016-01-26 20:19:49 -05:00
|
|
|
|
|
|
|
hash = {
|
2022-02-04 14:58:34 -05:00
|
|
|
type: type.to_s,
|
2016-01-26 20:19:49 -05:00
|
|
|
user_id: notification.user_id,
|
|
|
|
notification_id: notification.id,
|
|
|
|
notification_data_hash: notification.data_hash,
|
2022-02-04 14:58:34 -05:00
|
|
|
notification_type: notification_type.to_s,
|
2016-01-26 20:19:49 -05:00
|
|
|
}
|
|
|
|
|
2021-04-12 11:08:23 -04:00
|
|
|
hash[:post_id] = post_id if post_id > 0 && notification_type != :post_approved
|
2016-01-26 20:19:49 -05:00
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2013-08-24 07:21:39 -04:00
|
|
|
private
|
|
|
|
|
2016-01-18 19:39:57 -05:00
|
|
|
EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]]
|
|
|
|
|
2015-11-27 13:09:35 -05:00
|
|
|
def enqueue(type, delay = default_delay)
|
2019-03-15 10:55:11 -04:00
|
|
|
return if notification.user.user_option.email_level == UserOption.email_level_types[:never]
|
2016-02-01 13:12:10 -05:00
|
|
|
perform_enqueue(type, delay)
|
2013-08-24 07:21:39 -04:00
|
|
|
end
|
|
|
|
|
2016-02-01 04:14:30 -05:00
|
|
|
def enqueue_private(type, delay = private_delay)
|
2018-01-11 23:43:40 -05:00
|
|
|
if notification.user.user_option.nil?
|
2018-01-15 01:02:31 -05:00
|
|
|
# this can happen if we roll back user creation really early
|
|
|
|
# or delete user
|
|
|
|
# bypass this pm
|
2018-01-11 23:43:40 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-03-15 10:55:11 -04:00
|
|
|
if notification.user.user_option.email_messages_level == UserOption.email_level_types[:never]
|
|
|
|
return
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2016-02-01 13:12:10 -05:00
|
|
|
perform_enqueue(type, delay)
|
2016-01-26 20:19:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_enqueue(type, delay)
|
2016-06-16 12:55:47 -04:00
|
|
|
user = notification.user
|
|
|
|
return unless user.active? || user.staged?
|
2017-08-28 17:32:07 -04:00
|
|
|
return if SiteSetting.must_approve_users? && !user.approved? && !user.staged?
|
2024-03-12 23:05:34 -04:00
|
|
|
if user.staged? &&
|
|
|
|
(
|
|
|
|
type == :user_linked || type == :user_quoted || type == :user_mentioned ||
|
|
|
|
type == :group_mentioned
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
2016-06-16 12:55:47 -04:00
|
|
|
|
2024-05-27 06:27:13 -04:00
|
|
|
return if EMAILABLE_POST_TYPES.exclude?(post_type)
|
2015-06-09 03:35:26 -04:00
|
|
|
|
2016-01-26 20:19:49 -05:00
|
|
|
Jobs.enqueue_in(delay, :user_email, self.class.notification_params(notification, type))
|
2013-08-24 07:21:39 -04:00
|
|
|
end
|
2013-08-26 00:55:35 -04:00
|
|
|
|
2015-11-27 13:09:35 -05:00
|
|
|
def default_delay
|
2021-01-07 11:49:49 -05:00
|
|
|
no_delay ? 0 : SiteSetting.email_time_window_mins.minutes
|
2013-08-26 00:55:35 -04:00
|
|
|
end
|
2016-01-26 20:19:49 -05:00
|
|
|
|
2016-02-01 04:14:30 -05:00
|
|
|
def private_delay
|
2021-01-07 11:49:49 -05:00
|
|
|
no_delay ? 0 : SiteSetting.personal_email_time_window_seconds
|
2016-02-01 04:14:30 -05:00
|
|
|
end
|
|
|
|
|
2016-02-01 13:12:10 -05: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-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:29:34 -05:00
|
|
|
def self.disable
|
|
|
|
@disabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enable
|
|
|
|
@disabled = false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
def self.process_notification(notification, no_delay: false)
|
2016-12-21 23:29:34 -05:00
|
|
|
return if @disabled
|
|
|
|
|
2021-01-07 11:49:49 -05:00
|
|
|
email_user = EmailUser.new(notification, no_delay: no_delay)
|
2016-01-26 20:19:49 -05:00
|
|
|
email_method = Notification.types[notification.notification_type]
|
2023-11-08 11:29:00 -05:00
|
|
|
|
2023-12-06 17:25:00 -05:00
|
|
|
if DiscoursePluginRegistry.email_notification_filters.any? { |filter|
|
|
|
|
!filter.call(notification)
|
|
|
|
}
|
|
|
|
return
|
2023-11-08 11:29:00 -05:00
|
|
|
end
|
|
|
|
|
2019-05-06 21:27:05 -04:00
|
|
|
email_user.public_send(email_method) if email_user.respond_to? email_method
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|