Files
upstream-rocketchat/apps/meteor/app/apps/server/bridges/messages.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

140 lines
5.5 KiB
TypeScript

import type { IAppServerOrchestrator, IAppsMessage, IAppsUser } from '@rocket.chat/apps';
import type { ITypingDescriptor } from '@rocket.chat/apps/dist/server/bridges/MessageBridge';
import { MessageBridge } from '@rocket.chat/apps/dist/server/bridges/MessageBridge';
import type { Reaction } from '@rocket.chat/apps-engine/definition/messages';
import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { api } from '@rocket.chat/core-services';
import type { IMessage } from '@rocket.chat/core-typings';
import { Users, Subscriptions } from '@rocket.chat/models';
import { deleteMessage } from '../../../../server/lib/messages/deleteMessage';
import { updateMessage } from '../../../../server/lib/messages/updateMessage';
import { executeSetReaction } from '../../../../server/lib/messaging/reactions/setReaction';
import notifications from '../../../../server/lib/notifications/core/lib/Notifications';
import { executeSendMessage } from '../../../../server/meteor-methods/messages/sendMessage';
export class AppMessageBridge extends MessageBridge {
constructor(private readonly orch: IAppServerOrchestrator) {
super();
}
protected async create(message: IAppsMessage, appId: string): Promise<string> {
this.orch.debugLog(`The App ${appId} is creating a new message.`);
const convertedMessage: IMessage | undefined = await this.orch.getConverters()?.get('messages').convertAppMessage(message);
// #TODO: #AppsEngineTypes - Remove explicit types and typecasts once the apps-engine definition/implementation mismatch is fixed.
const definedMessage = convertedMessage as IMessage;
const sentMessage = await executeSendMessage(definedMessage.u._id, definedMessage);
return sentMessage._id;
}
protected async getById(messageId: string, appId: string): Promise<IAppsMessage> {
this.orch.debugLog(`The App ${appId} is getting the message: "${messageId}"`);
// #TODO: #AppsEngineTypes - Remove explicit types and typecasts once the apps-engine definition/implementation mismatch is fixed.
const message: IAppsMessage | undefined = await this.orch.getConverters()?.get('messages').convertById(messageId);
return message as IAppsMessage;
}
protected async update(message: IAppsMessage, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is updating a message.`);
if (!message.editor) {
throw new Error('Invalid editor assigned to the message for the update.');
}
// #TODO: #AppsEngineTypes - Remove explicit types and typecasts once the apps-engine definition/implementation mismatch is fixed.
const msg = await this.orch.getConverters()?.get('messages').convertAppMessage(message, true);
const editor = await Users.findOneById(message.editor.id);
if (!editor) {
throw new Error('Invalid editor assigned to the message for the update.');
}
await updateMessage(msg as IMessage, editor);
}
protected async delete(message: IAppsMessage, user: IAppsUser, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is deleting a message.`);
if (!message.id) {
throw new Error('Invalid message id');
}
const convertedMsg = await this.orch.getConverters()?.get('messages').convertAppMessage(message);
const convertedUser = (await Users.findOneById(user.id)) || this.orch.getConverters()?.get('users').convertToRocketChat(user);
await deleteMessage(convertedMsg as IMessage, convertedUser);
}
protected async notifyUser(user: IAppsUser, message: IAppsMessage, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is notifying a user.`);
const msg = await this.orch.getConverters()?.get('messages').convertAppMessage(message);
if (!msg) {
return;
}
void api.broadcast('notify.ephemeralMessage', user.id, msg.rid, {
...msg,
});
}
protected async notifyRoom(room: IRoom, message: IAppsMessage, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is notifying a room's users.`);
if (!room?.id) {
return;
}
// #TODO: #AppsEngineTypes - Remove explicit types and typecasts once the apps-engine definition/implementation mismatch is fixed.
const msg: IMessage | undefined = await this.orch.getConverters()?.get('messages').convertAppMessage(message);
const convertedMessage = msg as IMessage;
const users = (await Subscriptions.findByRoomIdWhenUserIdExists(room.id, { projection: { 'u._id': 1 } }).toArray()).map((s) => s.u._id);
await Users.findByIds(users, { projection: { _id: 1 } }).forEach(
({ _id }: { _id: string }) =>
void api.broadcast('notify.ephemeralMessage', _id, room.id, {
...convertedMessage,
}),
);
}
protected async typing({ scope, id, username, isTyping }: ITypingDescriptor): Promise<void> {
switch (scope) {
case 'room':
if (!username) {
throw new Error('Invalid username');
}
notifications.notifyRoom(id, 'user-activity', username, isTyping ? ['user-typing'] : []);
return;
default:
throw new Error('Unrecognized typing scope provided');
}
}
private isValidReaction(reaction: Reaction): boolean {
return reaction.startsWith(':') && reaction.endsWith(':');
}
protected async addReaction(messageId: string, userId: string, reaction: Reaction): Promise<void> {
if (!this.isValidReaction(reaction)) {
throw new Error('Invalid reaction');
}
return executeSetReaction(userId, reaction, messageId, true);
}
protected async removeReaction(messageId: string, userId: string, reaction: Reaction): Promise<void> {
if (!this.isValidReaction(reaction)) {
throw new Error('Invalid reaction');
}
return executeSetReaction(userId, reaction, messageId, false);
}
}