diff --git a/deno-runtime/handlers/listener/handler.ts b/deno-runtime/handlers/listener/handler.ts index 21ad917a1..6c3d3dc32 100644 --- a/deno-runtime/handlers/listener/handler.ts +++ b/deno-runtime/handlers/listener/handler.ts @@ -27,7 +27,7 @@ export default async function handleListener(method: string, params: unknown): P try { const args = parseArgs(evtInterface, params); - return await (eventExecutor as Function).apply(app, args); + return await (eventExecutor as (...args: unknown[]) => Promise).apply(app, args); } catch (e) { if (e instanceof JsonRpcError) { return e; diff --git a/src/server/managers/AppListenerManager.ts b/src/server/managers/AppListenerManager.ts index 0fe0cf34c..7d70bad06 100644 --- a/src/server/managers/AppListenerManager.ts +++ b/src/server/managers/AppListenerManager.ts @@ -32,13 +32,10 @@ import { } from '../../definition/uikit/UIKitInteractionContext'; import type { IFileUploadContext } from '../../definition/uploads/IFileUploadContext'; import type { IUser, IUserContext, IUserStatusContext, IUserUpdateContext } from '../../definition/users'; -import { MessageBuilder, MessageExtender, RoomBuilder, RoomExtender } from '../accessors'; import type { AppManager } from '../AppManager'; -import { Message } from '../messages/Message'; -import { Utilities } from '../misc/Utilities'; import type { ProxiedApp } from '../ProxiedApp'; -import { Room } from '../rooms/Room'; import type { AppAccessorManager } from './AppAccessorManager'; +import { Utilities } from '../misc/Utilities'; interface IListenerExecutor { [AppInterface.IPreMessageSentPrevent]: { @@ -490,7 +487,7 @@ export class AppListenerManager { for (const appId of this.listeners.get(AppInterface.IPreMessageSentModify)) { const app = this.manager.getOneById(appId); - const continueOn = (await app.call(AppMethod.CHECKPREMESSAGESENTMODIFY, data)) as boolean; + const continueOn = (await app.call(AppMethod.CHECKPREMESSAGESENTMODIFY, msg)) as boolean; if (continueOn) { msg = (await app.call(AppMethod.EXECUTEPREMESSAGESENTMODIFY, msg)) as IMessage; @@ -505,32 +502,31 @@ export class AppListenerManager { if (data.room.type === RoomType.DIRECT_MESSAGE && data.room.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 the sender is the bot just ignore it - - if (bot.id === data.sender.id) { - continue; - } - // if the user doesnt belong to the room ignore it - if (!data.room.userIds.includes(bot.id)) { - continue; - } - - await app.call(AppMethod.EXECUTEPOSTMESSAGESENTTOBOT, data); + + const reader = this.am.getReader(appId); + const bot = await reader.getUserReader().getAppUser(); + if (!bot) { + continue; + } + + // if the sender is the bot just ignore it + + if (bot.id === data.sender.id) { + continue; + } + // if the user doesnt belong to the room ignore it + if (!data.room.userIds.includes(bot.id)) { + continue; } + + await app.call(AppMethod.EXECUTEPOSTMESSAGESENTTOBOT, data); } } for (const appId of this.listeners.get(AppInterface.IPostMessageSent)) { const app = this.manager.getOneById(appId); - const continueOn = (await app.call(AppMethod.CHECKPOSTMESSAGESENT, data, this.am.getReader(appId), this.am.getHttp(appId))) as boolean; + const continueOn = (await app.call(AppMethod.CHECKPOSTMESSAGESENT, data)) as boolean; if (continueOn) { await app.call(AppMethod.EXECUTEPOSTMESSAGESENT, data); @@ -764,63 +760,28 @@ export class AppListenerManager { } } - // TODO CONTINUE FROM HERE private async executePostRoomUserLeave(externalData: IRoomUserLeaveContext): Promise { - const data = Utilities.deepClone(externalData); - - data.room = new Room(Utilities.deepFreeze(data.room), this.manager); - Utilities.deepFreeze(data.leavingUser); - for (const appId of this.listeners.get(AppInterface.IPostRoomUserLeave)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_ROOM_USER_LEAVE)) { - await app.call( - AppMethod.EXECUTE_POST_ROOM_USER_LEAVE, - data, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_ROOM_USER_LEAVE, externalData); } } // External Components private async executePostExternalComponentOpened(data: IExternalComponent): Promise { - const cfExternalComponent = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostExternalComponentOpened)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTEPOSTEXTERNALCOMPONENTOPENED)) { - await app.call( - AppMethod.EXECUTEPOSTEXTERNALCOMPONENTOPENED, - cfExternalComponent, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - ); - } + await app.call(AppMethod.EXECUTEPOSTEXTERNALCOMPONENTOPENED, data); } } private async executePostExternalComponentClosed(data: IExternalComponent): Promise { - const cfExternalComponent = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostExternalComponentClosed)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTEPOSTEXTERNALCOMPONENTCLOSED)) { - await app.call( - AppMethod.EXECUTEPOSTEXTERNALCOMPONENTCLOSED, - cfExternalComponent, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - ); - } + await app.call(AppMethod.EXECUTEPOSTEXTERNALCOMPONENTCLOSED, data); } } @@ -998,190 +959,75 @@ export class AppListenerManager { // Livechat private async executePostLivechatRoomStarted(data: ILivechatRoom): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatRoomStarted)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_STARTED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_ROOM_STARTED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_STARTED, data); } } private async executeLivechatRoomClosedHandler(data: ILivechatRoom): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.ILivechatRoomClosedHandler)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER, data); } } private async executePostLivechatRoomClosed(data: ILivechatRoom): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatRoomClosed)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_CLOSED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_ROOM_CLOSED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_CLOSED, data); } } private async executePostLivechatAgentAssigned(data: ILivechatEventContext): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatAgentAssigned)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_AGENT_ASSIGNED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_AGENT_ASSIGNED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_AGENT_ASSIGNED, data); } } private async executePostLivechatAgentUnassigned(data: ILivechatEventContext): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatAgentUnassigned)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_AGENT_UNASSIGNED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_AGENT_UNASSIGNED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_AGENT_UNASSIGNED, data); } } private async executePostLivechatRoomTransferred(data: ILivechatTransferEventContext): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatRoomTransferred)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_TRANSFERRED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_ROOM_TRANSFERRED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_TRANSFERRED, data); } } private async executePostLivechatGuestSaved(data: IVisitor): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatGuestSaved)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_GUEST_SAVED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_GUEST_SAVED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_GUEST_SAVED, data); } } private async executePostLivechatRoomSaved(data: ILivechatRoom): Promise { - const cfLivechatRoom = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostLivechatRoomSaved)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_SAVED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_LIVECHAT_ROOM_SAVED, - cfLivechatRoom, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_LIVECHAT_ROOM_SAVED, data); } } // FileUpload private async executePreFileUpload(data: IFileUploadContext): Promise { - const context = Object.freeze(data); - for (const appId of this.listeners.get(AppInterface.IPreFileUpload)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_PRE_FILE_UPLOAD)) { - await app.call( - AppMethod.EXECUTE_PRE_FILE_UPLOAD, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_PRE_FILE_UPLOAD, data); } } @@ -1191,240 +1037,100 @@ export class AppListenerManager { for (const appId of this.listeners.get(AppInterface.IPreEmailSent)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_PRE_EMAIL_SENT)) { - descriptor = await app.call( - AppMethod.EXECUTE_PRE_EMAIL_SENT, - { - context: data.context, - email: descriptor, - }, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + descriptor = await app.call(AppMethod.EXECUTE_PRE_EMAIL_SENT, { + context: data.context, + email: descriptor, + }); } return descriptor; } private async executePostMessageReacted(data: IMessageReactionContext): Promise { - const context = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostMessageReacted)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_MESSAGE_REACTED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_MESSAGE_REACTED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_MESSAGE_REACTED, data); } } private async executePostMessageFollowed(data: IMessageFollowContext): Promise { - const context = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostMessageFollowed)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_MESSAGE_FOLLOWED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_MESSAGE_FOLLOWED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_MESSAGE_FOLLOWED, data); } } private async executePostMessagePinned(data: IMessagePinContext): Promise { - const context = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostMessagePinned)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_MESSAGE_PINNED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_MESSAGE_PINNED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_MESSAGE_PINNED, data); } } private async executePostMessageStarred(data: IMessageStarContext): Promise { - const context = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostMessageStarred)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_MESSAGE_STARRED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_MESSAGE_STARRED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_MESSAGE_STARRED, data); } } private async executePostMessageReported(data: IMessageReportContext): Promise { - const context = Utilities.deepCloneAndFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostMessageReported)) { const app = this.manager.getOneById(appId); - if (!app.hasMethod(AppMethod.EXECUTE_POST_MESSAGE_REPORTED)) { - continue; - } - - await app.call( - AppMethod.EXECUTE_POST_MESSAGE_REPORTED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); + await app.call(AppMethod.EXECUTE_POST_MESSAGE_REPORTED, data); } } private async executePostUserCreated(data: IUserContext): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserCreated)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_CREATED)) { - await app.call( - AppMethod.EXECUTE_POST_USER_CREATED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_CREATED, data); } } private async executePostUserUpdated(data: IUserUpdateContext): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserUpdated)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_UPDATED)) { - await app.call( - AppMethod.EXECUTE_POST_USER_UPDATED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_UPDATED, data); } } private async executePostUserDeleted(data: IUserContext): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserDeleted)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_DELETED)) { - await app.call( - AppMethod.EXECUTE_POST_USER_DELETED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_DELETED, data); } } private async executePostUserLoggedIn(data: IUser): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserLoggedIn)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_LOGGED_IN)) { - await app.call( - AppMethod.EXECUTE_POST_USER_LOGGED_IN, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_LOGGED_IN, data); } } private async executePostUserLoggedOut(data: IUser): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserLoggedOut)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_LOGGED_OUT)) { - await app.call( - AppMethod.EXECUTE_POST_USER_LOGGED_OUT, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_LOGGED_OUT, data); } } private async executePostUserStatusChanged(data: IUserStatusContext): Promise { - const context = Utilities.deepFreeze(data); - for (const appId of this.listeners.get(AppInterface.IPostUserStatusChanged)) { const app = this.manager.getOneById(appId); - if (app.hasMethod(AppMethod.EXECUTE_POST_USER_STATUS_CHANGED)) { - await app.call( - AppMethod.EXECUTE_POST_USER_STATUS_CHANGED, - context, - this.am.getReader(appId), - this.am.getHttp(appId), - this.am.getPersistence(appId), - this.am.getModifier(appId), - ); - } + await app.call(AppMethod.EXECUTE_POST_USER_STATUS_CHANGED, data); } } }