FIX: Notify tag watchers when publishing topic (#17576)

When a topic was published from a shared draft and it had tags, the
users watching the tags were not notified. The problem was that the
topics are usually created in a secret category and publishing it just
moves an existent topic to the target category, without making any
changes to the tags.
This commit is contained in:
Bianca Nenciu 2022-07-20 19:07:18 +03:00 committed by GitHub
parent 7a668460e0
commit f75a99e932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -3,7 +3,7 @@
module Jobs
class NotifyTagChange < ::Jobs::Base
def execute(args)
return if SiteSetting.disable_tags_edit_notifications
return if SiteSetting.disable_tags_edit_notifications && !args[:force]
post = Post.find_by(id: args[:post_id])

View File

@ -46,6 +46,14 @@ class TopicPublisher
end
end
Jobs.enqueue(
:notify_tag_change,
post_id: @topic.first_post.id,
notified_user_ids: [@topic.first_post.user_id, @published_by.id].uniq,
diff_tags: @topic.tags.map(&:name),
force: true,
)
MessageBus.publish("/topic/#{@topic.id}", reload_topic: true, refresh_stream: true)
@topic

View File

@ -16,6 +16,8 @@ describe TopicPublisher do
fab!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }
fab!(:moderator) { Fabricate(:moderator) }
fab!(:op) { Fabricate(:post, topic: topic) }
fab!(:user) { Fabricate(:user) }
fab!(:tag) { Fabricate(:tag) }
before do
# Create a revision
@ -51,6 +53,24 @@ describe TopicPublisher do
expect(op.last_version_at).to eq_time(published_at)
end
end
it "will notify users watching tag" do
Jobs.run_immediately!
TagUser.create!(
user_id: user.id,
tag_id: tag.id,
notification_level: NotificationLevels.topic_levels[:watching]
)
topic.update!(tags: [tag])
expect { TopicPublisher.new(topic, moderator, shared_draft.category_id).publish! }
.to change { Notification.count }.by(1)
topic.reload
expect(topic.tags).to contain_exactly(tag)
end
end
end