diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb index a26dc0eb0a..5c45a01c9b 100644 --- a/lib/topics_bulk_action.rb +++ b/lib/topics_bulk_action.rb @@ -68,9 +68,10 @@ class TopicsBulkAction end def dismiss_posts + highest_number_source_column = @user.staff? ? 'highest_staff_post_number' : 'highest_post_number' sql = <<~SQL UPDATE topic_users tu - SET highest_seen_post_number = t.highest_post_number , last_read_post_number = highest_post_number + SET highest_seen_post_number = t.#{highest_number_source_column} , last_read_post_number = t.#{highest_number_source_column} FROM topics t WHERE t.id = tu.topic_id AND tu.user_id = :user_id AND t.id IN (:topic_ids) SQL diff --git a/spec/components/topics_bulk_action_spec.rb b/spec/components/topics_bulk_action_spec.rb index b0788c8b78..58cc96283c 100644 --- a/spec/components/topics_bulk_action_spec.rb +++ b/spec/components/topics_bulk_action_spec.rb @@ -20,6 +20,32 @@ describe TopicsBulkAction do expect(tu.last_read_post_number).to eq(3) expect(tu.highest_seen_post_number).to eq(3) end + + context "when the user is staff" do + fab!(:user) { Fabricate(:admin) } + + context "when the highest_staff_post_number is > highest_post_number for a topic (e.g. whisper is last post)" do + it "dismisses posts" do + post1 = create_post(user: user) + p = create_post(topic_id: post1.topic_id) + create_post(topic_id: post1.topic_id) + + whisper = PostCreator.new( + user, + topic_id: post1.topic.id, + post_type: Post.types[:whisper], + raw: 'this is a whispered reply' + ).create + + TopicsBulkAction.new(user, [post1.topic_id], type: 'dismiss_posts').perform! + + tu = TopicUser.find_by(user_id: user.id, topic_id: post1.topic_id) + + expect(tu.last_read_post_number).to eq(4) + expect(tu.highest_seen_post_number).to eq(4) + end + end + end end describe "invalid operation" do