Skip to content

Commit

Permalink
refactor: remove Meteor.call for addAllUserToRoom method (server) (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored Jan 20, 2025
1 parent 1fb9f86 commit d949016
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 50 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }),
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });

Expand Down
106 changes: 58 additions & 48 deletions apps/meteor/server/methods/addAllUserToRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,72 @@ declare module '@rocket.chat/ddp-client' {
}
}

Meteor.methods<ServerMethods>({
async addAllUserToRoom(rid, activeUsersOnly = false) {
check(rid, String);
check(activeUsersOnly, Boolean);
export const addAllUserToRoomFn = async (userId: string, rid: IRoom['_id'], activeUsersOnly = false): Promise<true> => {
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<number>('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<number>('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<ServerMethods>({
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);
},
});

0 comments on commit d949016

Please sign in to comment.