From c41b29e2bd70fd6fee598bba107fe454db072c79 Mon Sep 17 00:00:00 2001 From: Darshil Patel <55157259+Darshilp326@users.noreply.github.com> Date: Thu, 25 Jan 2024 01:40:20 +0530 Subject: [PATCH] fix: Missing error message when user limit exceeds in direct message (#20019) Co-authored-by: dougfabris --- .../app/lib/server/functions/createDirectRoom.ts | 12 ++++++++++-- .../client/sidebar/header/CreateDirectMessage.tsx | 11 +++++++++-- .../packages/rocketchat-i18n/i18n/en.i18n.json | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/lib/server/functions/createDirectRoom.ts b/apps/meteor/app/lib/server/functions/createDirectRoom.ts index d76fd4b34507..b8383875444f 100644 --- a/apps/meteor/app/lib/server/functions/createDirectRoom.ts +++ b/apps/meteor/app/lib/server/functions/createDirectRoom.ts @@ -48,9 +48,17 @@ export async function createDirectRoom( subscriptionExtra?: ISubscriptionExtraData; }, ): Promise { - if (members.length > (settings.get('DirectMesssage_maxUsers') || 1)) { - throw new Error('error-direct-message-max-user-exceeded'); + const maxUsers = settings.get('DirectMesssage_maxUsers') || 1; + if (members.length > maxUsers) { + throw new Meteor.Error( + 'error-direct-message-max-user-exceeded', + `You cannot add more than ${maxUsers} users, including yourself to a direct message`, + { + method: 'createDirectRoom', + }, + ); } + await callbacks.run('beforeCreateDirectRoom', members); const membersUsernames: string[] = members diff --git a/apps/meteor/client/sidebar/header/CreateDirectMessage.tsx b/apps/meteor/client/sidebar/header/CreateDirectMessage.tsx index 853de76b665a..626d1202a8e0 100644 --- a/apps/meteor/client/sidebar/header/CreateDirectMessage.tsx +++ b/apps/meteor/client/sidebar/header/CreateDirectMessage.tsx @@ -1,7 +1,7 @@ import type { IUser } from '@rocket.chat/core-typings'; import { Box, Modal, Button, FieldGroup, Field, FieldRow, FieldLabel, FieldError } from '@rocket.chat/fuselage'; import { useUniqueId } from '@rocket.chat/fuselage-hooks'; -import { useTranslation, useEndpoint, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; +import { useTranslation, useEndpoint, useToastMessageDispatch, useSetting } from '@rocket.chat/ui-contexts'; import { useMutation } from '@tanstack/react-query'; import React, { memo } from 'react'; import { useForm, Controller } from 'react-hook-form'; @@ -11,6 +11,7 @@ import { goToRoomById } from '../../lib/utils/goToRoomById'; const CreateDirectMessage = ({ onClose }: { onClose: () => void }) => { const t = useTranslation(); + const directMaxUsers = useSetting('DirectMesssage_maxUsers') || 1; const membersFieldId = useUniqueId(); const dispatchToastMessage = useToastMessageDispatch(); @@ -55,7 +56,13 @@ const CreateDirectMessage = ({ onClose }: { onClose: () => void }) => { + users.length + 1 > directMaxUsers + ? t('error-direct-message-max-user-exceeded', { maxUsers: directMaxUsers }) + : undefined, + }} control={control} render={({ field: { name, onChange, value, onBlur } }) => (