FEATURE: introduces chat_max_direct_message_users setting (#18997)

This setting limits the number of users in a direct message. 0 means you can only create a direct message with yourself.

Co-authored-by: David McClure <dave@xerotrope.org>
This commit is contained in:
Joffrey JAFFEUX
2022-11-15 10:40:28 +01:00
committed by GitHub
parent 99cdb5a5a7
commit 59cb0b656c
4 changed files with 80 additions and 0 deletions

View File

@ -11,6 +11,7 @@ module Chat::DirectMessageChannelCreator
if direct_message
chat_channel = ChatChannel.find_by!(chatable: direct_message)
else
enforce_max_direct_message_users!(acting_user, target_users)
ensure_actor_can_communicate!(acting_user, target_users)
direct_message = DirectMessage.create!(user_ids: target_users.map(&:id))
chat_channel = direct_message.create_chat_channel!
@ -24,6 +25,20 @@ module Chat::DirectMessageChannelCreator
private
def self.enforce_max_direct_message_users!(acting_user, target_users)
# We never want to prevent the actor from communicating with themself.
target_users = target_users.reject { |user| user.id == acting_user.id }
if !acting_user.staff? && target_users.size > SiteSetting.chat_max_direct_message_users
raise NotAllowed.new(
I18n.t(
"chat.errors.over_chat_max_direct_message_users",
count: SiteSetting.chat_max_direct_message_users + 1, # +1 for the acting_user
),
)
end
end
def self.update_memberships(acting_user, target_users, chat_channel_id)
sql_params = {
acting_user_id: acting_user.id,