From 3d99726981daa0048157c502dbcb45ab36c3c86d Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Mon, 16 Apr 2018 11:48:06 +0200 Subject: [PATCH] FIX: set notification level when changing post owner (#5616) FIX: do not notify last post editor if they mention themself --- app/services/post_alerter.rb | 2 +- app/services/post_owner_changer.rb | 6 ++++++ spec/services/post_alerter_spec.rb | 7 +++++++ spec/services/post_owner_changer_spec.rb | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 44973d7adf8..2c30ada8185 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -47,7 +47,7 @@ class PostAlerter end def after_save_post(post, new_record = false) - notified = [post.user] + notified = [post.user, post.last_editor].uniq # mentions (users/groups) mentioned_groups, mentioned_users = extract_mentions(post) diff --git a/app/services/post_owner_changer.rb b/app/services/post_owner_changer.rb index 4e2d894cbec..8d4e261e8b8 100644 --- a/app/services/post_owner_changer.rb +++ b/app/services/post_owner_changer.rb @@ -23,6 +23,12 @@ class PostOwnerChanger post.set_owner(@new_owner, @acting_user, @skip_revision) PostAction.remove_act(@new_owner, post, PostActionType.types[:like]) + if post.post_number == 1 + TopicUser.change(@new_owner.id, @topic.id, notification_level: NotificationLevels.topic_levels[:watching]) + else + TopicUser.change(@new_owner.id, @topic.id, notification_level: NotificationLevels.topic_levels[:tracking]) + end + @topic.update_statistics @new_owner.user_stat.update( diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index e36d603df0c..20b6e8a7785 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -410,6 +410,13 @@ describe PostAlerter do expect(n.data_hash["original_username"]).to eq(admin.username) end + it "doesn't notify the last post editor if they mention themself" do + post = create_post_with_alerts(user: user, raw: 'Post without a mention.') + expect { + post.revise(evil_trout, raw: "O hai, @eviltrout!") + }.not_to change(evil_trout.notifications, :count) + end + let(:alice) { Fabricate(:user, username: 'alice') } let(:bob) { Fabricate(:user, username: 'bob') } let(:carol) { Fabricate(:admin, username: 'carol') } diff --git a/spec/services/post_owner_changer_spec.rb b/spec/services/post_owner_changer_spec.rb index e462bbe9bd7..5ad9d8af574 100644 --- a/spec/services/post_owner_changer_spec.rb +++ b/spec/services/post_owner_changer_spec.rb @@ -75,6 +75,22 @@ describe PostOwnerChanger do expect(p1.reload.user).to eq(user_a) end + context "sets topic notification level for the new owner" do + let(:p4) { Fabricate(:post, post_number: 2, topic_id: topic.id) } + + it "'watching' if the first post gets a new owner" do + described_class.new(post_ids: [p1.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! + tu = TopicUser.find_by(user_id: user_a.id, topic_id: topic.id) + expect(tu.notification_level).to eq(3) + end + + it "'tracking' if other than the first post gets a new owner" do + described_class.new(post_ids: [p4.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! + tu = TopicUser.find_by(user_id: user_a.id, topic_id: topic.id) + expect(tu.notification_level).to eq(2) + end + end + context "integration tests" do let(:p1user) { p1.user } let(:p2user) { p2.user }