Skip to content

Commit

Permalink
add executeBotPostRoomCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Jun 6, 2023
1 parent 7c3f810 commit c6fbffa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export enum AppMethod {
EXECUTEPREROOMCREATEMODIFY = 'executePreRoomCreateModify',
CHECKPOSTROOMCREATE = 'checkPostRoomCreate',
EXECUTEPOSTROOMCREATE = 'executePostRoomCreate',
EXECUTEBOTPOSTROOMCREATE = 'executeBotPostRoomCreate',
CHECKPREROOMDELETEPREVENT = 'checkPreRoomDeletePrevent',
EXECUTEPREROOMDELETEPREVENT = 'executePreRoomDeletePrevent',
CHECKPOSTROOMDELETED = 'checkPostRoomDeleted',
Expand Down
15 changes: 15 additions & 0 deletions src/definition/rooms/IPostBotRoomCreate.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
}
29 changes: 29 additions & 0 deletions src/server/managers/AppListenerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c6fbffa

Please sign in to comment.