diff --git a/apps/meteor/app/api/server/v1/channels.ts b/apps/meteor/app/api/server/v1/channels.ts index a17729c47af2..4f473477f25a 100644 --- a/apps/meteor/app/api/server/v1/channels.ts +++ b/apps/meteor/app/api/server/v1/channels.ts @@ -27,6 +27,7 @@ import { Meteor } from 'meteor/meteor'; import { isTruthy } from '../../../../lib/isTruthy'; import { eraseRoom } from '../../../../server/lib/eraseRoom'; import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom'; +import { addAllUserToRoomFn } from '../../../../server/methods/addAllUserToRoom'; import { hideRoomMethod } from '../../../../server/methods/hideRoom'; import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom'; import { canAccessRoomAsync } from '../../../authorization/server'; @@ -96,7 +97,7 @@ API.v1.addRoute( const { activeUsersOnly, ...params } = this.bodyParams; const findResult = await findChannelByIdOrName({ params, userId: this.userId }); - await Meteor.callAsync('addAllUserToRoom', findResult._id, activeUsersOnly); + await addAllUserToRoomFn(this.userId, findResult._id, activeUsersOnly === 'true' || activeUsersOnly === 1); return API.v1.success({ channel: await findChannelByIdOrName({ params, userId: this.userId }), diff --git a/apps/meteor/app/api/server/v1/groups.ts b/apps/meteor/app/api/server/v1/groups.ts index 479ebdc9a42e..f33c9a6db55a 100644 --- a/apps/meteor/app/api/server/v1/groups.ts +++ b/apps/meteor/app/api/server/v1/groups.ts @@ -8,6 +8,7 @@ import type { Filter } from 'mongodb'; import { eraseRoom } from '../../../../server/lib/eraseRoom'; import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom'; +import { addAllUserToRoomFn } from '../../../../server/methods/addAllUserToRoom'; import { hideRoomMethod } from '../../../../server/methods/hideRoom'; import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom'; import { canAccessRoomAsync, roomAccessAttributes } from '../../../authorization/server'; @@ -122,7 +123,7 @@ API.v1.addRoute( userId: this.userId, }); - await Meteor.callAsync('addAllUserToRoom', findResult.rid, this.bodyParams.activeUsersOnly); + await addAllUserToRoomFn(this.userId, findResult.rid, activeUsersOnly === 'true' || activeUsersOnly === 1); const room = await Rooms.findOneById(findResult.rid, { projection: API.v1.defaultFieldsToExclude }); diff --git a/apps/meteor/server/methods/addAllUserToRoom.ts b/apps/meteor/server/methods/addAllUserToRoom.ts index 6b1b690b4bfd..530ea9cfdda2 100644 --- a/apps/meteor/server/methods/addAllUserToRoom.ts +++ b/apps/meteor/server/methods/addAllUserToRoom.ts @@ -19,62 +19,72 @@ declare module '@rocket.chat/ddp-client' { } } -Meteor.methods({ - async addAllUserToRoom(rid, activeUsersOnly = false) { - check(rid, String); - check(activeUsersOnly, Boolean); +export const addAllUserToRoomFn = async (userId: string, rid: IRoom['_id'], activeUsersOnly = false): Promise => { + check(rid, String); + check(activeUsersOnly, Boolean); - if (!this.userId || !(await hasPermissionAsync(this.userId, 'add-all-to-room'))) { - throw new Meteor.Error(403, 'Access to Method Forbidden', { - method: 'addAllToRoom', - }); - } + if (!(await hasPermissionAsync(userId, 'add-all-to-room'))) { + throw new Meteor.Error(403, 'Access to Method Forbidden', { + method: 'addAllToRoom', + }); + } - const userFilter: { - active?: boolean; - } = {}; - if (activeUsersOnly === true) { - userFilter.active = true; - } + const userFilter: { + active?: boolean; + } = {}; + if (activeUsersOnly === true) { + userFilter.active = true; + } - const users = await Users.find(userFilter).toArray(); - if (users.length > settings.get('API_User_Limit')) { - throw new Meteor.Error('error-user-limit-exceeded', 'User Limit Exceeded', { - method: 'addAllToRoom', - }); + const users = await Users.find(userFilter).toArray(); + if (users.length > settings.get('API_User_Limit')) { + throw new Meteor.Error('error-user-limit-exceeded', 'User Limit Exceeded', { + method: 'addAllToRoom', + }); + } + + const room = await Rooms.findOneById(rid); + if (!room) { + throw new Meteor.Error('error-invalid-room', 'Invalid room', { + method: 'addAllToRoom', + }); + } + + const now = new Date(); + for await (const user of users) { + const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id); + if (subscription != null) { + continue; + } + await callbacks.run('beforeJoinRoom', user, room); + const autoTranslateConfig = getSubscriptionAutotranslateDefaultConfig(user); + const { insertedId } = await Subscriptions.createWithRoomAndUser(room, user, { + ts: now, + open: true, + alert: true, + unread: 1, + userMentions: 1, + groupMentions: 0, + ...autoTranslateConfig, + ...getDefaultSubscriptionPref(user), + }); + if (insertedId) { + void notifyOnSubscriptionChangedById(insertedId, 'inserted'); } + await Message.saveSystemMessage('uj', rid, user.username || '', user, { ts: now }); + await callbacks.run('afterJoinRoom', user, room); + } + return true; +}; - const room = await Rooms.findOneById(rid); - if (!room) { - throw new Meteor.Error('error-invalid-room', 'Invalid room', { +Meteor.methods({ + async addAllUserToRoom(rid, activeUsersOnly = false) { + if (!this.userId) { + throw new Meteor.Error(403, 'Access to Method Forbidden', { method: 'addAllToRoom', }); } - const now = new Date(); - for await (const user of users) { - const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id); - if (subscription != null) { - continue; - } - await callbacks.run('beforeJoinRoom', user, room); - const autoTranslateConfig = getSubscriptionAutotranslateDefaultConfig(user); - const { insertedId } = await Subscriptions.createWithRoomAndUser(room, user, { - ts: now, - open: true, - alert: true, - unread: 1, - userMentions: 1, - groupMentions: 0, - ...autoTranslateConfig, - ...getDefaultSubscriptionPref(user), - }); - if (insertedId) { - void notifyOnSubscriptionChangedById(insertedId, 'inserted'); - } - await Message.saveSystemMessage('uj', rid, user.username || '', user, { ts: now }); - await callbacks.run('afterJoinRoom', user, room); - } - return true; + return addAllUserToRoomFn(this.userId, rid, activeUsersOnly); }, });