chore: sync latest Apps-engine alpha (#33649)

pull/33605/head^2
Douglas Gubert 2 years ago committed by GitHub
parent 916e205011
commit 94a1119bc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 20
      packages/apps-engine/src/definition/accessors/IRoomRead.ts
  2. 22
      packages/apps-engine/src/server/accessors/RoomRead.ts
  3. 16
      packages/apps-engine/src/server/bridges/RoomBridge.ts
  4. 4
      packages/apps-engine/src/server/bridges/UserBridge.ts
  5. 18
      packages/apps-engine/src/server/managers/AppListenerManager.ts
  6. 21
      packages/apps-engine/tests/server/accessors/RoomRead.spec.ts
  7. 8
      packages/apps-engine/tests/test-data/bridges/roomBridge.ts
  8. 2
      packages/apps-engine/tests/test-data/bridges/userBridge.ts

@ -90,4 +90,24 @@ export interface IRoomRead {
* @returns a list of the users with the leader role in the room
*/
getLeaders(roomId: string): Promise<Array<IUser>>;
/**
* Retrieves an array of unread messages for a specific user in a specific room.
*
* @param roomId The unique identifier of the room from which to retrieve unread messages.
* @param uid The unique identifier of the user for whom to retrieve unread 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 'asc' for ascending order or 'desc' for descending order.
* @returns A Promise that resolves to an array of IMessage objects representing the unread messages for the specified user in the specified room.
*/
getUnreadByUser(roomId: string, uid: string, options?: Partial<GetMessagesOptions>): Promise<IMessageRaw[]>;
/**
* Gets the user's unread messages count in a room.
* @param roomId room's id
* @param uid user's id
*/
getUserUnreadMessageCount(roomId: string, uid: string): Promise<number>;
}

@ -58,6 +58,28 @@ export class RoomRead implements IRoomRead {
return this.roomBridge.doGetLeaders(roomId, this.appId);
}
public async getUnreadByUser(roomId: string, uid: string, options: Partial<GetMessagesOptions> = {}): Promise<IMessageRaw[]> {
const { limit = 100, sort = { createdAt: 'asc' }, skip = 0 } = options;
if (typeof roomId !== 'string' || roomId.trim().length === 0) {
throw new Error('Invalid roomId: must be a non-empty string');
}
if (!Number.isFinite(limit) || limit <= 0 || limit > 100) {
throw new Error(`Invalid limit provided. Expected number between 1 and 100, got ${limit}`);
}
this.validateSort(sort);
const completeOptions: GetMessagesOptions = { limit, sort, skip };
return this.roomBridge.doGetUnreadByUser(roomId, uid, completeOptions, this.appId);
}
public getUserUnreadMessageCount(roomId: string, uid: string): Promise<number> {
return this.roomBridge.doGetUserUnreadMessageCount(roomId, uid, this.appId);
}
// If there are any invalid fields or values, throw
private validateSort(sort: Record<string, unknown>) {
Object.entries(sort).forEach(([key, value]) => {

@ -111,6 +111,18 @@ export abstract class RoomBridge extends BaseBridge {
}
}
public async doGetUnreadByUser(roomId: string, uid: string, options: GetMessagesOptions, appId: string): Promise<IMessageRaw[]> {
if (this.hasReadPermission(appId)) {
return this.getUnreadByUser(roomId, uid, options, appId);
}
}
public async doGetUserUnreadMessageCount(roomId: string, uid: string, appId: string): Promise<number> {
if (this.hasReadPermission(appId)) {
return this.getUserUnreadMessageCount(roomId, uid, appId);
}
}
protected abstract create(room: IRoom, members: Array<string>, appId: string): Promise<string>;
protected abstract getById(roomId: string, appId: string): Promise<IRoom>;
@ -147,6 +159,10 @@ export abstract class RoomBridge extends BaseBridge {
protected abstract removeUsers(roomId: string, usernames: Array<string>, appId: string): Promise<void>;
protected abstract getUnreadByUser(roomId: string, uid: string, options: GetMessagesOptions, appId: string): Promise<IMessageRaw[]>;
protected abstract getUserUnreadMessageCount(roomId: string, uid: string, appId: string): Promise<number>;
private hasWritePermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions.room.write)) {
return true;

@ -41,7 +41,7 @@ export abstract class UserBridge extends BaseBridge {
public async doGetUserUnreadMessageCount(uid: string, appId: string): Promise<number> {
if (this.hasReadPermission(appId)) {
return this.getUserUnreadMessageCount(uid);
return this.getUserUnreadMessageCount(uid, appId);
}
}
@ -65,7 +65,7 @@ export abstract class UserBridge extends BaseBridge {
protected abstract getActiveUserCount(): Promise<number>;
protected abstract getUserUnreadMessageCount(uid: string): Promise<number>;
protected abstract getUserUnreadMessageCount(uid: string, appId: string): Promise<number>;
/**
* Creates a user.

@ -22,8 +22,7 @@ import type {
IUIKitIncomingInteractionMessageContainer,
IUIKitIncomingInteractionModalContainer,
} from '../../definition/uikit/UIKitIncomingInteractionContainer';
import type { IUIKitLivechatIncomingInteraction } from '../../definition/uikit/livechat';
import { UIKitLivechatBlockInteractionContext } from '../../definition/uikit/livechat';
import type { IUIKitLivechatBlockIncomingInteraction, IUIKitLivechatIncomingInteraction } from '../../definition/uikit/livechat';
import type { IFileUploadContext } from '../../definition/uploads/IFileUploadContext';
import type { IUser, IUserContext, IUserStatusContext, IUserUpdateContext } from '../../definition/users';
import type { AppManager } from '../AppManager';
@ -1016,14 +1015,17 @@ export class AppListenerManager {
const app = this.manager.getOneById(appId);
const interactionContext = ((interactionType: UIKitIncomingInteractionType, interactionData: IUIKitLivechatIncomingInteraction) => {
const { actionId, message, visitor, room, triggerId, container } = interactionData;
const interactionData = ((
interactionType: UIKitIncomingInteractionType,
interaction: IUIKitLivechatIncomingInteraction,
): IUIKitLivechatBlockIncomingInteraction => {
const { actionId, message, visitor, room, triggerId, container } = interaction;
switch (interactionType) {
case UIKitIncomingInteractionType.BLOCK: {
const { value, blockId } = interactionData.payload as { value: string; blockId: string };
const { value, blockId } = interaction.payload as { value: string; blockId: string };
return new UIKitLivechatBlockInteractionContext({
return {
appId,
actionId,
blockId,
@ -1033,12 +1035,12 @@ export class AppListenerManager {
value,
message,
container: container as IUIKitIncomingInteractionModalContainer | IUIKitIncomingInteractionMessageContainer,
});
};
}
}
})(type, data);
return app.call(method, interactionContext);
return app.call(method, interactionData);
}
// Livechat

@ -14,6 +14,10 @@ export class RoomReadAccessorTestFixture {
private messages: IMessageRaw[];
private unreadRoomId: string;
private unreadUserId: string;
private mockRoomBridgeWithRoom: RoomBridge;
@SetupFixture
@ -21,10 +25,16 @@ export class RoomReadAccessorTestFixture {
this.room = TestData.getRoom();
this.user = TestData.getUser();
this.messages = ['507f1f77bcf86cd799439011', '507f191e810c19729de860ea'].map((id) => TestData.getMessageRaw(id));
this.unreadRoomId = this.messages[0].roomId;
this.unreadUserId = this.messages[0].sender._id;
const theRoom = this.room;
const theUser = this.user;
const theMessages = this.messages;
const theUnreadMsg = this.messages;
const { unreadRoomId } = this;
const { unreadUserId } = this;
this.mockRoomBridgeWithRoom = {
doGetById(id, appId): Promise<IRoom> {
return Promise.resolve(theRoom);
@ -47,6 +57,12 @@ export class RoomReadAccessorTestFixture {
doGetMessages(roomId, options, appId): Promise<IMessageRaw[]> {
return Promise.resolve(theMessages);
},
doGetUnreadByUser(roomId, uid, options, appId): Promise<IMessageRaw[]> {
if (roomId === unreadRoomId && uid === unreadUserId) {
return Promise.resolve(theUnreadMsg);
}
return Promise.resolve([]);
},
} as RoomBridge;
}
@ -68,6 +84,11 @@ export class RoomReadAccessorTestFixture {
Expect(await rr.getDirectByUsernames([this.user.username])).toBe(this.room);
Expect(await rr.getMessages('testing')).toBeDefined();
Expect(await rr.getMessages('testing')).toBe(this.messages);
Expect(await rr.getUnreadByUser(this.unreadRoomId, this.unreadUserId)).toBeDefined();
Expect(await rr.getUnreadByUser(this.unreadRoomId, this.unreadUserId)).toEqual(this.messages);
Expect(await rr.getUnreadByUser('fake', 'fake')).toBeDefined();
Expect(await rr.getUnreadByUser('fake', 'fake')).toEqual([]);
}
@AsyncTest()

@ -64,4 +64,12 @@ export class TestsRoomBridge extends RoomBridge {
public removeUsers(roomId: string, usernames: string[], appId: string): Promise<void> {
throw new Error('Method not implemented');
}
public getUnreadByUser(roomId: string, uid: string, options: GetMessagesOptions, appId: string): Promise<IMessageRaw[]> {
throw new Error('Method not implemented.');
}
protected getUserUnreadMessageCount(roomId: string, uid: string, appId: string): Promise<number> {
throw new Error('Method not implemented.');
}
}

@ -34,7 +34,7 @@ export class TestsUserBridge extends UserBridge {
throw new Error('Method not implemented');
}
protected getUserUnreadMessageCount(uid: string): Promise<number> {
protected getUserUnreadMessageCount(uid: string, appId: string): Promise<number> {
throw new Error('Method not implemented.');
}

Loading…
Cancel
Save