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: Apps-Engine method to read multiple messages from a room #32176

Merged
merged 37 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d7134c5
implement the rooms bridge
Dnouv Apr 11, 2024
2c6bd5b
change param order
Dnouv Apr 15, 2024
f3bb1a5
Update Apps-Engine
d-gubert Apr 15, 2024
4b0c386
change changeset
Dnouv Apr 16, 2024
9d34e6d
Merge branch 'develop' into new/room_msg_bridge
Dnouv Apr 16, 2024
2b20034
make changeset less developer friendly
Dnouv Apr 16, 2024
caf56fb
Update .changeset/polite-foxes-repair.md
d-gubert Apr 16, 2024
c1c5378
handle type assertions
Apr 16, 2024
d35c081
handle NaN, -Infifity
Apr 17, 2024
12b2b84
merge develop
Dnouv May 20, 2024
fb24310
resolve conflick on yarn.lock
Dnouv May 20, 2024
47418c5
update method and linting issues
Dnouv May 20, 2024
33cd125
Merge branch 'develop' into new/room_msg_bridge
d-gubert May 20, 2024
9c10374
introduce new converter to reduce db calls
Dnouv May 23, 2024
e9913d8
correct type for editedby id
Dnouv May 23, 2024
4f91b44
Merge branch 'develop' into new/room_msg_bridge
Dnouv May 23, 2024
667dcd3
Update packages/apps/src/converters/IAppMessagesConverter.ts
d-gubert May 23, 2024
cd37b40
add new type msgraw
Dnouv May 24, 2024
248495a
update apps-engine version
Dnouv May 30, 2024
eb4e60d
Merge branch 'develop' into new/room_msg_bridge
Dnouv May 30, 2024
624e077
Merge branch 'develop' into new/room_msg_bridge
d-gubert May 31, 2024
a58737b
Merge branch 'develop' into new/room_msg_bridge
d-gubert May 31, 2024
06042f6
Merge branch 'develop' into new/room_msg_bridge
d-gubert Jun 3, 2024
d3c51be
use map<->foreach
Dnouv Jun 4, 2024
fd2aee8
add logs
Dnouv Jun 7, 2024
41f3b9f
Fix wrong field name
d-gubert Jun 10, 2024
deabeb6
remove the limit checks
Dnouv Jun 20, 2024
f35fa9a
merge develop
Dnouv Jun 20, 2024
0dc45d9
Merge remote-tracking branch 'origin/develop' into new/room_msg_bridge
d-gubert Jul 19, 2024
6ac6e9d
Adapt implementation to apps-engine changes
d-gubert Jul 19, 2024
286d1a6
Merge branch 'develop' into new/room_msg_bridge
Dnouv Jul 19, 2024
a5348fd
bump apps engine
Dnouv Jul 19, 2024
b0d35be
lint
Dnouv Jul 19, 2024
bd288d8
Merge branch 'develop' into new/room_msg_bridge
Dnouv Jul 19, 2024
22aca58
Convert field name in sort option
d-gubert Jul 19, 2024
a73afff
Merge branch 'develop' into new/room_msg_bridge
d-gubert Jul 19, 2024
8a0e0fa
Merge branch 'develop' into new/room_msg_bridge
d-gubert Jul 19, 2024
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
5 changes: 5 additions & 0 deletions .changeset/polite-foxes-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---

Added a method to the Apps-Engine that allows apps to read multiple messages from a room
48 changes: 37 additions & 11 deletions apps/meteor/app/apps/server/bridges/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { IAppServerOrchestrator } from '@rocket.chat/apps';
import type { IMessage } from '@rocket.chat/apps-engine/definition/messages';
import type { IMessage, IMessageRaw } from '@rocket.chat/apps-engine/definition/messages';
import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import type { IUser } from '@rocket.chat/apps-engine/definition/users';
import type { GetMessagesOptions } from '@rocket.chat/apps-engine/server/bridges/RoomBridge';
import { RoomBridge } from '@rocket.chat/apps-engine/server/bridges/RoomBridge';
import type { ISubscription, IUser as ICoreUser, IRoom as ICoreRoom } from '@rocket.chat/core-typings';
import { Subscriptions, Users, Rooms } from '@rocket.chat/models';
import type { ISubscription, IUser as ICoreUser, IRoom as ICoreRoom, IMessage as ICoreMessage } from '@rocket.chat/core-typings';
import { Subscriptions, Users, Rooms, Messages } from '@rocket.chat/models';
import type { FindOptions, Sort } from 'mongodb';

import { createDirectMessage } from '../../../../server/methods/createDirectMessage';
import { createDiscussion } from '../../../discussion/server/methods/createDiscussion';
Expand Down Expand Up @@ -103,6 +105,38 @@ export class AppRoomBridge extends RoomBridge {
return this.orch.getConverters()?.get('users').convertById(room.u._id);
}

protected async getMessages(roomId: string, options: GetMessagesOptions, appId: string): Promise<IMessageRaw[]> {
this.orch.debugLog(`The App ${appId} is getting the messages of the room: "${roomId}" with options:`, options);

const { limit, skip = 0, sort: _sort } = options;

const messageConverter = this.orch.getConverters()?.get('messages');
if (!messageConverter) {
throw new Error('Message converter not found');
}
Dnouv marked this conversation as resolved.
Show resolved Hide resolved

// We support only one field for now
const sort: Sort | undefined = _sort?.createdAt ? { ts: _sort.createdAt } : undefined;

const messageQueryOptions: FindOptions<ICoreMessage> = {
limit,
skip,
sort,
};

const query = {
Dnouv marked this conversation as resolved.
Show resolved Hide resolved
rid: roomId,
_hidden: { $ne: true },
t: { $exists: false },
};

const cursor = Messages.find(query, messageQueryOptions);
Dnouv marked this conversation as resolved.
Show resolved Hide resolved

const messagePromises: Promise<IMessageRaw>[] = await cursor.map((message) => messageConverter.convertMessageRaw(message)).toArray();

return Promise.all(messagePromises);
}

protected async getMembers(roomId: string, appId: string): Promise<Array<IUser>> {
this.orch.debugLog(`The App ${appId} is getting the room's members by room id: "${roomId}"`);
const subscriptions = await Subscriptions.findByRoomId(roomId, {});
Expand Down Expand Up @@ -220,12 +254,4 @@ export class AppRoomBridge extends RoomBridge {
const members = await Users.findUsersByUsernames(usernames, { limit: 50 }).toArray();
await Promise.all(members.map((user) => removeUserFromRoom(roomId, user)));
}

protected getMessages(
_roomId: string,
_options: { limit: number; skip?: number; sort?: Record<string, 1 | -1> },
_appId: string,
): Promise<IMessage[]> {
throw new Error('Method not implemented.');
}
}
34 changes: 34 additions & 0 deletions apps/meteor/app/apps/server/converters/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ export class AppMessagesConverter {
return this.convertMessage(msg);
}

async convertMessageRaw(msgObj) {
if (!msgObj) {
return undefined;
}

const { attachments, ...message } = msgObj;
const getAttachments = async () => this._convertAttachmentsToApp(attachments);
Dnouv marked this conversation as resolved.
Show resolved Hide resolved

const map = {
id: '_id',
threadId: 'tmid',
reactions: 'reactions',
parseUrls: 'parseUrls',
text: 'msg',
createdAt: 'ts',
updatedAt: '_updatedAt',
editedAt: 'editedAt',
emoji: 'emoji',
avatarUrl: 'avatar',
alias: 'alias',
file: 'file',
customFields: 'customFields',
groupable: 'groupable',
token: 'token',
blocks: 'blocks',
roomId: 'rid',
editor: 'editedBy',
attachments: getAttachments,
sender: 'u',
};

return transformMappedData(message, map);
}

async convertMessage(msgObj) {
if (!msgObj) {
return undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/apps/src/AppsEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type {
IVisitorPhone as IAppsVisitorPhone,
} from '@rocket.chat/apps-engine/definition/livechat';
export type { IMessage as IAppsMessage } from '@rocket.chat/apps-engine/definition/messages';
export type { IMessageRaw as IAppsMesssageRaw } from '@rocket.chat/apps-engine/definition/messages';
export { AppInterface as AppEvents } from '@rocket.chat/apps-engine/definition/metadata';
export type { IUser as IAppsUser } from '@rocket.chat/apps-engine/definition/users';
export type { IRole as IAppsRole } from '@rocket.chat/apps-engine/definition/roles';
Expand Down
4 changes: 3 additions & 1 deletion packages/apps/src/converters/IAppMessagesConverter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IMessage } from '@rocket.chat/core-typings';

import type { IAppsMessage } from '../AppsEngine';
import type { IAppsMessage, IAppsMesssageRaw } from '../AppsEngine';

export interface IAppMessagesConverter {
convertById(messageId: IMessage['_id']): Promise<IAppsMessage | undefined>;
Expand All @@ -10,4 +10,6 @@ export interface IAppMessagesConverter {
convertAppMessage(message: undefined | null): Promise<undefined>;
convertAppMessage(message: IAppsMessage): Promise<IMessage | undefined>;
convertAppMessage(message: IAppsMessage | undefined | null): Promise<IMessage | undefined>;
convertMessageRaw(message: IMessage): Promise<IAppsMesssageRaw>;
convertMessageRaw(message: IMessage | undefined | null): Promise<IAppsMesssageRaw | undefined>;
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8510,8 +8510,8 @@ __metadata:
linkType: soft

"@rocket.chat/apps-engine@npm:alpha":
version: 1.44.0-alpha.818
resolution: "@rocket.chat/apps-engine@npm:1.44.0-alpha.818"
version: 1.44.0-alpha.828
resolution: "@rocket.chat/apps-engine@npm:1.44.0-alpha.828"
dependencies:
"@msgpack/msgpack": 3.0.0-beta2
adm-zip: ^0.5.9
Expand All @@ -8527,7 +8527,7 @@ __metadata:
uuid: ~8.3.2
peerDependencies:
"@rocket.chat/ui-kit": "*"
checksum: acef47bc7f13e0682d018531638b5168c3acd59beae37b013e881ea6fadfe12670fe10545f4a89487f7bedbe9166028833cba7ed3fc401d4283327e47e00e61c
checksum: e26914b62d2e9823577fe8165a2635d65f69ddc315a880bbc417ddd674e4df487dc9bc9507bf3a0616de06cd927596872c1e90e4c29c61da8581e0a1b7c8d97d
languageName: node
linkType: hard

Expand Down
Loading