Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replicate IPreRoomUserLeave and IPostRoomUserLeave event in meteor method and add removedBy to IRoomUserLeaveContext #32724

Merged
merged 9 commits into from
Jul 16, 2024
5 changes: 5 additions & 0 deletions .changeset/sour-forks-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": minor
---

Extended apps-engine events for users leaving a room to also fire when being removed by another user. Also added the triggering user's information to the event's context payload.
3 changes: 2 additions & 1 deletion apps/meteor/app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ export class AppListenerBridge {
};
case AppInterface.IPreRoomUserLeave:
case AppInterface.IPostRoomUserLeave:
const [leavingUser] = payload;
const [leavingUser, removedBy] = payload;
return {
room: rm,
leavingUser: this.orch.getConverters().get('users').convertToApp(leavingUser),
removedBy: this.orch.getConverters().get('users').convertToApp(removedBy),
};
default:
return rm;
Expand Down
10 changes: 3 additions & 7 deletions apps/meteor/app/lib/server/functions/removeUserFromRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import { beforeLeaveRoomCallback } from '../../../../lib/callbacks/beforeLeaveRo
import { settings } from '../../../settings/server';
import { notifyOnRoomChangedById } from '../lib/notifyListener';

export const removeUserFromRoom = async function (
rid: string,
user: IUser,
options?: { byUser: Pick<IUser, '_id' | 'username'> },
): Promise<void> {
export const removeUserFromRoom = async function (rid: string, user: IUser, options?: { byUser: IUser }): Promise<void> {
const room = await Rooms.findOneById(rid);

if (!room) {
return;
}

try {
await Apps.self?.triggerEvent(AppEvents.IPreRoomUserLeave, room, user);
await Apps.self?.triggerEvent(AppEvents.IPreRoomUserLeave, room, user, options?.byUser);
gabriellsh marked this conversation as resolved.
Show resolved Hide resolved
} catch (error: any) {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
Expand Down Expand Up @@ -75,5 +71,5 @@ export const removeUserFromRoom = async function (

void notifyOnRoomChangedById(rid);

await Apps.self?.triggerEvent(AppEvents.IPostRoomUserLeave, room, user);
await Apps.self?.triggerEvent(AppEvents.IPostRoomUserLeave, room, user, options?.byUser);
};
14 changes: 14 additions & 0 deletions apps/meteor/server/methods/removeUserFromRoom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Message, Team } from '@rocket.chat/core-services';
import { Subscriptions, Rooms, Users } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
Expand Down Expand Up @@ -75,6 +77,16 @@ export const removeUserFromRoomMethod = async (fromId: string, data: { rid: stri
}
}

try {
await Apps.self?.triggerEvent(AppEvents.IPreRoomUserLeave, room, removedUser, fromUser);
} catch (error: any) {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
}

throw error;
}

await callbacks.run('beforeRemoveFromRoom', { removedUser, userWhoRemoved: fromUser }, room);

await Subscriptions.removeByRoomIdAndUserId(data.rid, removedUser._id);
Expand All @@ -99,6 +111,8 @@ export const removeUserFromRoomMethod = async (fromId: string, data: { rid: stri
void notifyOnRoomChanged(room);
});

await Apps.self?.triggerEvent(AppEvents.IPostRoomUserLeave, room, removedUser, fromUser);

return true;
};

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/services/room/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class RoomService extends ServiceClassInternal implements IRoomService {
return addUserToRoom(roomId, user, inviter, silenced);
}

async removeUserFromRoom(roomId: string, user: IUser, options?: { byUser: Pick<IUser, '_id' | 'username'> }): Promise<void> {
async removeUserFromRoom(roomId: string, user: IUser, options?: { byUser: IUser }): Promise<void> {
return removeUserFromRoom(roomId, user, options);
}

Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ export class TeamService extends ServiceClassInternal implements ITeamService {
const usersToRemove = await Users.findByIds(membersIds, {
projection: { _id: 1, username: 1 },
}).toArray();
const byUser = (await Users.findOneById(uid, { projection: { _id: 1, username: 1 } })) as Pick<IUser, '_id' | 'username'>;
const byUser = await Users.findOneById(uid);

for await (const member of members) {
if (!member.userId) {
Expand Down Expand Up @@ -802,7 +802,7 @@ export class TeamService extends ServiceClassInternal implements ITeamService {
await removeUserFromRoom(
team.roomId,
removedUser,
uid !== member.userId
uid !== member.userId && byUser
? {
byUser,
}
Expand Down
Loading