FEATURE: notify by email when there are posts from new users waiting to be reviewed

This commit is contained in:
Neil Lalonde 2015-06-18 15:46:50 -04:00
parent 455ba6ae0c
commit 77595bcaa9
5 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,34 @@
module Jobs
class PendingQueuedPostReminder < Jobs::Scheduled
every 1.hour
def execute(args)
return true unless SiteSetting.notify_about_queued_posts_after > 0 && SiteSetting.contact_email
if should_notify_ids.size > 0 && last_notified_id.to_i < should_notify_ids.max
message = PendingQueuedPostsMailer.notify(count: should_notify_ids.size)
Email::Sender.new(message, :pending_queued_posts_reminder).send
self.last_notified_id = should_notify_ids.max
end
true
end
def should_notify_ids
@_should_notify_ids ||= QueuedPost.new_posts.visible.where('created_at < ?', SiteSetting.notify_about_queued_posts_after.hours.ago).pluck(:id)
end
def last_notified_id
(i = $redis.get(self.class.last_notified_key)) && i.to_i
end
def last_notified_id=(arg)
$redis.set(self.class.last_notified_key, arg)
end
def self.last_notified_key
"last_notified_queued_post_id"
end
end
end

View File

@ -0,0 +1,10 @@
require_dependency 'email/message_builder'
class PendingQueuedPostsMailer < ActionMailer::Base
include Email::BuildEmailHelper
def notify(opts={})
return unless SiteSetting.contact_email
build_email(SiteSetting.contact_email, template: 'queued_posts_reminder', count: opts[:count])
end
end

View File

@ -1155,6 +1155,7 @@ en:
approve_post_count: "The amount of posts from a new user that must be approved"
approve_unless_trust_level: "Posts for users below this trust level must be approved"
notify_about_queued_posts_after: "If there are posts that have been waiting to be reviewed for more than this many hours, an email will be sent to the contact email. Set to 0 to disable these emails."
errors:
invalid_email: "Invalid email address."
@ -1424,6 +1425,15 @@ en:
one: "1 flag waiting to be handled"
other: "%{count} flags waiting to be handled"
queued_posts_reminder:
subject_template:
one: "[%{site_name}] 1 post waiting to be reviewed"
other: "[%{site_name}] %{count} posts waiting to be reviewed"
text_body_template: |
Hello,
There are posts from new users that are waiting to be reviewed. [They can be approved or rejected here](%{base_url}/queued-posts).
flag_reasons:
off_topic: "Your post was flagged as **off-topic**: the community feels it is not a good fit for the topic, as currently defined by the title and the first post."
inappropriate: "Your post was flagged as **inappropriate**: the community feels it is offensive, abusive, or a violation of [our community guidelines](/guidelines)."

View File

@ -450,6 +450,9 @@ posting:
approve_unless_trust_level:
default: 0
enum: 'TrustLevelSetting'
notify_about_queued_posts_after:
default: 24
min: 0
email:
email_time_window_mins:

View File

@ -0,0 +1,38 @@
require "spec_helper"
describe Jobs::PendingQueuedPostReminder do
context "notify_about_queued_posts_after is 0" do
before { SiteSetting.stubs(:notify_about_queued_posts_after).returns(0) }
it "never emails" do
described_class.any_instance.expects(:should_notify_ids).never
Email::Sender.any_instance.expects(:send).never
described_class.new.execute({})
end
end
context "notify_about_queued_posts_after is 24" do
before { SiteSetting.stubs(:notify_about_queued_posts_after).returns(24) }
it "doesn't email if there are no queued posts" do
described_class.any_instance.stubs(:should_notify_ids).returns([])
described_class.any_instance.stubs(:last_notified_id).returns(nil)
Email::Sender.any_instance.expects(:send).never
described_class.new.execute({})
end
it "emails if there are new queued posts" do
described_class.any_instance.stubs(:should_notify_ids).returns([1,2])
described_class.any_instance.stubs(:last_notified_id).returns(nil)
Email::Sender.any_instance.expects(:send).once
described_class.new.execute({})
end
it "doesn't email again about the same posts" do
described_class.any_instance.stubs(:should_notify_ids).returns([2])
described_class.any_instance.stubs(:last_notified_id).returns(2)
Email::Sender.any_instance.expects(:send).never
described_class.new.execute({})
end
end
end