From 96d03ea9c0500fc715e8b6f9713488d4d8548aa6 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Thu, 2 Mar 2023 13:47:54 +0800 Subject: [PATCH] FIX: No small action created when a non-author removes itself from a PM (#20502) Fixes a small issue where allowed user removes themselves from a private message before the post activity (small action) is created. I also added some test coverage to prevent regression. /t/92811 --- app/models/topic.rb | 3 +-- spec/models/topic_spec.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 6192f727594..155736aa056 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -1103,14 +1103,13 @@ class Topic < ActiveRecord::Base topic_user = topic_allowed_users.find_by(user_id: user.id) if topic_user - topic_user.destroy - if user.id == removed_by&.id add_small_action(removed_by, "user_left", user.username) else add_small_action(removed_by, "removed_user", user.username) end + topic_user.destroy return true end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 4db4f3a5546..9f8e466a7be 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -3081,6 +3081,17 @@ RSpec.describe Topic do describe "#remove_allowed_user" do fab!(:topic) { Fabricate(:topic) } + fab!(:private_topic) do + Fabricate( + :private_message_topic, + title: "Private message", + user: admin, + topic_allowed_users: [ + Fabricate.build(:topic_allowed_user, user: admin), + Fabricate.build(:topic_allowed_user, user: user1), + ], + ) + end describe "removing oneself" do it "should remove onself" do @@ -3095,6 +3106,12 @@ RSpec.describe Topic do expect(post.post_type).to eq(Post.types[:small_action]) expect(post.action_code).to eq("user_left") end + + it "should show a small action when user removes themselves from pm" do + expect do private_topic.remove_allowed_user(user1, user1) end.to change { + private_topic.posts.where(action_code: "user_left").count + }.by(1) + end end end