2014-02-06 18:06:35 -06:00
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class NotifyMailingListSubscribers < Jobs::Base
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
post_id = args[:post_id]
|
2014-11-24 10:40:21 -06:00
|
|
|
post = post_id ? Post.with_deleted.find_by(id: post_id) : nil
|
2014-02-06 18:06:35 -06:00
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post
|
2014-11-24 10:40:21 -06:00
|
|
|
return if post.trashed? || post.user_deleted? || (!post.topic)
|
2014-02-06 18:06:35 -06:00
|
|
|
|
2014-09-03 16:50:19 -05:00
|
|
|
users =
|
|
|
|
User.activated.not_blocked.not_suspended.real
|
2016-02-03 12:27:58 -06:00
|
|
|
.where(mailing_list_mode: true)
|
2014-02-06 18:06:35 -06:00
|
|
|
.where('NOT EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM topic_users tu
|
|
|
|
WHERE
|
|
|
|
tu.topic_id = ? AND
|
|
|
|
tu.user_id = users.id AND
|
|
|
|
tu.notification_level = ?
|
|
|
|
)', post.topic_id, TopicUser.notification_levels[:muted])
|
|
|
|
.where('NOT EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM category_users cu
|
|
|
|
WHERE
|
|
|
|
cu.category_id = ? AND
|
|
|
|
cu.user_id = users.id AND
|
|
|
|
cu.notification_level = ?
|
|
|
|
)', post.topic.category_id, CategoryUser.notification_levels[:muted])
|
2014-09-03 16:50:19 -05:00
|
|
|
|
|
|
|
users.each do |user|
|
|
|
|
if Guardian.new(user).can_see?(post)
|
|
|
|
begin
|
|
|
|
message = UserNotifications.mailing_list_notify(user, post)
|
2016-02-03 12:27:58 -06:00
|
|
|
Email::Sender.new(message, :mailing_list, user).send if message
|
2014-09-03 16:50:19 -05:00
|
|
|
rescue => e
|
2016-02-03 12:27:58 -06: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 16:50:19 -05:00
|
|
|
end
|
|
|
|
end
|
2014-02-06 18:06:35 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|