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
This commit is contained in:
Guo Xiang Tan 2018-05-17 16:09:21 +08:00
parent 33899664ce
commit 147ea37115
3 changed files with 49 additions and 25 deletions

View File

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

View File

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

View File

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