Skip to content

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.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
)
- 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)
end
end