2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class NotifyMailingListSubscribers < ::Jobs::Base
|
2018-08-20 23:14:43 -04:00
|
|
|
include Skippable
|
2014-02-06 19:06:35 -05:00
|
|
|
|
2018-10-04 10:56:24 -04:00
|
|
|
RETRY_TIMES = [5.minute, 15.minute, 30.minute, 45.minute, 90.minute, 180.minute, 300.minute]
|
|
|
|
|
2016-04-06 22:56:43 -04:00
|
|
|
sidekiq_options queue: 'low'
|
|
|
|
|
2018-10-04 10:56:24 -04:00
|
|
|
sidekiq_options retry: RETRY_TIMES.size
|
|
|
|
|
|
|
|
sidekiq_retry_in do |count, exception|
|
2021-07-05 23:37:52 -04:00
|
|
|
# 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
|
2018-10-04 10:56:24 -04:00
|
|
|
case exception.wrapped
|
|
|
|
when SocketError
|
2021-07-05 23:37:52 -04:00
|
|
|
return RETRY_TIMES[count]
|
2018-10-04 10:56:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
def execute(args)
|
2016-03-21 23:50:35 -04:00
|
|
|
return if SiteSetting.disable_mailing_list_mode
|
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
post_id = args[:post_id]
|
2014-11-24 11:40:21 -05:00
|
|
|
post = post_id ? Post.with_deleted.find_by(id: post_id) : nil
|
2014-02-06 19:06:35 -05:00
|
|
|
|
2021-08-26 01:16:35 -04:00
|
|
|
return if !post || post.trashed? || post.user_deleted? ||
|
|
|
|
!post.topic || post.raw.blank? || post.topic.private_message?
|
2014-02-06 19:06:35 -05:00
|
|
|
|
2014-09-03 17:50:19 -04:00
|
|
|
users =
|
2017-11-10 12:18:08 -05:00
|
|
|
User.activated.not_silenced.not_suspended.real
|
2016-02-16 23:46:19 -05:00
|
|
|
.joins(:user_option)
|
2016-10-05 19:28:58 -04:00
|
|
|
.where('user_options.mailing_list_mode AND user_options.mailing_list_mode_frequency > 0')
|
2016-12-12 09:28:26 -05:00
|
|
|
.where('NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM muted_users mu
|
|
|
|
WHERE mu.muted_user_id = ? AND mu.user_id = users.id
|
|
|
|
)', post.user_id)
|
2019-03-21 07:15:34 -04:00
|
|
|
.where('NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM ignored_users iu
|
|
|
|
WHERE iu.ignored_user_id = ? AND iu.user_id = users.id
|
|
|
|
)', post.user_id)
|
2016-12-12 09:28:26 -05:00
|
|
|
.where('NOT EXISTS (
|
2014-02-06 19:06:35 -05:00
|
|
|
SELECT 1
|
|
|
|
FROM topic_users tu
|
2016-12-12 09:28:26 -05:00
|
|
|
WHERE tu.topic_id = ? AND tu.user_id = users.id AND tu.notification_level = ?
|
2014-02-06 19:06:35 -05:00
|
|
|
)', post.topic_id, TopicUser.notification_levels[:muted])
|
2016-12-12 09:28:26 -05:00
|
|
|
.where('NOT EXISTS (
|
2014-02-06 19:06:35 -05:00
|
|
|
SELECT 1
|
|
|
|
FROM category_users cu
|
2016-12-12 09:28:26 -05:00
|
|
|
WHERE cu.category_id = ? AND cu.user_id = users.id AND cu.notification_level = ?
|
2014-02-06 19:06:35 -05:00
|
|
|
)', post.topic.category_id, CategoryUser.notification_levels[:muted])
|
2014-09-03 17:50:19 -04:00
|
|
|
|
2020-02-19 15:14:42 -05:00
|
|
|
if SiteSetting.tagging_enabled?
|
|
|
|
users = users.where('NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM tag_users tu
|
|
|
|
WHERE tu.tag_id in (:tag_ids) AND tu.user_id = users.id AND tu.notification_level = :muted
|
|
|
|
)', tag_ids: post.topic.tag_ids, muted: TagUser.notification_levels[:muted])
|
|
|
|
end
|
|
|
|
|
2017-12-12 23:13:17 -05:00
|
|
|
if SiteSetting.must_approve_users
|
|
|
|
users = users.where(approved: true)
|
|
|
|
end
|
|
|
|
|
2020-12-15 17:30:21 -05:00
|
|
|
if SiteSetting.mute_all_categories_by_default
|
|
|
|
users = users.watching_topic_when_mute_categories_by_default(post.topic)
|
|
|
|
end
|
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
DiscourseEvent.trigger(:notify_mailing_list_subscribers, users, post)
|
2017-05-04 22:57:31 -04:00
|
|
|
users.find_each do |user|
|
2014-09-03 17:50:19 -04:00
|
|
|
if Guardian.new(user).can_see?(post)
|
2016-07-05 06:20:07 -04:00
|
|
|
if EmailLog.reached_max_emails?(user)
|
2018-07-24 00:55:43 -04:00
|
|
|
skip(user.email, user.id, post.id,
|
|
|
|
SkippedEmailLog.reason_types[:exceeded_emails_limit]
|
|
|
|
)
|
|
|
|
|
2016-07-05 06:20:07 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if user.user_stat.bounce_score >= SiteSetting.bounce_score_threshold
|
2018-07-24 00:55:43 -04:00
|
|
|
skip(user.email, user.id, post.id,
|
|
|
|
SkippedEmailLog.reason_types[:exceeded_bounces_limit]
|
|
|
|
)
|
|
|
|
|
2016-07-05 06:20:07 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-10-05 19:28:58 -04:00
|
|
|
if (user.id == post.user_id) && (user.user_option.mailing_list_mode_frequency == 2)
|
2018-07-24 00:55:43 -04:00
|
|
|
skip(user.email, user.id, post.id,
|
|
|
|
SkippedEmailLog.reason_types[:mailing_list_no_echo_mode]
|
|
|
|
)
|
|
|
|
|
2016-10-05 19:28:58 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2014-09-03 17:50:19 -04:00
|
|
|
begin
|
2016-07-05 06:20:07 -04:00
|
|
|
if message = UserNotifications.mailing_list_notify(user, post)
|
|
|
|
EmailLog.unique_email_per_post(post, user) do
|
|
|
|
Email::Sender.new(message, :mailing_list, user).send
|
2016-04-15 01:59:01 -04:00
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
end
|
2014-09-03 17:50:19 -04:00
|
|
|
rescue => e
|
2016-02-03 13:27:58 -05:00
|
|
|
Discourse.handle_job_exception(e, error_context(args, "Sending post to mailing list subscribers", user_id: user.id, user_email: user.email))
|
2014-09-03 17:50:19 -04:00
|
|
|
end
|
|
|
|
end
|
2014-02-06 19:06:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2016-07-05 06:20:07 -04:00
|
|
|
|
2018-07-24 00:55:43 -04:00
|
|
|
def skip(to_address, user_id, post_id, reason_type)
|
2018-08-20 23:14:43 -04:00
|
|
|
create_skipped_email_log(
|
2016-07-05 06:20:07 -04:00
|
|
|
email_type: 'mailing_list',
|
|
|
|
to_address: to_address,
|
|
|
|
user_id: user_id,
|
|
|
|
post_id: post_id,
|
2018-07-24 00:55:43 -04:00
|
|
|
reason_type: reason_type
|
2018-08-20 23:14:43 -04:00
|
|
|
)
|
2016-07-05 06:20:07 -04:00
|
|
|
end
|
2014-02-06 19:06:35 -05:00
|
|
|
end
|
|
|
|
end
|