From 71707718a8d55de19f8b1b72e7217e9fd2b5e5f0 Mon Sep 17 00:00:00 2001 From: Rafael Tapia Date: Mon, 9 Sep 2024 18:42:22 -0300 Subject: [PATCH] fix: implement notifier on deno side (#797) --- deno-runtime/lib/accessors/mod.ts | 17 +++---- deno-runtime/lib/accessors/notifier.ts | 70 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 deno-runtime/lib/accessors/notifier.ts diff --git a/deno-runtime/lib/accessors/mod.ts b/deno-runtime/lib/accessors/mod.ts index 3c0c0670f..e71f01442 100644 --- a/deno-runtime/lib/accessors/mod.ts +++ b/deno-runtime/lib/accessors/mod.ts @@ -21,7 +21,7 @@ import { AppObjectRegistry } from '../../AppObjectRegistry.ts'; import { ModifyCreator } from './modify/ModifyCreator.ts'; import { ModifyUpdater } from './modify/ModifyUpdater.ts'; import { ModifyExtender } from './modify/ModifyExtender.ts'; -import { MessageBuilder } from "./builders/MessageBuilder.ts"; +import { Notifier } from './notifier.ts'; const httpMethods = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const; @@ -77,6 +77,7 @@ export class AppAccessors { ) as T; this.http = new Http(this.getReader(), this.getPersistence(), this.httpExtend, this.getSenderFn()); + this.notifier = new Notifier(this.getSenderFn()); } public getSenderFn() { @@ -225,7 +226,7 @@ export class AppAccessors { getPersistenceReader: () => this.proxify('getReader:getPersistenceReader'), getRoomReader: () => this.proxify('getReader:getRoomReader'), getUserReader: () => this.proxify('getReader:getUserReader'), - getNotifier: () => this.getNotifier('getReader:getNotifier'), + getNotifier: () => this.getNotifier(), getLivechatReader: () => this.proxify('getReader:getLivechatReader'), getUploadReader: () => this.proxify('getReader:getUploadReader'), getCloudWorkspaceReader: () => this.proxify('getReader:getCloudWorkspaceReader'), @@ -246,7 +247,7 @@ export class AppAccessors { getUpdater: this.getUpdater.bind(this), getExtender: this.getExtender.bind(this), getDeleter: () => this.proxify('getModifier:getDeleter'), - getNotifier: () => this.getNotifier('getModifier:getNotifier'), + getNotifier: () => this.getNotifier(), getUiController: () => this.proxify('getModifier:getUiController'), getScheduler: () => this.proxify('getModifier:getScheduler'), getOAuthAppsModifier: () => this.proxify('getModifier:getOAuthAppsModifier'), @@ -293,14 +294,8 @@ export class AppAccessors { return this.extender; } - private getNotifier(namespace: string) { - if (!this.notifier) { - this.notifier = this.proxify(namespace, { - getMessageBuilder: () => new MessageBuilder(), - }); - } - - return this.notifier; + private getNotifier() { + return this.notifier; } } diff --git a/deno-runtime/lib/accessors/notifier.ts b/deno-runtime/lib/accessors/notifier.ts new file mode 100644 index 000000000..ed075fd02 --- /dev/null +++ b/deno-runtime/lib/accessors/notifier.ts @@ -0,0 +1,70 @@ +import type { IMessageBuilder, INotifier } from '@rocket.chat/apps-engine/definition/accessors'; +import type { ITypingOptions } from '@rocket.chat/apps-engine/definition/accessors/INotifier.ts'; +import { TypingScope } from '@rocket.chat/apps-engine/definition/accessors/INotifier.ts'; +import type { IMessage } from '@rocket.chat/apps-engine/definition/messages'; +import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; +import type { IUser } from '@rocket.chat/apps-engine/definition/users'; +import { MessageBuilder } from './builders/MessageBuilder.ts'; +import { AppObjectRegistry } from '../../AppObjectRegistry.ts'; +import * as Messenger from '../messenger.ts'; + +export class Notifier implements INotifier { + private senderFn: typeof Messenger.sendRequest; + + constructor(senderFn: typeof Messenger.sendRequest) { + this.senderFn = senderFn; + } + + public async notifyUser(user: IUser, message: IMessage): Promise { + if (!message.sender || !message.sender.id) { + const appUser = await this.getAppUser(); + + message.sender = appUser; + } + + await this.callMessageBridge('doNotifyUser', [user, message, AppObjectRegistry.get('id')]); + } + + public async notifyRoom(room: IRoom, message: IMessage): Promise { + if (!message.sender || !message.sender.id) { + const appUser = await this.getAppUser(); + + message.sender = appUser; + } + + await this.callMessageBridge('doNotifyRoom', [room, message, AppObjectRegistry.get('id')]); + } + + public async typing(options: ITypingOptions): Promise<() => Promise> { + options.scope = options.scope || TypingScope.Room; + + if (!options.username) { + const appUser = await this.getAppUser(); + options.username = (appUser && appUser.name) || ''; + } + + const appId = AppObjectRegistry.get('id'); + + await this.callMessageBridge('doTyping', [{ ...options, isTyping: true }, appId]); + + return async () => { + await this.callMessageBridge('doTyping', [{ ...options, isTyping: false }, appId]); + }; + } + + public getMessageBuilder(): IMessageBuilder { + return new MessageBuilder(); + } + + private async callMessageBridge(method: string, params: Array): Promise { + await this.senderFn({ + method: `bridges:getMessageBridge:${method}`, + params, + }); + } + + private async getAppUser(): Promise { + const response = await this.senderFn({ method: 'bridges:getUserBridge:doGetAppUser', params: [AppObjectRegistry.get('id')] }); + return response.result; + } +}