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