From 958fbfb7199dfe8ae475b1776489e5cd3e3348a8 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Mon, 12 Apr 2021 12:08:23 -0300 Subject: [PATCH] FEATURE: Send an email notification when a post is approved. (#12665) We now send an email when a queued post is approved, and we create a notification. --- app/mailers/user_notifications.rb | 14 ++++++++++++++ app/models/reviewable_queued_post.rb | 2 +- app/services/notification_emailer.rb | 9 +++++++-- config/locales/server.en.yml | 7 +++++++ spec/mailers/user_notifications_spec.rb | 13 +++++++++++++ spec/services/notification_emailer_spec.rb | 9 +++++++++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 6ccb6476da..fa99a5309f 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -37,6 +37,20 @@ class UserNotifications < ActionMailer::Base new_user_tips: tips) end + def post_approved(user, opts = {}) + post_url = opts.dig(:notification_data_hash, :post_url) + + return if post_url.nil? + + locale = user_locale(user) + build_email(user.email, + template: 'user_notifications.post_approved', + locale: locale, + base_url: Discourse.base_url, + post_url: post_url + ) + end + def signup_after_reject(user, opts = {}) locale = user_locale(user) build_email(user.email, diff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rb index af0e9860e7..8d108c6707 100644 --- a/app/models/reviewable_queued_post.rb +++ b/app/models/reviewable_queued_post.rb @@ -97,7 +97,7 @@ class ReviewableQueuedPost < Reviewable Notification.create!( notification_type: Notification.types[:post_approved], user_id: created_by.id, - data: {}, + data: { post_url: created_post.url }.to_json, topic_id: created_post.topic_id, post_number: created_post.post_number ) diff --git a/app/services/notification_emailer.rb b/app/services/notification_emailer.rb index 21e31e5ebb..a1e7f12efa 100644 --- a/app/services/notification_emailer.rb +++ b/app/services/notification_emailer.rb @@ -38,6 +38,10 @@ class NotificationEmailer enqueue :user_watching_first_post end + def post_approved + enqueue :post_approved + end + def private_message enqueue_private(:user_private_message) end @@ -52,16 +56,17 @@ class NotificationEmailer def self.notification_params(notification, type) post_id = (notification.data_hash[:original_post_id] || notification.post_id).to_i + notification_type = Notification.types[notification.notification_type] hash = { type: type, user_id: notification.user_id, notification_id: notification.id, notification_data_hash: notification.data_hash, - notification_type: Notification.types[notification.notification_type], + notification_type: notification_type, } - hash[:post_id] = post_id if post_id > 0 + hash[:post_id] = post_id if post_id > 0 && notification_type != :post_approved hash end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 03737f1da6..c20e0e89d8 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3924,6 +3924,13 @@ en: If this was not you, please [review your existing sessions](%{base_url}/my/preferences/account) and consider changing your password. + post_approved: + title: "Your post was approved" + subject_template: "[%{site_name}] Your post was approved" + text_body_template: | + Hello, + + This is an automated message from %{site_name} to let you know that [your post](%{base_url}%{post_url}) was approved. page_forbidden: title: "Oops! That page is private." diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 537d10edc5..605511ea89 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -80,6 +80,19 @@ describe UserNotifications do end end + describe '.post_approved' do + fab!(:post) { Fabricate(:post) } + + it 'works' do + subject = UserNotifications.post_approved(user, { notification_data_hash: { post_url: post.url } }) + + expect(subject.to).to eq([user.email]) + expect(subject.subject).to be_present + expect(subject.from).to eq([SiteSetting.notification_email]) + expect(subject.body).to be_present + end + end + describe ".confirm_new_email" do let(:opts) do { requested_by_admin: requested_by_admin, email_token: token } diff --git a/spec/services/notification_emailer_spec.rb b/spec/services/notification_emailer_spec.rb index 69f622172f..bb6c38a46c 100644 --- a/spec/services/notification_emailer_spec.rb +++ b/spec/services/notification_emailer_spec.rb @@ -244,5 +244,14 @@ describe NotificationEmailer do include_examples "enqueue_public" end + + context 'post_approved' do + let(:no_delay) { no_delay } + let(:type) { :post_approved } + let(:delay) { SiteSetting.email_time_window_mins.minutes } + let!(:notification) { create_notification(:post_approved) } + + include_examples "enqueue_public" + end end end