FEATURE: Send an email notification when a post is approved. (#12665)

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.

diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 6ccb647..fa99a53 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 af0e986..8d108c6 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 21e31e5..a1e7f12 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 03737f1..c20e0e8 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 537d10e..605511e 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 69f6221..bb6c38a 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

GitHub sha: 958fbfb7

This commit appears in #12665 which was approved by eviltrout. It was merged by romanrizzi.