Add `DiscourseEvent` trigger when user's topic notification level changes.

This commit is contained in:
Guo Xiang Tan 2017-04-12 11:44:00 +08:00
parent 66a7b0c30b
commit 7cb389a235
2 changed files with 24 additions and 2 deletions

View File

@ -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

View File

@ -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)