discourse/app/jobs/regular/notify_mailing_list_subscribers.rb
James Kiesel feffe23cc5 FEATURE: More granular mailing list mode (#4068)
* Rearrange frontend to account for mailing list mode

* Allow update of user preference for mailing list frequency

* Add mailing list frequency estimate

* Simplify frequency estimate; disable activity summary for mailing list mode

* Remove combined updates

* Add specs for enqueue mailing list mode job

* Write mailing list method for mailer

* Fix linting error

* Account for stale topics

* Add translations for default mailing list setting

* One query for mailing list topics

* Fix failing spec

* WIP

* Flesh out html template

* First pass at text-based mailing list summary

* Add user avatar

* Properly format posts for mailing list

* Move make_all_links_absolute into Email::Styles

* Apply first_seen_at to user

* Send mailing list email summary hourly based on first_seen_at

* Branch and test cleanup

* Use existing mailing list mode estimate

* Fix failing specs
2016-05-21 15:17:54 +02:00

65 lines
2.3 KiB
Ruby

module Jobs
class NotifyMailingListSubscribers < Jobs::Base
sidekiq_options queue: 'low'
def execute(args)
return if SiteSetting.disable_mailing_list_mode
post_id = args[:post_id]
post = post_id ? Post.with_deleted.find_by(id: post_id) : nil
raise Discourse::InvalidParameters.new(:post_id) unless post
return if post.trashed? || post.user_deleted? || (!post.topic)
users =
User.activated.not_blocked.not_suspended.real
.joins(:user_option)
.where(user_options: {mailing_list_mode: true, mailing_list_mode_frequency: 1})
.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])
users.each do |user|
if Guardian.new(user).can_see?(post)
begin
if EmailLog.reached_max_emails?(user)
EmailLog.create!(
email_type: 'mailing_list',
to_address: user.email,
user_id: user.id,
post_id: post.id,
skipped: true,
skipped_reason: "[MailingList] #{I18n.t('email_log.exceeded_limit')}"
)
else
message = UserNotifications.mailing_list_notify(user, post)
if message
EmailLog.unique_email_per_post(post, user) do
Email::Sender.new(message, :mailing_list, user).send
end
end
end
rescue => e
Discourse.handle_job_exception(e, error_context(args, "Sending post to mailing list subscribers", { user_id: user.id, user_email: user.email }))
end
end
end
end
end
end