A platform for community discussion. Free, open, simple.
FIX: Missing notification for watching first post users when topic is recategorized.
Guo Xiang Tan147ea37115d0
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 4ee00680016..369d0e3367a 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -677,10 +677,16 @@ SQLpost = self.ordered_posts.firstif 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)+ )endenddiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex 2bc5524cf48..66648a69ee0 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -101,28 +101,31 @@ class PostAlertertopic = post.topicif 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)endendend+ 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.rbindex 72eb939feec..2ec56aa94a5 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -1212,7 +1212,7 @@ describe Topic doexpect(category.reload.topic_count).to eq(0)end- describe 'user that watching the new category' do+ describe 'user that is watching the new category' doit 'should generate the notification for the topic' dotopic.posts << Fabricate(:post)@@ -1222,16 +1222,31 @@ describe Topic donew_category.id)- expect do- topic.change_category_to_id(new_category.id)- end.to change { Notification.count }.by(1)+ another_user = Fabricate(:user)- notification = Notification.last+ CategoryUser.set_notification_level_for_category(+ another_user,+ CategoryUser::notification_levels[:watching_first_post],+ new_category.id+ )- 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 do+ topic.change_category_to_id(new_category.id)+ end.to change { Notification.count }.by(2)++ 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.where(+ user_id: another_user.id,+ topic_id: topic.id,+ post_number: 1,+ notification_type: Notification.types[:watching_first_post]+ ).exists?).to eq(true)endend