From 7cb389a235c3cafbef7581e41b7c263492a1f9a7 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 12 Apr 2017 11:44:00 +0800 Subject: [PATCH] Add `DiscourseEvent` trigger when user's topic notification level changes. --- app/models/topic_user.rb | 12 +++++++++++- spec/models/topic_user_spec.rb | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 49c43e7b4e0..eb6cceeaf59 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -136,7 +136,17 @@ SQL end if attrs[:notification_level] - MessageBus.publish("/topic/#{topic_id}", { notification_level_change: attrs[:notification_level] }, user_ids: [user_id]) + MessageBus.publish( + "/topic/#{topic_id}", + { notification_level_change: attrs[:notification_level] }, + user_ids: [user_id] + ) + + DiscourseEvent.trigger(:topic_notification_level_changed, + attrs[:notification_level], + user_id, + topic_id + ) end rescue ActiveRecord::RecordNotUnique diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index 6b8cc4b889a..6b6d886f050 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -72,7 +72,7 @@ describe TopicUser do guardian = Guardian.new(u) TopicCreator.create(u, guardian, title: "this is my topic title") } - let(:topic_user) { TopicUser.get(topic,user) } + let(:topic_user) { TopicUser.get(topic, user) } let(:topic_creator_user) { TopicUser.get(topic, topic.user) } let(:post) { Fabricate(:post, topic: topic, user: user) } @@ -99,6 +99,18 @@ describe TopicUser do end describe 'notifications' do + it 'should trigger the right DiscourseEvent' do + begin + called = false + DiscourseEvent.on(:topic_notification_level_changed) { called = true } + + TopicUser.change(user.id, topic.id, notification_level: TopicUser.notification_levels[:tracking]) + + expect(called).to eq(true) + ensure + DiscourseEvent.off(:topic_notification_level_changed) { called = true } + end + end it 'should be set to tracking if auto_track_topics is enabled' do user.user_option.update_column(:auto_track_topics_after_msecs, 0)