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.rbindex 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 Chatclass_name: Chat::DirectMessageChannel::Policy::MaxUsersExcesstransaction do- step :upsert_memberships+ step :create_membershipsstep :recompute_users_countstep :notice_channelend@@ -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 Chatendcontext[: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.rbindex 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 Chatmodel :direct_message, :fetch_or_create_direct_messagemodel :channel, :fetch_or_create_channelstep :set_optional_params- step :update_memberships+ step :create_membershipsstep :recompute_users_countprivate@@ -101,7 +101,7 @@ module Chatchannel.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 Chatuser_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.rbindex 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 doit "only notifies the newly added users" doexpect(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 }+ endendenddiff --git a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rbindex 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" doexpect { 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 docontext "when the channel has one user and no name" dolet(: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 }+ endendcontext "when theres also a group channel with same users" do