Skip to content

Commit

Permalink
feat: add rooms bridge method to remove users from a room (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh authored Jul 10, 2024
1 parent dfcbd7a commit becd40f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/definition/accessors/IModifyDeleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export interface IModifyDeleter {
deleteUsers(appId: Exclude<IUser['appId'], undefined>, userType: UserType.APP | UserType.BOT): Promise<boolean>;

deleteMessage(message: IMessage, user: IUser): Promise<void>;

removeUsersFromRoom(roomId: string, usernames: Array<string>): Promise<void>;
}
15 changes: 15 additions & 0 deletions src/server/accessors/ModifyDeleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ export class ModifyDeleter implements IModifyDeleter {
public async deleteMessage(message: IMessage, user: IUser): Promise<void> {
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<string>) {
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);
}
}
8 changes: 8 additions & 0 deletions src/server/bridges/RoomBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export abstract class RoomBridge extends BaseBridge {
}
}

public async doRemoveUsers(roomId: string, usernames: Array<string>, appId: string): Promise<void> {
if (this.hasWritePermission(appId)) {
return this.removeUsers(roomId, usernames, appId);
}
}

protected abstract create(room: IRoom, members: Array<string>, appId: string): Promise<string>;

protected abstract getById(roomId: string, appId: string): Promise<IRoom>;
Expand Down Expand Up @@ -123,6 +129,8 @@ export abstract class RoomBridge extends BaseBridge {

protected abstract getLeaders(roomId: string, appId: string): Promise<Array<IUser>>;

protected abstract removeUsers(roomId: string, usernames: Array<string>, appId: string): Promise<void>;

private hasWritePermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions.room.write)) {
return true;
Expand Down
4 changes: 4 additions & 0 deletions tests/test-data/bridges/roomBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ export class TestsRoomBridge extends RoomBridge {
public getOwners(roomId: string, appId: string): Promise<Array<IUser>> {
throw new Error('Method not implemented.');
}

public removeUsers(roomId: string, usernames: string[], appId: string): Promise<void> {
throw new Error('Method not implemented');
}
}

0 comments on commit becd40f

Please sign in to comment.