diff --git a/src/config.service.ts b/src/config.service.ts index 0dd2182f..0a5e8450 100644 --- a/src/config.service.ts +++ b/src/config.service.ts @@ -1,22 +1,18 @@ -import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { GlobalWebhookConfigConfig } from '@waha/core/plugins/WebhookPlugin.env'; import { MediaDownloadOptions } from '@waha/core/media/IMediaManager'; import { IgnoreJidConfig } from '@waha/core/utils/jids'; import * as lodash from 'lodash'; import { parseBool } from './helpers'; -import { WebhookConfig } from './structures/webhooks.config.dto'; import { Auth } from '@waha/core/auth/config'; @Injectable() -export class WhatsappConfigService implements OnApplicationBootstrap { +export class WhatsappConfigService { private logger: Logger; - private webhookConfig: GlobalWebhookConfigConfig; constructor(private configService: ConfigService) { this.logger = new Logger('WhatsappConfigService'); - this.webhookConfig = new GlobalWebhookConfigConfig(configService); } get schema() { @@ -155,10 +151,6 @@ export class WhatsappConfigService implements OnApplicationBootstrap { return this.configService.get('WHATSAPP_PROXY_SERVER_PASSWORD', undefined); } - getWebhookConfig(): WebhookConfig | undefined { - return this.webhookConfig.config; - } - getSessionMongoUrl(): string | undefined { return this.configService.get('WHATSAPP_SESSIONS_MONGO_URL', undefined); } @@ -236,11 +228,4 @@ export class WhatsappConfigService implements OnApplicationBootstrap { ); return { status, groups, channels, broadcast }; } - - onApplicationBootstrap() { - const error = this.webhookConfig.validateConfig(); - if (error) { - throw new Error(`Invalid global webhook config:\n${error}\n`); - } - } } diff --git a/src/core/app.module.core.ts b/src/core/app.module.core.ts index 623665f9..110cc7a3 100644 --- a/src/core/app.module.core.ts +++ b/src/core/app.module.core.ts @@ -34,6 +34,7 @@ import { isPresenceAutoOnlineEnabled } from '@waha/modules/waha-maintain-online- import { MaintainOnlineStatusModule } from '@waha/modules/waha-maintain-online-status/maintain-online-status.module'; import { isJidEngine } from '@waha/modules/waha-wid-jid/wid-jid.plugins'; import { MessageSourceModule } from '@waha/modules/waha-message-source/message-source.module'; +import { WebhookModule } from '@waha/modules/waha-webhook/webhook.module'; import { SessionRuntimeInfoModule } from '@waha/modules/waha-session-runtime-info/session-runtime-info.module'; import { WidJIDModule } from '@waha/modules/waha-wid-jid/wid-jid.module'; import { WidSuffixModule } from '@waha/modules/waha-wid-suffix/wid-suffix.module'; @@ -149,6 +150,7 @@ export const IMPORTS_CORE = [ TerminusModule, SessionPluginsModule, SessionRuntimeInfoModule, + WebhookModule, MessageSourceModule, WidSuffixModule, ConditionalModule.registerWhen(WidJIDModule, isJidEngine, { diff --git a/src/core/manager.core.ts b/src/core/manager.core.ts index 5a5d3c71..11ffe1e0 100644 --- a/src/core/manager.core.ts +++ b/src/core/manager.core.ts @@ -67,12 +67,10 @@ import { SessionDTO, SessionInfo, } from '../structures/sessions.dto'; -import { WebhookConfig } from '../structures/webhooks.config.dto'; import { populateSessionInfo, SessionManager } from './abc/manager.abc'; import { SessionParams, WhatsappSession } from './abc/session.abc'; import { EngineConfigService } from './config/EngineConfigService'; -import { WebhookPlugin } from '@waha/core/plugins/WebhookPlugin'; import { SessionPluginsService } from '@waha/plugins/SessionPluginsService'; const ALL = '*'; @@ -379,9 +377,6 @@ export class SessionManagerCore this.updateSessions(); // Plugins - const webhooks = this.getWebhooks(config); - session.plugins.add(WebhookPlugin.with({ webhooks: webhooks }, null)); - // Plugins contributed by modules (SessionPluginsModule) for (const options of this.sessionPlugins.plugins(session)) { session.plugins.add(options); } @@ -479,21 +474,6 @@ export class SessionManagerCore this.log.info({ session: name }, `Session has been logged out.`); } - /** - * Combine per session and global webhooks - */ - private getWebhooks(config: SessionConfig) { - let webhooks: WebhookConfig[] = []; - if (config?.webhooks) { - webhooks = webhooks.concat(config.webhooks); - } - const globalWebhookConfig = this.config.getWebhookConfig(); - if (globalWebhookConfig) { - webhooks.push(globalWebhookConfig); - } - return webhooks; - } - /** * Get either session's or global proxy if defined */ diff --git a/src/core/plugins/WebhookPlugin.sender.ts b/src/modules/waha-webhook/WebhookPlugin.sender.ts similarity index 100% rename from src/core/plugins/WebhookPlugin.sender.ts rename to src/modules/waha-webhook/WebhookPlugin.sender.ts diff --git a/src/core/plugins/WebhookPlugin.ts b/src/modules/waha-webhook/WebhookPlugin.ts similarity index 74% rename from src/core/plugins/WebhookPlugin.ts rename to src/modules/waha-webhook/WebhookPlugin.ts index 8a8e5fad..576d599a 100644 --- a/src/core/plugins/WebhookPlugin.ts +++ b/src/modules/waha-webhook/WebhookPlugin.ts @@ -1,26 +1,38 @@ import { populateSessionInfo } from '@waha/core/abc/manager.abc'; import { SessionPlugin } from '@waha/core/abc/session.plugin'; -import { WebhookSender } from '@waha/core/plugins/WebhookPlugin.sender'; +import { WebhookSender } from '@waha/modules/waha-webhook/WebhookPlugin.sender'; import { WAHAEvents, WAHAEventsWild } from '@waha/structures/enums.dto'; import { WebhookConfig } from '@waha/structures/webhooks.config.dto'; import { EventWildUnmask } from '@waha/utils/events'; export class WebhookPluginConfig { - webhooks: WebhookConfig[]; + webhooks: WebhookConfig | null; } /** - * Sends session events to the configured webhooks (per session and global ones) + * Sends session events to the configured webhooks (per session and predefined ones) */ export class WebhookPlugin extends SessionPlugin { private eventUnmask = new EventWildUnmask(WAHAEvents, WAHAEventsWild); attach(): void { - for (const webhookConfig of this.config.webhooks) { + for (const webhookConfig of this.webhooks()) { this.configure(webhookConfig); } } + /** + * Session webhooks + the predefined one (WHATSAPP_HOOK_URL) + */ + private webhooks(): WebhookConfig[] { + const webhooks: WebhookConfig[] = []; + webhooks.push(...(this.session.sessionConfig?.webhooks ?? [])); + if (this.config.webhooks) { + webhooks.push(this.config.webhooks); + } + return webhooks; + } + private getSuitableEvents(events: WAHAEvents[] | string[]): WAHAEvents[] { return this.eventUnmask.unmask(events); } diff --git a/src/core/plugins/WebhookPlugin.env.ts b/src/modules/waha-webhook/webhook.config.ts similarity index 93% rename from src/core/plugins/WebhookPlugin.env.ts rename to src/modules/waha-webhook/webhook.config.ts index d73dc30e..58c69744 100644 --- a/src/core/plugins/WebhookPlugin.env.ts +++ b/src/modules/waha-webhook/webhook.config.ts @@ -1,3 +1,4 @@ +import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { WAHAEvents } from '@waha/structures/enums.dto'; import { @@ -19,11 +20,19 @@ enum Env { WHATSAPP_HOOK_CUSTOM_HEADERS = 'WHATSAPP_HOOK_CUSTOM_HEADERS', } -export class GlobalWebhookConfigConfig { +@Injectable() +export class GlobalWebhookConfig implements OnApplicationBootstrap { protected _config: WebhookConfig | null = null; constructor(private configService: ConfigService) {} + onApplicationBootstrap() { + const error = this.validateConfig(); + if (error) { + throw new Error(`Invalid global webhook config:\n${error}\n`); + } + } + private getUrl(): string | undefined { return this.configService.get(Env.WHATSAPP_HOOK_URL); } diff --git a/src/modules/waha-webhook/webhook.module.ts b/src/modules/waha-webhook/webhook.module.ts new file mode 100644 index 00000000..92e8222b --- /dev/null +++ b/src/modules/waha-webhook/webhook.module.ts @@ -0,0 +1,18 @@ +import { Module } from '@nestjs/common'; +import { GlobalWebhookConfig } from '@waha/modules/waha-webhook/webhook.config'; +import { WebhookPluginsProvider } from '@waha/modules/waha-webhook/webhook.plugins'; +import { SessionPluginsModule } from '@waha/plugins/session.plugins.module'; +import { SessionPluginsService } from '@waha/plugins/SessionPluginsService'; + +@Module({ + imports: [SessionPluginsModule], + providers: [GlobalWebhookConfig, WebhookPluginsProvider], +}) +export class WebhookModule { + constructor( + sessionPlugins: SessionPluginsService, + provider: WebhookPluginsProvider, + ) { + sessionPlugins.register(provider); + } +} diff --git a/src/modules/waha-webhook/webhook.plugins.ts b/src/modules/waha-webhook/webhook.plugins.ts new file mode 100644 index 00000000..99b4888c --- /dev/null +++ b/src/modules/waha-webhook/webhook.plugins.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@nestjs/common'; +import type { WhatsappSession } from '@waha/core/abc/session.abc'; +import { PluginOptions } from '@waha/core/abc/session.plugin'; +import { GlobalWebhookConfig } from '@waha/modules/waha-webhook/webhook.config'; +import { WebhookPlugin } from '@waha/modules/waha-webhook/WebhookPlugin'; +import { SessionPluginsProvider } from '@waha/plugins/SessionPluginsService'; + +@Injectable() +export class WebhookPluginsProvider implements SessionPluginsProvider { + constructor(private globalWebhook: GlobalWebhookConfig) {} + + plugins(session: WhatsappSession): PluginOptions[] { + return [WebhookPlugin.with({ webhooks: this.globalWebhook.config }, null)]; + } +}