Skip to content

Commit

Permalink
fix: implement notifier on deno side (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
tapiarafael authored Sep 9, 2024
1 parent 3f97b22 commit 7170771
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
17 changes: 6 additions & 11 deletions deno-runtime/lib/accessors/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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'),
Expand All @@ -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'),
Expand Down Expand Up @@ -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;
}
}

Expand Down
70 changes: 70 additions & 0 deletions deno-runtime/lib/accessors/notifier.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
if (!message.sender || !message.sender.id) {
const appUser = await this.getAppUser();

message.sender = appUser;
}

await this.callMessageBridge('doNotifyUser', [user, message, AppObjectRegistry.get<string>('id')]);
}

public async notifyRoom(room: IRoom, message: IMessage): Promise<void> {
if (!message.sender || !message.sender.id) {
const appUser = await this.getAppUser();

message.sender = appUser;
}

await this.callMessageBridge('doNotifyRoom', [room, message, AppObjectRegistry.get<string>('id')]);
}

public async typing(options: ITypingOptions): Promise<() => Promise<void>> {
options.scope = options.scope || TypingScope.Room;

if (!options.username) {
const appUser = await this.getAppUser();
options.username = (appUser && appUser.name) || '';
}

const appId = AppObjectRegistry.get<string>('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<unknown>): Promise<void> {
await this.senderFn({
method: `bridges:getMessageBridge:${method}`,
params,
});
}

private async getAppUser(): Promise<IUser | undefined> {
const response = await this.senderFn({ method: 'bridges:getUserBridge:doGetAppUser', params: [AppObjectRegistry.get<string>('id')] });
return response.result;
}
}

0 comments on commit 7170771

Please sign in to comment.