Skip to content

Commit

Permalink
fix: Missing error message when user limit exceeds in direct message (#…
Browse files Browse the repository at this point in the history
…20019)

Co-authored-by: dougfabris <[email protected]>
  • Loading branch information
Darshilp326 and dougfabris authored Jan 24, 2024
1 parent 46c96d3 commit c41b29e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 10 additions & 2 deletions apps/meteor/app/lib/server/functions/createDirectRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ export async function createDirectRoom(
subscriptionExtra?: ISubscriptionExtraData;
},
): Promise<ICreatedRoom> {
if (members.length > (settings.get<number>('DirectMesssage_maxUsers') || 1)) {
throw new Error('error-direct-message-max-user-exceeded');
const maxUsers = settings.get<number>('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
Expand Down
11 changes: 9 additions & 2 deletions apps/meteor/client/sidebar/header/CreateDirectMessage.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,6 +11,7 @@ import { goToRoomById } from '../../lib/utils/goToRoomById';

const CreateDirectMessage = ({ onClose }: { onClose: () => void }) => {
const t = useTranslation();
const directMaxUsers = useSetting<number>('DirectMesssage_maxUsers') || 1;
const membersFieldId = useUniqueId();
const dispatchToastMessage = useToastMessageDispatch();

Expand Down Expand Up @@ -55,7 +56,13 @@ const CreateDirectMessage = ({ onClose }: { onClose: () => void }) => {
<FieldRow>
<Controller
name='users'
rules={{ required: t('error-the-field-is-required', { field: t('Members') }) }}
rules={{
required: t('error-the-field-is-required', { field: t('Members') }),
validate: (users) =>
users.length + 1 > directMaxUsers
? t('error-direct-message-max-user-exceeded', { maxUsers: directMaxUsers })
: undefined,
}}
control={control}
render={({ field: { name, onChange, value, onBlur } }) => (
<UserAutoCompleteMultipleFederated
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,7 @@
"Direct": "Direct",
"Direction": "Direction",
"Livechat_Facebook_API_Secret": "OmniChannel API Secret",
"DirectMesssage_maxUsers": "Max users in direct messages",
"Direct_Message": "Direct message",
"Livechat_Facebook_Enabled": "Facebook integration enabled",
"Direct_message_creation_description": "You are about to create a chat with multiple users. Add the ones you would like to talk, everyone in the same place, using direct messages.",
Expand Down Expand Up @@ -1989,6 +1990,7 @@
"error-delete-protected-role": "Cannot delete a protected role",
"error-department-not-found": "Department not found",
"error-department-removal-disabled": "Department removal is disabled by administration, please contact your administrator",
"error-direct-message-max-user-exceeded": "You cannot add more than {{maxUsers}} users, including yourself to a direct message",
"error-direct-message-file-upload-not-allowed": "File sharing not allowed in direct messages",
"error-duplicate-channel-name": "A channel with name '{{channel_name}}' exists",
"error-duplicate-priority-name": "A priority with the same name already exists",
Expand Down

0 comments on commit c41b29e

Please sign in to comment.