From 3ceff0a92a8aeb629fe1a18d57542a039ef55533 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 15 Apr 2019 16:19:32 -0400 Subject: [PATCH] FEATURE: Send the user a notification when their post is approved --- .../javascripts/discourse-common/lib/icon-library.js.es6 | 3 ++- app/models/notification.rb | 5 +++-- app/models/reviewable_queued_post.rb | 8 ++++++++ config/locales/client.en.yml | 1 + spec/models/reviewable_queued_post_spec.rb | 6 ++++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 b/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 index 5f39e062866..a8751806a47 100644 --- a/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 +++ b/app/assets/javascripts/discourse-common/lib/icon-library.js.es6 @@ -34,7 +34,8 @@ const REPLACEMENTS = { "notification.granted_badge": "certificate", "notification.topic_reminder": "far-clock", "notification.watching_first_post": "far-dot-circle", - "notification.group_message_summary": "group" + "notification.group_message_summary": "group", + "notification.post_approved": "check" }; // TODO: use lib/svg_sprite/fa4-renames.json here diff --git a/app/models/notification.rb b/app/models/notification.rb index ba49fbb624a..60ffd7f26c9 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -61,6 +61,7 @@ class Notification < ActiveRecord::Base watching_first_post: 17, topic_reminder: 18, liked_consolidated: 19, + post_approved: 20 ) end @@ -124,10 +125,10 @@ class Notification < ActiveRecord::Base # Be wary of calling this frequently. O(n) JSON parsing can suck. def data_hash @data_hash ||= begin - return nil if data.blank? + return {} if data.blank? parsed = JSON.parse(data) - return nil if parsed.blank? + return {} if parsed.blank? parsed.with_indifferent_access end diff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rb index ddf2b4dc28b..289705b8152 100644 --- a/app/models/reviewable_queued_post.rb +++ b/app/models/reviewable_queued_post.rb @@ -72,6 +72,14 @@ class ReviewableQueuedPost < Reviewable # Backwards compatibility, new code should listen for `reviewable_transitioned_to` DiscourseEvent.trigger(:approved_post, self, created_post) + Notification.create!( + notification_type: Notification.types[:post_approved], + user_id: created_by.id, + data: {}, + topic_id: created_post.topic_id, + post_number: created_post.post_number + ) + create_result(:success, :approved) { |result| result.created_post = created_post } end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index c7d34c3e21c..fdf332d66d6 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1638,6 +1638,7 @@ en: none: "Unable to load notifications at this time." empty: "No notifications found." more: "view older notifications" + post_approved: "Your post was approved" total_flagged: "total flagged posts" mentioned: "{{username}} {{description}}" group_mentioned: "{{username}} {{description}}" diff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rb index 17cba67f6ed..a7c78f49900 100644 --- a/spec/models/reviewable_queued_post_spec.rb +++ b/spec/models/reviewable_queued_post_spec.rb @@ -57,6 +57,12 @@ RSpec.describe ReviewableQueuedPost, type: :model do expect(Topic.count).to eq(topic_count) expect(Post.count).to eq(post_count + 1) + notifications = Notification.where( + user: reviewable.created_by, + notification_type: Notification.types[:post_approved] + ) + expect(notifications).to be_present + # We can't approve twice expect(-> { reviewable.perform(moderator, :approve) }).to raise_error(Reviewable::InvalidAction) end