2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
module Jobs
|
|
|
|
# Asynchronously send an email to a user
|
2019-10-02 12:01:53 +08:00
|
|
|
class UserEmail < ::Jobs::Base
|
2018-08-21 11:14:43 +08:00
|
|
|
include Skippable
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-05-22 13:20:55 +08:00
|
|
|
sidekiq_options queue: "low"
|
|
|
|
|
2021-07-06 11:37:52 +08:00
|
|
|
sidekiq_retry_in do |count, exception|
|
|
|
|
# retry in an hour when SMTP server is busy
|
|
|
|
# or use default sidekiq retry formula. returning
|
|
|
|
# nil/0 will trigger the default sidekiq
|
|
|
|
# retry formula
|
|
|
|
#
|
|
|
|
# See https://github.com/mperham/sidekiq/blob/3330df0ee37cfd3e0cd3ef01e3e66b584b99d488/lib/sidekiq/job_retry.rb#L216-L234
|
|
|
|
case exception.wrapped
|
|
|
|
when Net::SMTPServerBusy
|
|
|
|
return 1.hour + (rand(30) * (count + 1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-01 01:15:45 +08:00
|
|
|
# Can be overridden by subclass, for example critical email
|
|
|
|
# should always consider being sent
|
|
|
|
def quit_email_early?
|
|
|
|
SiteSetting.disable_emails == "yes"
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def execute(args)
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present?
|
2016-01-29 23:49:49 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:type) unless args[:type].present?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-10-01 01:15:45 +08:00
|
|
|
# This is for performance. Quit out fast without doing a bunch
|
|
|
|
# of extra work when emails are disabled.
|
|
|
|
return if quit_email_early?
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
args[:type] = args[:type].to_s
|
|
|
|
|
2020-10-07 22:30:38 +08:00
|
|
|
send_user_email(args)
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
if args[:type] == "digest"
|
2020-10-07 22:30:38 +08:00
|
|
|
# Record every attempt at sending a digest email, even if it was skipped
|
2023-03-09 23:30:25 +08:00
|
|
|
UserStat.where(user_id: args[:user_id]).update_all(digest_attempted_at: Time.current)
|
2020-10-07 22:30:38 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_user_email(args)
|
2016-02-16 00:53:07 +08:00
|
|
|
post = nil
|
|
|
|
notification = nil
|
2016-01-27 09:19:49 +08:00
|
|
|
type = args[:type]
|
|
|
|
user = User.find_by(id: args[:user_id])
|
2020-05-27 23:09:40 +08:00
|
|
|
to_address =
|
|
|
|
args[:to_address].presence || user&.primary_email&.email.presence || "no_email_found"
|
2016-02-16 00:53:07 +08:00
|
|
|
|
2016-02-16 23:35:57 +08:00
|
|
|
set_skip_context(type, args[:user_id], to_address, args[:post_id])
|
2015-11-07 02:19:13 +08:00
|
|
|
|
2020-05-27 23:09:40 +08:00
|
|
|
return skip(SkippedEmailLog.reason_types[:user_email_no_user]) if !user
|
|
|
|
if to_address == "no_email_found"
|
|
|
|
return skip(SkippedEmailLog.reason_types[:user_email_no_email])
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2016-02-16 00:53:07 +08:00
|
|
|
if args[:post_id].present?
|
2014-05-06 21:41:59 +08:00
|
|
|
post = Post.find_by(id: args[:post_id])
|
2018-07-24 12:55:43 +08:00
|
|
|
|
|
|
|
return skip(SkippedEmailLog.reason_types[:user_email_post_not_found]) if post.blank?
|
2019-07-01 19:57:33 +08:00
|
|
|
|
|
|
|
if !Guardian.new(user).can_see?(post)
|
|
|
|
return skip(SkippedEmailLog.reason_types[:user_email_access_denied])
|
|
|
|
end
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
if args[:notification_id].present?
|
|
|
|
notification = Notification.find_by(id: args[:notification_id])
|
|
|
|
end
|
|
|
|
|
2018-07-24 12:55:43 +08:00
|
|
|
message, skip_reason_type = message_for_email(user, post, type, notification, args)
|
2016-01-27 09:19:49 +08:00
|
|
|
|
|
|
|
if message
|
2019-03-22 05:57:09 +08:00
|
|
|
Email::Sender.new(message, type, user).send
|
2019-03-12 09:39:16 +08:00
|
|
|
|
2018-08-28 15:01:44 +08:00
|
|
|
if (b = user.user_stat.bounce_score) > SiteSetting.bounce_score_erode_on_send
|
|
|
|
# erode bounce score each time we send an email
|
|
|
|
# this means that we are punished a lot less for bounces
|
|
|
|
# and we can recover more quickly
|
|
|
|
user.user_stat.update(bounce_score: b - SiteSetting.bounce_score_erode_on_send)
|
|
|
|
end
|
2016-01-27 09:19:49 +08:00
|
|
|
else
|
2018-07-24 12:55:43 +08:00
|
|
|
skip_reason_type
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
2016-02-16 23:35:57 +08:00
|
|
|
def set_skip_context(type, user_id, to_address, post_id)
|
|
|
|
@skip_context = { type: type, user_id: user_id, to_address: to_address, post_id: post_id }
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
2016-05-07 01:34:33 +08:00
|
|
|
NOTIFICATIONS_SENT_BY_MAILING_LIST ||=
|
|
|
|
Set.new %w[posted replied mentioned group_mentioned quoted]
|
|
|
|
|
2017-09-14 02:43:36 +08:00
|
|
|
def message_for_email(user, post, type, notification, args = nil)
|
|
|
|
args ||= {}
|
|
|
|
|
|
|
|
notification_type = args[:notification_type]
|
|
|
|
notification_data_hash = args[:notification_data_hash]
|
|
|
|
email_token = args[:email_token]
|
|
|
|
to_address = args[:to_address]
|
|
|
|
|
2016-02-16 23:35:57 +08:00
|
|
|
set_skip_context(type, user.id, to_address || user.email, post.try(:id))
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-07-24 12:55:43 +08:00
|
|
|
if user.anonymous?
|
|
|
|
return skip_message(SkippedEmailLog.reason_types[:user_email_anonymous_user])
|
|
|
|
end
|
2017-09-14 02:43:36 +08:00
|
|
|
|
2023-03-08 00:58:14 +08:00
|
|
|
if user.suspended?
|
2023-03-09 23:30:25 +08:00
|
|
|
if !type.in?(%w[user_private_message account_suspended])
|
2023-03-08 00:58:14 +08:00
|
|
|
return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended_not_pm])
|
|
|
|
elsif post.topic.group_pm?
|
|
|
|
return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended])
|
|
|
|
end
|
2017-09-14 02:43:36 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
if type == "digest"
|
2019-03-23 00:33:54 +08:00
|
|
|
return if user.staged
|
|
|
|
if user.last_emailed_at &&
|
|
|
|
user.last_emailed_at >
|
|
|
|
(
|
|
|
|
user.user_option&.digest_after_minutes ||
|
|
|
|
SiteSetting.default_email_digest_frequency.to_i
|
|
|
|
).minutes.ago
|
2023-01-09 20:20:10 +08:00
|
|
|
return
|
|
|
|
end
|
2019-03-23 00:33:54 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
seen_recently =
|
|
|
|
(
|
|
|
|
user.last_seen_at.present? &&
|
|
|
|
user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago
|
|
|
|
)
|
2022-04-19 02:32:11 +08:00
|
|
|
if !args[:force_respect_seen_recently] &&
|
|
|
|
(
|
|
|
|
always_email_regular?(user, type) || always_email_private_message?(user, type) ||
|
|
|
|
user.staged
|
|
|
|
)
|
|
|
|
seen_recently = false
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
email_args = {}
|
|
|
|
|
2022-04-19 02:32:11 +08:00
|
|
|
if (post || notification || notification_type || args[:force_respect_seen_recently]) &&
|
2018-07-24 12:55:43 +08:00
|
|
|
(seen_recently && !user.suspended?)
|
|
|
|
return skip_message(SkippedEmailLog.reason_types[:user_email_seen_recently])
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
2017-03-09 02:19:11 +08:00
|
|
|
email_args[:post] = post if post
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
if notification || notification_type
|
2016-02-16 00:53:07 +08:00
|
|
|
email_args[:notification_type] ||= notification_type || notification.try(:notification_type)
|
2016-01-27 09:19:49 +08:00
|
|
|
email_args[:notification_data_hash] ||= notification_data_hash ||
|
|
|
|
notification.try(:data_hash)
|
|
|
|
|
2016-02-06 03:07:30 +08:00
|
|
|
unless String === email_args[:notification_type]
|
|
|
|
if Numeric === email_args[:notification_type]
|
|
|
|
email_args[:notification_type] = Notification.types[email_args[:notification_type]]
|
|
|
|
end
|
|
|
|
email_args[:notification_type] = email_args[:notification_type].to_s
|
|
|
|
end
|
|
|
|
|
2021-03-17 23:39:10 +08:00
|
|
|
if !SiteSetting.disable_mailing_list_mode && user.user_option.mailing_list_mode? &&
|
2016-10-06 07:28:58 +08:00
|
|
|
user.user_option.mailing_list_mode_frequency > 0 && # don't catch notifications for users on daily mailing list mode
|
2016-02-24 14:30:28 +08:00
|
|
|
(!post.try(:topic).try(:private_message?)) &&
|
2016-02-04 02:27:58 +08:00
|
|
|
NOTIFICATIONS_SENT_BY_MAILING_LIST.include?(email_args[:notification_type])
|
|
|
|
# no need to log a reason when the mail was already sent via the mailing list job
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2019-03-15 22:55:11 +08:00
|
|
|
unless always_email_regular?(user, type) || always_email_private_message?(user, type)
|
2016-01-27 09:19:49 +08:00
|
|
|
if (notification && notification.read?) || (post && post.seen?(user))
|
2018-07-24 12:55:43 +08:00
|
|
|
return skip_message(SkippedEmailLog.reason_types[:user_email_notification_already_read])
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-24 12:55:43 +08:00
|
|
|
skip_reason_type = skip_email_for_post(post, user)
|
|
|
|
return skip_message(skip_reason_type) if skip_reason_type.present?
|
2013-06-11 00:02:04 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Make sure that mailer exists
|
2016-01-27 09:19:49 +08:00
|
|
|
unless UserNotifications.respond_to?(type)
|
|
|
|
raise Discourse::InvalidParameters.new("type=#{type}")
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2016-01-27 09:19:49 +08:00
|
|
|
|
2020-10-07 11:02:24 +08:00
|
|
|
if email_token.present?
|
|
|
|
email_args[:email_token] = email_token
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
if type == "confirm_new_email"
|
2020-10-07 11:02:24 +08:00
|
|
|
change_req = EmailChangeRequest.find_by_new_token(email_token)
|
|
|
|
|
|
|
|
email_args[:requested_by_admin] = change_req.requested_by_admin? if change_req
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
email_args[:new_email] = args[:new_email] || user.email if type == "notify_old_email" ||
|
|
|
|
type == "notify_old_email_add"
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2018-10-25 17:45:31 +08:00
|
|
|
if args[:client_ip] && args[:user_agent]
|
|
|
|
email_args[:client_ip] = args[:client_ip]
|
|
|
|
email_args[:user_agent] = args[:user_agent]
|
|
|
|
end
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
if EmailLog.reached_max_emails?(user, type)
|
2018-07-24 12:55:43 +08:00
|
|
|
return skip_message(SkippedEmailLog.reason_types[:exceeded_emails_limit])
|
2016-05-03 05:15:32 +08:00
|
|
|
end
|
|
|
|
|
2023-03-09 23:30:25 +08:00
|
|
|
if !EmailLog::CRITICAL_EMAIL_TYPES.include?(type) &&
|
2017-05-03 19:36:01 +08:00
|
|
|
user.user_stat.bounce_score >= SiteSetting.bounce_score_threshold
|
2018-07-24 12:55:43 +08:00
|
|
|
return skip_message(SkippedEmailLog.reason_types[:exceeded_bounces_limit])
|
2016-03-23 12:08:34 +08:00
|
|
|
end
|
|
|
|
|
2017-09-14 02:43:36 +08:00
|
|
|
if args[:user_history_id]
|
|
|
|
email_args[:user_history] = UserHistory.where(id: args[:user_history_id]).first
|
|
|
|
end
|
|
|
|
|
2021-01-15 06:43:26 +08:00
|
|
|
email_args[:reject_reason] = args[:reject_reason]
|
|
|
|
|
2016-04-15 13:59:01 +08:00
|
|
|
message =
|
|
|
|
EmailLog.unique_email_per_post(post, user) do
|
2019-05-07 09:27:05 +08:00
|
|
|
UserNotifications.public_send(type, user, email_args)
|
2016-04-15 13:59:01 +08:00
|
|
|
end
|
2015-12-01 09:12:55 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Update the to address if we have a custom one
|
2017-03-09 02:19:11 +08:00
|
|
|
message.to = to_address if message && to_address.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-29 23:49:49 +08:00
|
|
|
[message, nil]
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
def skip_message(reason)
|
2016-01-29 23:49:49 +08:00
|
|
|
[nil, skip(reason)]
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
2013-06-11 04:46:08 +08:00
|
|
|
# If this email has a related post, don't send an email if it's been deleted or seen recently.
|
|
|
|
def skip_email_for_post(post, user)
|
2022-04-19 02:32:11 +08:00
|
|
|
return false unless post
|
2018-07-24 12:55:43 +08:00
|
|
|
|
2022-04-19 02:32:11 +08:00
|
|
|
return SkippedEmailLog.reason_types[:user_email_topic_nil] if post.topic.blank?
|
2018-07-24 12:55:43 +08:00
|
|
|
|
2022-04-19 02:32:11 +08:00
|
|
|
return SkippedEmailLog.reason_types[:user_email_post_user_deleted] if post.user.blank?
|
2018-07-24 12:55:43 +08:00
|
|
|
|
2022-04-19 02:32:11 +08:00
|
|
|
return SkippedEmailLog.reason_types[:user_email_post_deleted] if post.user_deleted?
|
2016-07-01 09:22:07 +08:00
|
|
|
|
2022-04-19 02:32:11 +08:00
|
|
|
if user.suspended? && (!post.user&.staff? || !post.user&.human?)
|
|
|
|
return SkippedEmailLog.reason_types[:user_email_user_suspended]
|
|
|
|
end
|
|
|
|
|
|
|
|
already_read =
|
|
|
|
user.user_option.email_level != UserOption.email_level_types[:always] &&
|
|
|
|
PostTiming.exists?(
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
user_id: user.id,
|
|
|
|
)
|
|
|
|
SkippedEmailLog.reason_types[:user_email_already_read] if already_read
|
2014-02-15 02:06:21 +08:00
|
|
|
end
|
|
|
|
|
2018-07-24 12:55:43 +08:00
|
|
|
def skip(reason_type)
|
2018-08-21 11:14:43 +08:00
|
|
|
create_skipped_email_log(
|
2016-01-29 23:49:49 +08:00
|
|
|
email_type: @skip_context[:type],
|
|
|
|
to_address: @skip_context[:to_address],
|
|
|
|
user_id: @skip_context[:user_id],
|
2016-02-16 23:35:57 +08:00
|
|
|
post_id: @skip_context[:post_id],
|
2018-07-24 12:55:43 +08:00
|
|
|
reason_type: reason_type,
|
2016-01-29 23:49:49 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2019-03-15 22:55:11 +08:00
|
|
|
def always_email_private_message?(user, type)
|
2023-03-09 23:30:25 +08:00
|
|
|
type == "user_private_message" &&
|
2020-09-24 00:13:21 +08:00
|
|
|
user.user_option.email_messages_level == UserOption.email_level_types[:always]
|
2019-03-15 22:55:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def always_email_regular?(user, type)
|
2023-03-09 23:30:25 +08:00
|
|
|
type != "user_private_message" &&
|
2020-09-24 00:13:21 +08:00
|
|
|
user.user_option.email_level == UserOption.email_level_types[:always]
|
2019-03-15 22:55:11 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|