From 4e80c9eb13ddb3d3661d919dbfadc44a71e2215d Mon Sep 17 00:00:00 2001 From: David Battersby Date: Mon, 3 Jun 2024 12:11:49 +0400 Subject: [PATCH] FIX: chat direct message group user limit is off by 1 (#27014) This change allows the correct number of members to be added when creating a group direct message, based on the site setting chat_max_direct_message_users. Previously we counted the current user within the max user limit and therefore the count was off by 1. --- .../api/channels_memberships_controller.rb | 3 +++ .../app/services/chat/add_users_to_channel.rb | 21 +++++++------------ .../chat/message-creator/add-members.gjs | 14 ++++++------- .../chat/message-creator/new-group.gjs | 11 +++++----- .../chat/add_users_to_channel_spec.rb | 14 +++++++------ .../spec/system/chat_message_creator_spec.rb | 7 ++++--- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb b/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb index 14ece64edcf..0a805df1a53 100644 --- a/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb +++ b/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb @@ -36,6 +36,9 @@ class Chat::Api::ChannelsMembershipsController < Chat::Api::ChannelsController on_failed_policy(:can_add_users_to_channel) do render_json_error(I18n.t("chat.errors.users_cant_be_added_to_channel")) end + on_failed_policy(:satisfies_dms_max_users_limit) do |policy| + render_json_dump({ error: policy.reason }, status: 400) + end on_failed_contract do |contract| render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400) end 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 3785d1e62ff..b6ad6e4719b 100644 --- a/plugins/chat/app/services/chat/add_users_to_channel.rb +++ b/plugins/chat/app/services/chat/add_users_to_channel.rb @@ -25,10 +25,11 @@ module Chat contract model :channel policy :can_add_users_to_channel - model :users, optional: true + model :target_users, optional: true + policy :satisfies_dms_max_users_limit, + class_name: Chat::DirectMessageChannel::MaxUsersExcessPolicy transaction do - step :validate_user_count step :upsert_memberships step :recompute_users_count step :notice_channel @@ -56,7 +57,7 @@ module Chat channel.direct_message_channel? && channel.chatable.group end - def fetch_users(contract:, channel:) + def fetch_target_users(contract:, channel:) ::Chat::UsersFromUsernamesAndGroupsQuery.call( usernames: contract.usernames, groups: contract.groups, @@ -68,17 +69,11 @@ module Chat ::Chat::Channel.includes(:chatable).find_by(id: contract.channel_id) end - def validate_user_count(channel:, users:) - if channel.user_count + users.length > SiteSetting.chat_max_direct_message_users - fail!("should have less than #{SiteSetting.chat_max_direct_message_users} elements") - end - end - - def upsert_memberships(channel:, users:) + def upsert_memberships(channel:, target_users:) always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always] memberships = - users.map do |user| + target_users.map do |user| { user_id: user.id, chat_channel_id: channel.id, @@ -128,8 +123,8 @@ module Chat ) end - def notice_channel(guardian:, channel:, users:) - added_users = users.select { |u| context.added_user_ids.include?(u.id) } + def notice_channel(guardian:, channel:, target_users:) + added_users = target_users.select { |u| context.added_user_ids.include?(u.id) } return if added_users.blank? diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/add-members.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/add-members.gjs index 2448d04261c..2512196516d 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/add-members.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/chat/message-creator/add-members.gjs @@ -27,6 +27,10 @@ export default class AddMembers extends Component { return userCount + (this.args.channel?.membershipsCount ?? 0); } + get maxMembers() { + return this.siteSettings.chat_max_direct_message_users; + } + @action async saveGroupMembers() { try { @@ -60,10 +64,7 @@ export default class AddMembers extends Component {