DEV: Refactor `Jobs::UserEmail` a little

This commit is contained in:
Loïc Guitaut 2023-03-09 16:30:25 +01:00 committed by Loïc Guitaut
parent 964f37476d
commit 8a8f2758d5
2 changed files with 17 additions and 15 deletions

View File

@ -34,11 +34,13 @@ module Jobs
# of extra work when emails are disabled.
return if quit_email_early?
args[:type] = args[:type].to_s
send_user_email(args)
if args[:user_id].present? && args[:type].to_s == "digest"
if args[:type] == "digest"
# Record every attempt at sending a digest email, even if it was skipped
UserStat.where(user_id: args[:user_id]).update_all(digest_attempted_at: Time.zone.now)
UserStat.where(user_id: args[:user_id]).update_all(digest_attempted_at: Time.current)
end
end
@ -109,14 +111,14 @@ module Jobs
end
if user.suspended?
if !type.to_s.in?(%w[user_private_message account_suspended])
if !type.in?(%w[user_private_message account_suspended])
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
end
if type.to_s == "digest"
if type == "digest"
return if user.staged
if user.last_emailed_at &&
user.last_emailed_at >
@ -188,26 +190,26 @@ module Jobs
if email_token.present?
email_args[:email_token] = email_token
if type.to_s == "confirm_new_email"
if type == "confirm_new_email"
change_req = EmailChangeRequest.find_by_new_token(email_token)
email_args[:requested_by_admin] = change_req.requested_by_admin? if change_req
end
end
email_args[:new_email] = args[:new_email] || user.email if type.to_s == "notify_old_email" ||
type.to_s == "notify_old_email_add"
email_args[:new_email] = args[:new_email] || user.email if type == "notify_old_email" ||
type == "notify_old_email_add"
if args[:client_ip] && args[:user_agent]
email_args[:client_ip] = args[:client_ip]
email_args[:user_agent] = args[:user_agent]
end
if EmailLog.reached_max_emails?(user, type.to_s)
if EmailLog.reached_max_emails?(user, type)
return skip_message(SkippedEmailLog.reason_types[:exceeded_emails_limit])
end
if !EmailLog::CRITICAL_EMAIL_TYPES.include?(type.to_s) &&
if !EmailLog::CRITICAL_EMAIL_TYPES.include?(type) &&
user.user_stat.bounce_score >= SiteSetting.bounce_score_threshold
return skip_message(SkippedEmailLog.reason_types[:exceeded_bounces_limit])
end
@ -270,12 +272,12 @@ module Jobs
end
def always_email_private_message?(user, type)
type.to_s == "user_private_message" &&
type == "user_private_message" &&
user.user_option.email_messages_level == UserOption.email_level_types[:always]
end
def always_email_regular?(user, type)
type.to_s != "user_private_message" &&
type != "user_private_message" &&
user.user_option.email_level == UserOption.email_level_types[:always]
end
end

View File

@ -462,7 +462,7 @@ RSpec.describe Jobs::UserEmail do
it "doesn't send email" do
send_email
expect(ActionMailer::Base.deliveries).to eq([])
expect(ActionMailer::Base.deliveries).to be_empty
end
end
end
@ -563,7 +563,7 @@ RSpec.describe Jobs::UserEmail do
Jobs::UserEmail.new.message_for_email(
user,
post,
:user_mentioned,
"user_mentioned",
notification,
notification_type: notification.notification_type,
notification_data_hash: notification.data_hash,
@ -847,7 +847,7 @@ RSpec.describe Jobs::UserEmail do
Jobs::UserEmail.new.message_for_email(
suspended,
Fabricate.build(:post),
:user_private_message,
"user_private_message",
notification,
)
@ -873,7 +873,7 @@ RSpec.describe Jobs::UserEmail do
Jobs::UserEmail.new.message_for_email(
suspended,
@pm_from_staff,
:user_private_message,
"user_private_message",
@pm_notification,
)
end