From c6fbffa78aca8fa4dc857867e25ae8b746a37e05 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 6 Jun 2023 15:11:11 -0300 Subject: [PATCH] add executeBotPostRoomCreate --- src/definition/metadata/AppMethod.ts | 1 + src/definition/rooms/IPostBotRoomCreate.ts | 15 +++++++++++ src/server/managers/AppListenerManager.ts | 29 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/definition/rooms/IPostBotRoomCreate.ts diff --git a/src/definition/metadata/AppMethod.ts b/src/definition/metadata/AppMethod.ts index ffe80c4d6..71b913128 100644 --- a/src/definition/metadata/AppMethod.ts +++ b/src/definition/metadata/AppMethod.ts @@ -58,6 +58,7 @@ export enum AppMethod { EXECUTEPREROOMCREATEMODIFY = 'executePreRoomCreateModify', CHECKPOSTROOMCREATE = 'checkPostRoomCreate', EXECUTEPOSTROOMCREATE = 'executePostRoomCreate', + EXECUTEBOTPOSTROOMCREATE = 'executeBotPostRoomCreate', CHECKPREROOMDELETEPREVENT = 'checkPreRoomDeletePrevent', EXECUTEPREROOMDELETEPREVENT = 'executePreRoomDeletePrevent', CHECKPOSTROOMDELETED = 'checkPostRoomDeleted', diff --git a/src/definition/rooms/IPostBotRoomCreate.ts b/src/definition/rooms/IPostBotRoomCreate.ts new file mode 100644 index 000000000..849765e72 --- /dev/null +++ b/src/definition/rooms/IPostBotRoomCreate.ts @@ -0,0 +1,15 @@ +import { IHttp, IModify, IPersistence, IRead } from '../accessors'; +import { IRoom } from './IRoom'; + +/** Handler for after a dm room with Bot is created. */ +export interface IPostBotRoomCreate { + /** + * Method called *after* the room has been created. + * + * @param room The room which was created + * @param read An accessor to the environment + * @param http An accessor to the outside world + * @param persistence An accessor to the App's persistence + */ + executePostBotRoomCreate(room: IRoom, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise; +} diff --git a/src/server/managers/AppListenerManager.ts b/src/server/managers/AppListenerManager.ts index f48abc5d4..c0742f850 100644 --- a/src/server/managers/AppListenerManager.ts +++ b/src/server/managers/AppListenerManager.ts @@ -842,6 +842,35 @@ export class AppListenerManager { for (const appId of this.listeners.get(AppInterface.IPostRoomCreate)) { const app = this.manager.getOneById(appId); + // Check if the dm belongs to the app + + if (data.type === RoomType.DIRECT_MESSAGE && data.userIds.length > 1) { + for (const appId of this.listeners.get(AppInterface.IPostMessageSentToBot)) { + const app = this.manager.getOneById(appId); + if (app.hasMethod(AppMethod.EXECUTEPOSTMESSAGESENTTOBOT)) { + const reader = this.am.getReader(appId); + const bot = await reader.getUserReader().getAppUser(); + if (!bot) { + continue; + } + + if (!data.userIds.includes(bot.id)) { + continue; + } + + await app.call( + AppMethod.EXECUTEBOTPOSTROOMCREATE, + cfRoom, + this.am.getReader(appId), + this.am.getHttp(appId), + this.am.getPersistence(appId), + this.am.getModifier(appId), + ); + } + } + } + + // executePostBotRoomCreate let continueOn = true; if (app.hasMethod(AppMethod.CHECKPOSTROOMCREATE)) { continueOn = (await app.call(AppMethod.CHECKPOSTROOMCREATE, cfRoom, this.am.getReader(appId), this.am.getHttp(appId))) as boolean;