FEATURE: send notification to OP when staff marked a post as solution. (#97)

FEATURE: send notification to OP when staff marked a post as solution. (#97)

Previously, we won’t send any notification to the original poster when a staff user marked a post as the solution on behalf of OP.

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index bc2c68f..c35ace6 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -7,6 +7,7 @@ en:
     solved_quote_length: "Number of characters to quote when displaying the solution under the first post"
     solved_topics_auto_close_hours: "Auto close topic (n) hours after the last reply once the topic has been marked as solved. Set to 0 to disable auto closing."
     show_filter_by_solved_status: "Show a dropdown to filter a topic list by solved status."
+    notify_on_staff_accept_solved: "Send notification to the topic creator when a post is marked as solution by a staff."
   reports:
     accepted_solutions:
       title: "Accepted solutions"
diff --git a/config/settings.yml b/config/settings.yml
index 5bbd6c6..c317001 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -19,3 +19,5 @@ plugins:
   show_filter_by_solved_status:
     default: false
     client: true
+  notify_on_staff_accept_solved:
+    default: false
diff --git a/plugin.rb b/plugin.rb
index 72708d7..0c971fe 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -108,17 +108,29 @@ SQL
           )
         end
 
+        notification_data = {
+          message: 'solved.accepted_notification',
+          display_username: acting_user.username,
+          topic_title: topic.title
+        }.to_json
+
         unless acting_user.id == post.user_id
           Notification.create!(
             notification_type: Notification.types[:custom],
             user_id: post.user_id,
             topic_id: post.topic_id,
             post_number: post.post_number,
-            data: {
-              message: 'solved.accepted_notification',
-              display_username: acting_user.username,
-              topic_title: topic.title
-            }.to_json
+            data: notification_data
+          )
+        end
+
+        if SiteSetting.notify_on_staff_accept_solved && acting_user.id != topic.user_id
+          Notification.create!(
+            notification_type: Notification.types[:custom],
+            user_id: topic.user_id,
+            topic_id: post.topic_id,
+            post_number: post.post_number,
+            data: notification_data
           )
         end
 
diff --git a/spec/integration/solved_spec.rb b/spec/integration/solved_spec.rb
index f1cd055..dbeb3cd 100644
--- a/spec/integration/solved_spec.rb
+++ b/spec/integration/solved_spec.rb
@@ -64,6 +64,32 @@ RSpec.describe "Managing Posts solved status" do
       expect(topic.public_topic_timer.based_on_last_post).to eq(true)
     end
 
+    it 'sends notifications to correct users' do
+      SiteSetting.notify_on_staff_accept_solved = true
+      user = Fabricate(:user)
+      topic = Fabricate(:topic, user: user)
+      post = Fabricate(:post, post_number: 2, topic: topic)
+
+      op = topic.user
+      user = post.user
+
+      expect {
+        DiscourseSolved.accept_answer!(post, Discourse.system_user)
+      }.to \
+        change { user.notifications.count }.by(1) &
+        change { op.notifications.count }.by(1)
+
+      notification = user.notifications.last
+      expect(notification.notification_type).to eq(Notification.types[:custom])
+      expect(notification.topic_id).to eq(post.topic_id)
+      expect(notification.post_number).to eq(post.post_number)
+
+      notification = op.notifications.last
+      expect(notification.notification_type).to eq(Notification.types[:custom])
+      expect(notification.topic_id).to eq(post.topic_id)
+      expect(notification.post_number).to eq(post.post_number)
+    end
+
     it 'does not set a timer when the topic is closed' do
       topic.update!(closed: true)
       post "/solution/accept.json", params: { id: p1.id }

GitHub sha: f04ccbd4

This commit appears in #97 which was approved by eviltrout. It was merged by vinothkannans.