FIX: Notify on tag change. (#7119)

This commit is contained in:
Bianca Nenciu 2019-03-12 19:09:34 +02:00 committed by Régis Hanol
parent e5e2fa4064
commit c6ed86220e
3 changed files with 46 additions and 1 deletions

View File

@ -0,0 +1,15 @@
require_dependency "post_alerter"
module Jobs
class NotifyTagChange < Jobs::Base
def execute(args)
post = Post.find_by(id: args[:post_id])
if post&.topic
post_alerter = PostAlerter.new
post_alerter.notify_post_users(post, User.where(id: args[:notified_user_ids]))
post_alerter.notify_first_post_watchers(post, post_alerter.tag_watchers(post.topic))
end
end
end
end

View File

@ -83,7 +83,14 @@ class PostRevisor
tc.check_result(false)
next
end
tc.record_change('tags', prev_tags, tags) unless prev_tags.sort == tags.sort
if prev_tags.sort != tags.sort
tc.record_change('tags', prev_tags, tags)
DB.after_commit do
post = tc.topic.ordered_posts.first
notified_user_ids = [post.user_id, post.last_editor_id].uniq
Jobs.enqueue(:notify_tag_change, post_id: post.id, notified_user_ids: notified_user_ids)
end
end
end
end

View File

@ -926,6 +926,29 @@ describe PostAlerter do
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
end
end
context "on change" do
let(:user) { Fabricate(:user) }
let(:other_tag) { Fabricate(:tag) }
let(:watched_tag) { Fabricate(:tag) }
let(:post) { Fabricate(:post) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.queue_jobs = false
end
it "triggers a notification" do
TagUser.change(user.id, watched_tag.id, TagUser.notification_levels[:watching_first_post])
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
PostRevisor.new(post).revise!(Fabricate(:user), tags: [other_tag.name, watched_tag.name])
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
PostRevisor.new(post).revise!(Fabricate(:user), tags: [watched_tag.name, other_tag.name])
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
end
end
end
describe '#extract_linked_users' do