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: add rooms bridge method to remove users from a room #776

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 entities that need to be modified.
d-gubert marked this conversation as resolved.
Show resolved Hide resolved
*/
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');
}
}
Loading