diff --git a/app/jobs/regular/notify_tag_change.rb b/app/jobs/regular/notify_tag_change.rb new file mode 100644 index 0000000000..b0fcb390c8 --- /dev/null +++ b/app/jobs/regular/notify_tag_change.rb @@ -0,0 +1,15 @@ +require_dependency "post_alerter" + +module Jobs + class NotifyTagChange < Jobs::Base + def execute(args) + post = Post.find_by(id: args[:post_id]) + + if post&.topic + post_alerter = PostAlerter.new + post_alerter.notify_post_users(post, User.where(id: args[:notified_user_ids])) + post_alerter.notify_first_post_watchers(post, post_alerter.tag_watchers(post.topic)) + end + end + end +end diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index e3c2407b0e..bd0a800596 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -83,7 +83,14 @@ class PostRevisor tc.check_result(false) next end - tc.record_change('tags', prev_tags, tags) unless prev_tags.sort == tags.sort + if prev_tags.sort != tags.sort + tc.record_change('tags', prev_tags, tags) + DB.after_commit do + post = tc.topic.ordered_posts.first + notified_user_ids = [post.user_id, post.last_editor_id].uniq + Jobs.enqueue(:notify_tag_change, post_id: post.id, notified_user_ids: notified_user_ids) + end + end end end diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index c2f414041c..7108917c69 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -926,6 +926,29 @@ describe PostAlerter do expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post]) end end + + context "on change" do + let(:user) { Fabricate(:user) } + let(:other_tag) { Fabricate(:tag) } + let(:watched_tag) { Fabricate(:tag) } + let(:post) { Fabricate(:post) } + + before do + SiteSetting.tagging_enabled = true + SiteSetting.queue_jobs = false + end + + it "triggers a notification" do + TagUser.change(user.id, watched_tag.id, TagUser.notification_levels[:watching_first_post]) + expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0) + + PostRevisor.new(post).revise!(Fabricate(:user), tags: [other_tag.name, watched_tag.name]) + expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1) + + PostRevisor.new(post).revise!(Fabricate(:user), tags: [watched_tag.name, other_tag.name]) + expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1) + end + end end describe '#extract_linked_users' do