FIX: Error when leaving group DM channel (#31537)

Followup b5147a4634f0fd5c98262f949a8c766bfd73d290

When we aliased `leave` to `remove` and renamed
the method in `DirectMessageChannel` in the previous
commit, this inadvertantly caused an error when
unfollowing group channels in the channel list.

When clicking the X in the channel list, we hit
ChannelsCurrentUserMembershipFollowsController for the
current user and the channel, which is supposed to only
unfollow the channel for all channel types including DMs.

Group DMs have a different Leave behaviour vs Unfollow.
Leaving the channel altogether is done from the channel
settings page, the "Leave channel" button, and that
deletes the user's membership and DM user record from that
channel.

So, we were trying to do the leave channel behaviour in the
unfollow channel controller, which was returning the wrong
record for the serializer (a User not a Membership)

This fixes the issue and removes a bit of delegate/alias indirection
which was making the code a bit harder to fllow and search, even
though it was more succinct. Also adds missing specs that would
have caught this regression.
This commit is contained in:
Martin Brennan
2025-02-27 14:26:07 +10:00
committed by GitHub
parent 39f4485939
commit e92e05b22e
12 changed files with 142 additions and 50 deletions

View File

@ -89,12 +89,14 @@ RSpec.describe Chat::CategoryChannel do
end
describe "#leave" do
let(:original_method) { channel.method(:remove) }
let(:aliased_method) { channel.method(:leave) }
let(:public_category) { Fabricate(:category, read_restricted: false) }
let(:channel) { Fabricate(:category_channel, chatable: public_category) }
it "is an alias to '#remove'" do
expect(original_method.original_name).to eq(aliased_method.original_name)
expect(original_method.source_location).to eq(aliased_method.source_location)
it "calls #remove" do
Fabricate(:user_chat_channel_membership, chat_channel: channel)
user = channel.user_chat_channel_memberships.first.user
channel.expects(:remove).with(user)
channel.leave(user)
end
end

View File

@ -6,7 +6,6 @@ RSpec.describe Chat::DirectMessageChannel do
it_behaves_like "a chat channel model"
it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message).as(:user_ids) }
it { is_expected.to delegate_method(:group?).to(:direct_message).with_prefix.allow_nil }
it { is_expected.to validate_length_of(:description).is_at_most(500) }
it { is_expected.to validate_length_of(:chatable_type).is_at_most(100) }
it { is_expected.to validate_length_of(:type).is_at_most(100) }
@ -110,4 +109,15 @@ RSpec.describe Chat::DirectMessageChannel do
expect(channel.slug).to eq(nil)
end
end
describe "#direct_message_group?" do
it "returns false if the DirectMessage chatable is not for a group DM" do
channel.chatable.update!(group: false)
expect(channel).not_to be_direct_message_group
end
it "returns true if the DirectMessage chatable is for a group DM" do
channel.chatable.update!(group: true)
expect(channel).to be_direct_message_group
end
end
end