Skip to content

Commit

Permalink
feat: Apps-Engine method for reading and counting unread room message…
Browse files Browse the repository at this point in the history
…s for a user (#32194)
  • Loading branch information
Dnouv authored Oct 15, 2024
1 parent 08d0df2 commit dd923a8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-files-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---

Adds methods to the Apps-Engine to interact with unread messages to enhance message capabilities on Apps.
51 changes: 51 additions & 0 deletions apps/meteor/app/apps/server/bridges/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,57 @@ export class AppRoomBridge extends RoomBridge {
return users.map((user: ICoreUser) => userConverter.convertToApp(user));
}

protected async getUnreadByUser(roomId: string, uid: string, options: GetMessagesOptions, appId: string): Promise<Array<IMessageRaw>> {
this.orch.debugLog(`The App ${appId} is getting the unread messages for the user: "${uid}" in the room: "${roomId}"`);

const messageConverter = this.orch.getConverters()?.get('messages');
if (!messageConverter) {
throw new Error('Message converter not found');
}

const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, uid, { projection: { ls: 1 } });

if (!subscription) {
const errorMessage = `No subscription found for user with ID "${uid}" in room with ID "${roomId}". This means the user is not subscribed to the room.`;
this.orch.debugLog(errorMessage);
throw new Error('User not subscribed to room');
}

const lastSeen = subscription?.ls;
if (!lastSeen) {
return [];
}

const sort: Sort = options.sort?.createdAt ? { ts: options.sort.createdAt } : { ts: 1 };

const cursor = Messages.findVisibleByRoomIdBetweenTimestampsNotContainingTypes(roomId, lastSeen, new Date(), [], {
...options,
sort,
});

const messages = await cursor.toArray();
return Promise.all(messages.map((msg) => messageConverter.convertMessageRaw(msg)));
}

protected async getUserUnreadMessageCount(roomId: string, uid: string, appId: string): Promise<number> {
this.orch.debugLog(`The App ${appId} is getting the unread messages count of the room: "${roomId}" for the user: "${uid}"`);

const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, uid, { projection: { ls: 1 } });

if (!subscription) {
const errorMessage = `No subscription found for user with ID "${uid}" in room with ID "${roomId}". This means the user is not subscribed to the room.`;
this.orch.debugLog(errorMessage);
throw new Error('User not subscribed to room');
}

const lastSeen = subscription?.ls;
if (!lastSeen) {
return 0;
}

return Messages.countVisibleByRoomIdBetweenTimestampsNotContainingTypes(roomId, lastSeen, new Date(), []);
}

protected async removeUsers(roomId: string, usernames: Array<string>, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is removing users ${usernames} from room id: ${roomId}`);
if (!roomId) {
Expand Down

0 comments on commit dd923a8

Please sign in to comment.