From 147ea37115d043caf6b547933b97eabf34e9f27e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 17 May 2018 16:09:21 +0800 Subject: [PATCH] FIX: Missing notification for watching first post users when topic is recategorized. https://meta.discourse.org/t/not-receiving-notifications-for-announcements/87275/2?u=tgxworld --- app/models/topic.rb | 8 +++++++- app/services/post_alerter.rb | 37 +++++++++++++++++++----------------- spec/models/topic_spec.rb | 29 +++++++++++++++++++++------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 4ee00680016..369d0e3367a 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -677,10 +677,16 @@ SQL post = self.ordered_posts.first if post - PostAlerter.new.notify_post_users( + post_alerter = PostAlerter.new + + post_alerter.notify_post_users( post, [post.user, post.last_editor].uniq ) + + post_alerter.notify_first_post_watchers( + post, post_alerter.category_watchers(self) + ) end end diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 2bc5524cf48..66648a69ee0 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -101,28 +101,31 @@ class PostAlerter topic = post.topic if topic.present? - cat_watchers = topic.category_users - .where(notification_level: CategoryUser.notification_levels[:watching_first_post]) - .pluck(:user_id) - - tag_watchers = topic.tag_users - .where(notification_level: TagUser.notification_levels[:watching_first_post]) - .pluck(:user_id) - - group_ids = topic.allowed_groups.pluck(:group_id) - - group_watchers = GroupUser.where( - group_id: group_ids, - notification_level: GroupUser.notification_levels[:watching_first_post] - ).pluck(:user_id) - - watchers = [cat_watchers, tag_watchers, group_watchers].flatten - + watchers = category_watchers(topic) + tag_watchers(topic) + group_watchers(topic) notify_first_post_watchers(post, watchers) end end end + def group_watchers(topic) + GroupUser.where( + group_id: topic.allowed_groups.pluck(:group_id), + notification_level: GroupUser.notification_levels[:watching_first_post] + ).pluck(:user_id) + end + + def tag_watchers(topic) + topic.tag_users + .where(notification_level: TagUser.notification_levels[:watching_first_post]) + .pluck(:user_id) + end + + def category_watchers(topic) + topic.category_users + .where(notification_level: CategoryUser.notification_levels[:watching_first_post]) + .pluck(:user_id) + end + def notify_first_post_watchers(post, user_ids) return if user_ids.blank? user_ids.uniq! diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 72eb939feec..2ec56aa94a5 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1212,7 +1212,7 @@ describe Topic do expect(category.reload.topic_count).to eq(0) end - describe 'user that watching the new category' do + describe 'user that is watching the new category' do it 'should generate the notification for the topic' do topic.posts << Fabricate(:post) @@ -1222,16 +1222,31 @@ describe Topic do new_category.id ) + another_user = Fabricate(:user) + + CategoryUser.set_notification_level_for_category( + another_user, + CategoryUser::notification_levels[:watching_first_post], + new_category.id + ) + expect do topic.change_category_to_id(new_category.id) - end.to change { Notification.count }.by(1) + end.to change { Notification.count }.by(2) - notification = Notification.last + expect(Notification.where( + user_id: user.id, + topic_id: topic.id, + post_number: 1, + notification_type: Notification.types[:posted] + ).exists?).to eq(true) - expect(notification.notification_type).to eq(Notification.types[:posted]) - expect(notification.topic_id).to eq(topic.id) - expect(notification.user_id).to eq(user.id) - expect(notification.post_number).to eq(1) + expect(Notification.where( + user_id: another_user.id, + topic_id: topic.id, + post_number: 1, + notification_type: Notification.types[:watching_first_post] + ).exists?).to eq(true) end end