Skip to content

A platform for community discussion. Free, open, simple.

FIX: when rejoining a chat channel dont overwrite membership settings (#36528)

David Battersby24cc2b9dcf97
diff --git a/plugins/chat/app/services/chat/add_users_to_channel.rb b/plugins/chat/app/services/chat/add_users_to_channel.rb
index 3dfa0017a29..12192c5dfce 100644
--- a/plugins/chat/app/services/chat/add_users_to_channel.rb
+++ b/plugins/chat/app/services/chat/add_users_to_channel.rb
@@ -48,7 +48,7 @@ module Chat
class_name: Chat::DirectMessageChannel::Policy::MaxUsersExcess
transaction do
- step :upsert_memberships
+ step :create_memberships
step :recompute_users_count
step :notice_channel
end
@@ -75,7 +75,7 @@ module Chat
) + channel.chatable.users.where.not(id: guardian.user)
end
- def upsert_memberships(channel:, target_users:)
+ def create_memberships(channel:, target_users:)
always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always]
memberships =
@@ -97,7 +97,7 @@ module Chat
end
context[:added_user_ids] = ::Chat::UserChatChannelMembership
- .upsert_all(
+ .insert_all(
memberships,
unique_by: %i[user_id chat_channel_id],
returning: Arel.sql("user_id, (xmax = '0') as inserted"),
@@ -105,7 +105,7 @@ module Chat
.select { |row| row["inserted"] }
.map { |row| row["user_id"] }
- ::Chat::DirectMessageUser.upsert_all(
+ ::Chat::DirectMessageUser.insert_all(
context.added_user_ids.map do |id|
{
user_id: id,
diff --git a/plugins/chat/app/services/chat/create_direct_message_channel.rb b/plugins/chat/app/services/chat/create_direct_message_channel.rb
index 6ea4e94475b..28901846445 100644
--- a/plugins/chat/app/services/chat/create_direct_message_channel.rb
+++ b/plugins/chat/app/services/chat/create_direct_message_channel.rb
@@ -50,7 +50,7 @@ module Chat
model :direct_message, :fetch_or_create_direct_message
model :channel, :fetch_or_create_channel
step :set_optional_params
- step :update_memberships
+ step :create_memberships
step :recompute_users_count
private
@@ -101,7 +101,7 @@ module Chat
channel.update!(optional_params) if !optional_params.empty?
end
- def update_memberships(channel:, target_users:)
+ def create_memberships(channel:, target_users:)
always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always]
memberships =
@@ -110,14 +110,14 @@ module Chat
user_id: user.id,
chat_channel_id: channel.id,
muted: false,
- following: false,
+ following: true,
notification_level: always_level,
created_at: Time.zone.now,
updated_at: Time.zone.now,
}
end
- ::Chat::UserChatChannelMembership.upsert_all(
+ ::Chat::UserChatChannelMembership.insert_all(
memberships,
unique_by: %i[user_id chat_channel_id],
)
diff --git a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb
index 8c3ad1dc36d..1ba68b2e569 100644
--- a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb
+++ b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb
@@ -129,6 +129,16 @@ RSpec.describe Chat::AddUsersToChannel do
it "only notifies the newly added users" do
expect(result.added_user_ids).to eq users.last(2).map(&:id)
end
+
+ it "respects membership settings for existing users" do
+ membership = channel.user_chat_channel_memberships.find_by(user: users.first)
+ membership.update!(
+ muted: true,
+ notification_level: ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:mention],
+ )
+
+ expect { result }.not_to change { membership.reload.attributes }
+ end
end
end
diff --git a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb
index c174b4e0022..ab613c40897 100644
--- a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb
+++ b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb
@@ -58,14 +58,14 @@ RSpec.describe Chat::CreateDirectMessageChannel do
)
end
- it "makes all target users a member of the channel and updates all users to following" do
+ it "makes all target users a member of the channel and sets new memberships to following" do
expect { result }.to change { Chat::UserChatChannelMembership.count }.by(3)
expect(result.channel.user_chat_channel_memberships.pluck(:user_id)).to match_array(
[current_user.id, user_1.id, user_2.id],
)
result.channel.user_chat_channel_memberships.each do |membership|
expect(membership).to have_attributes(
- following: false,
+ following: true,
muted: false,
notification_level: "always",
)
@@ -141,12 +141,25 @@ RSpec.describe Chat::CreateDirectMessageChannel do
context "when the channel has one user and no name" do
let(:target_usernames) { [user_1.username] }
+ let(:existing_channel) { described_class.call(params:, **dependencies).channel }
it "reuses the existing channel" do
- existing_channel = described_class.call(params:, **dependencies).channel
-
expect(result.channel.id).to eq(existing_channel.id)
end
+
+ it "respects membership settings for existing users" do
+ mention_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:mention]
+
+ membership =
+ Chat::UserChatChannelMembership.find_by(
+ user_id: user_1.id,
+ chat_channel_id: existing_channel.id,
+ )
+
+ membership.update!(muted: true, notification_level: mention_level)
+
+ expect { result }.not_to change { membership.reload.attributes }
+ end
end
context "when theres also a group channel with same users" do