From becd40f0cca516b8003b8a285d2e6f42df9bb719 Mon Sep 17 00:00:00 2001 From: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Date: Wed, 10 Jul 2024 19:24:10 -0300 Subject: [PATCH] feat: add rooms bridge method to remove users from a room (#776) --- src/definition/accessors/IModifyDeleter.ts | 2 ++ src/server/accessors/ModifyDeleter.ts | 15 +++++++++++++++ src/server/bridges/RoomBridge.ts | 8 ++++++++ tests/test-data/bridges/roomBridge.ts | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/src/definition/accessors/IModifyDeleter.ts b/src/definition/accessors/IModifyDeleter.ts index 01b4c2a78..7d1103ba1 100644 --- a/src/definition/accessors/IModifyDeleter.ts +++ b/src/definition/accessors/IModifyDeleter.ts @@ -7,4 +7,6 @@ export interface IModifyDeleter { deleteUsers(appId: Exclude, userType: UserType.APP | UserType.BOT): Promise; deleteMessage(message: IMessage, user: IUser): Promise; + + removeUsersFromRoom(roomId: string, usernames: Array): Promise; } diff --git a/src/server/accessors/ModifyDeleter.ts b/src/server/accessors/ModifyDeleter.ts index 8c4b441a8..81c4f0fa1 100644 --- a/src/server/accessors/ModifyDeleter.ts +++ b/src/server/accessors/ModifyDeleter.ts @@ -17,4 +17,19 @@ export class ModifyDeleter implements IModifyDeleter { public async deleteMessage(message: IMessage, user: IUser): Promise { return this.bridges.getMessageBridge().doDelete(message, user, this.appId); } + + /** + * Removes `usernames` from the room's member list + * + * For performance reasons, it is only possible to remove 50 users in one + * call to this method. Removing users is an expensive operation due to the + * amount of entity relationships that need to be modified. + */ + public async removeUsersFromRoom(roomId: string, usernames: Array) { + if (usernames.length > 50) { + throw new Error('A maximum of 50 members can be removed in a single call'); + } + + return this.bridges.getRoomBridge().doRemoveUsers(roomId, usernames, this.appId); + } } diff --git a/src/server/bridges/RoomBridge.ts b/src/server/bridges/RoomBridge.ts index 677620283..b5d51c473 100644 --- a/src/server/bridges/RoomBridge.ts +++ b/src/server/bridges/RoomBridge.ts @@ -91,6 +91,12 @@ export abstract class RoomBridge extends BaseBridge { } } + public async doRemoveUsers(roomId: string, usernames: Array, appId: string): Promise { + if (this.hasWritePermission(appId)) { + return this.removeUsers(roomId, usernames, appId); + } + } + protected abstract create(room: IRoom, members: Array, appId: string): Promise; protected abstract getById(roomId: string, appId: string): Promise; @@ -123,6 +129,8 @@ export abstract class RoomBridge extends BaseBridge { protected abstract getLeaders(roomId: string, appId: string): Promise>; + protected abstract removeUsers(roomId: string, usernames: Array, appId: string): Promise; + private hasWritePermission(appId: string): boolean { if (AppPermissionManager.hasPermission(appId, AppPermissions.room.write)) { return true; diff --git a/tests/test-data/bridges/roomBridge.ts b/tests/test-data/bridges/roomBridge.ts index c25edbb4f..b2c8f5031 100644 --- a/tests/test-data/bridges/roomBridge.ts +++ b/tests/test-data/bridges/roomBridge.ts @@ -55,4 +55,8 @@ export class TestsRoomBridge extends RoomBridge { public getOwners(roomId: string, appId: string): Promise> { throw new Error('Method not implemented.'); } + + public removeUsers(roomId: string, usernames: string[], appId: string): Promise { + throw new Error('Method not implemented'); + } }