Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
feat: Implement the Apps Engine Room message read bridge (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnouv committed Apr 15, 2024
1 parent 212eec7 commit cfc0a0b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/definition/accessors/IRoomRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ export interface IRoomRead {
getCreatorUserByName(name: string): Promise<IUser | undefined>;

/**
* Gets an iterator for all of the messages in the provided room.
* Retrieves an array of messages from the specified room.
*
* @param roomId the room's id
* @returns an iterator for messages
* @param roomId The unique identifier of the room from which to retrieve messages.
* @param options Optional parameters for retrieving messages:
* - limit: The maximum number of messages to retrieve. If more than 100 is passed, it defaults to 100.
* - skip: The number of messages to skip (for pagination).
* - sort: An object defining the sorting order of the messages. Each key is a field to sort by, and the value is either 1 for ascending order or -1 for descending order.
* @returns A Promise that resolves to an array of IMessage objects representing the messages in the room.
*/
getMessages(roomId: string): Promise<IterableIterator<IMessage>>;
getMessages(
roomId: string,
options?: Partial<{
limit: number;
skip: number;
sort: Record<string, 1 | -1>;
}>,
): Promise<IMessage[]>;

/**
* Gets an iterator for all of the users in the provided room.
Expand Down
11 changes: 9 additions & 2 deletions src/server/accessors/RoomRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ export class RoomRead implements IRoomRead {
return this.roomBridge.doGetCreatorByName(name, this.appId);
}

public getMessages(roomId: string): Promise<IterableIterator<IMessage>> {
throw new Error('Method not implemented.');
public getMessages(
roomId: string,
options?: Partial<{
limit: number;
skip: number;
sort: Record<string, 1 | -1>;
}>,
): Promise<IMessage[]> {
return this.roomBridge.doGetMessages(roomId, { limit: 100, ...options }, this.appId);
}

public getMembers(roomId: string): Promise<Array<IUser>> {
Expand Down
24 changes: 24 additions & 0 deletions src/server/bridges/RoomBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ export abstract class RoomBridge extends BaseBridge {
}
}

public async doGetMessages(
roomId: string,
options: {
limit: number;
skip?: number;
sort?: Record<string, 1 | -1>;
},
appId: string,
): Promise<IMessage[]> {
if (this.hasReadPermission(appId)) {
return this.getMessages(roomId, options, 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 +137,16 @@ export abstract class RoomBridge extends BaseBridge {

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

protected abstract getMessages(
roomId: string,
options: {
limit: number;
skip?: number;
sort?: Record<string, 1 | -1>;
},
appId: string,
): Promise<IMessage[]>;

private hasWritePermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions.room.write)) {
return true;
Expand Down
11 changes: 10 additions & 1 deletion tests/server/accessors/RoomRead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import type { IUser } from '../../../src/definition/users';
import { RoomRead } from '../../../src/server/accessors';
import type { RoomBridge } from '../../../src/server/bridges';
import { TestData } from '../../test-data/utilities';
import type { IMessage } from '../../../src/definition/messages';

export class RoomReadAccessorTestFixture {
private room: IRoom;

private user: IUser;

private messages: IMessage[];

private mockRoomBridgeWithRoom: RoomBridge;

@SetupFixture
public setupFixture() {
this.room = TestData.getRoom();
this.user = TestData.getUser();
this.messages = ['507f1f77bcf86cd799439011', '507f191e810c19729de860ea'].map((id) => TestData.getMessage(id));

const theRoom = this.room;
const theUser = this.user;
const theMessages = this.messages;
this.mockRoomBridgeWithRoom = {
doGetById(id, appId): Promise<IRoom> {
return Promise.resolve(theRoom);
Expand All @@ -39,6 +44,9 @@ export class RoomReadAccessorTestFixture {
doGetMembers(name, appId): Promise<Array<IUser>> {
return Promise.resolve([theUser]);
},
doGetMessages(roomId, appId, options): Promise<IMessage[]> {
return Promise.resolve(theMessages);
},
} as RoomBridge;
}

Expand All @@ -58,14 +66,15 @@ export class RoomReadAccessorTestFixture {
Expect(await rr.getCreatorUserByName('testing')).toBe(this.user);
Expect(await rr.getDirectByUsernames([this.user.username])).toBeDefined();
Expect(await rr.getDirectByUsernames([this.user.username])).toBe(this.room);
Expect(await rr.getMessages('testing')).toBeDefined();
Expect(await rr.getMessages('testing')).toBe(this.messages);
}

@AsyncTest()
public async useTheIterators() {
Expect(() => new RoomRead(this.mockRoomBridgeWithRoom, 'testing-app')).not.toThrow();

const rr = new RoomRead(this.mockRoomBridgeWithRoom, 'testing-app');
await Expect(() => rr.getMessages('faker')).toThrowErrorAsync(Error, 'Method not implemented.');

Expect(await rr.getMembers('testing')).toBeDefined();
Expect((await rr.getMembers('testing')) as Array<IUser>).not.toBeEmpty();
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 @@ -32,6 +32,10 @@ export class TestsRoomBridge extends RoomBridge {
throw new Error('Method not implemented.');
}

public getMessages(roomId: string, options: { limit: number; skip?: number; sort?: Record<string, 1 | -1> }, appId: string): Promise<IMessage[]> {
throw new Error('Method not implemented.');
}

public update(room: IRoom, members: Array<string>, appId: string): Promise<void> {
throw new Error('Method not implemented.');
}
Expand Down

0 comments on commit cfc0a0b

Please sign in to comment.