diff --git a/src/apps/chatwoot/consumers/waha/base.ts b/src/apps/chatwoot/consumers/waha/base.ts index e479ee50..44467c0c 100644 --- a/src/apps/chatwoot/consumers/waha/base.ts +++ b/src/apps/chatwoot/consumers/waha/base.ts @@ -34,19 +34,26 @@ import { isJidBroadcast, isJidGroup, toCusFormat } from '@waha/core/utils/jids'; import { EngineHelper } from '@waha/apps/chatwoot/waha'; import { EnsureSeconds } from '@waha/utils/timehelper'; import { CHATWOOT_MESSAGE_CALENDAR_THRESHOLD_SECONDS } from '@waha/apps/chatwoot/env'; +import { + ChatWootAppConfig, + ChatWootConfig, +} from '@waha/apps/chatwoot/dto/config.dto'; -export function ListenEventsForChatWoot() { - return [ +export function ListenEventsForChatWoot(config: ChatWootConfig) { + const events = [ WAHAEvents.MESSAGE_ANY, WAHAEvents.MESSAGE_REACTION, WAHAEvents.MESSAGE_EDITED, WAHAEvents.MESSAGE_REVOKED, - WAHAEvents.MESSAGE_ACK, WAHAEvents.SESSION_STATUS, WAHAEvents.CALL_RECEIVED, WAHAEvents.CALL_ACCEPTED, WAHAEvents.CALL_REJECTED, ]; + if (config.conversations.markAsRead) { + events.push(WAHAEvents.MESSAGE_ACK); + } + return events; } /** diff --git a/src/apps/chatwoot/di/DIContainer.ts b/src/apps/chatwoot/di/DIContainer.ts index 62e9947c..b79d22d5 100644 --- a/src/apps/chatwoot/di/DIContainer.ts +++ b/src/apps/chatwoot/di/DIContainer.ts @@ -207,18 +207,28 @@ export class DIContainer { @CacheSync() public ChatWootConfig(): ChatWootConfig { - const defaults: ChatWootConfig = { - templates: {}, - linkPreview: LinkPreview.OFF, - commands: { - server: true, - queue: false, - }, - conversations: { - sort: ConversationSort.created_newest, - status: null, - }, - }; - return lodash.defaults({}, this.config, defaults); + return ChatWootConfigDefaults(this.config); } } + +/** + * Applies default values to the ChatWoot configuration + */ +export function ChatWootConfigDefaults( + config: ChatWootAppConfig, +): ChatWootConfig { + const defaults: ChatWootConfig = { + templates: {}, + linkPreview: LinkPreview.OFF, + commands: { + server: true, + queue: false, + }, + conversations: { + sort: ConversationSort.created_newest, + status: null, + markAsRead: true, + }, + }; + return lodash.defaultsDeep({}, config, defaults); +} diff --git a/src/apps/chatwoot/dto/config.dto.ts b/src/apps/chatwoot/dto/config.dto.ts index c576909f..68cabc39 100644 --- a/src/apps/chatwoot/dto/config.dto.ts +++ b/src/apps/chatwoot/dto/config.dto.ts @@ -7,12 +7,10 @@ import { IsString, ValidateNested, } from 'class-validator'; +import { ApiPropertyOptional } from '@nestjs/swagger'; import { Type } from 'class-transformer'; import { IsDynamicObject } from '@waha/nestjs/validation/IsDynamicObject'; -import { - ConversationSelectorConfig, - ConversationSort, -} from '@waha/apps/chatwoot/services/ConversationSelector'; +import { ConversationSort } from '@waha/apps/chatwoot/services/ConversationSelector'; import { ConversationStatus } from '@waha/apps/chatwoot/client/types'; export const DEFAULT_LOCALE = 'en-US'; @@ -39,6 +37,15 @@ export class ChatWootConversationsConfig { @IsOptional() @IsEnum(ConversationStatus, { each: true }) status: Array | null; + + @ApiPropertyOptional({ + description: + 'Process message.ack events to mark ChatWoot conversations as read. Enabled by default.', + default: true, + }) + @IsOptional() + @IsBoolean() + markAsRead?: boolean = true; } export interface ChatWootConfig { diff --git a/src/apps/chatwoot/services/ChatWootAppService.ts b/src/apps/chatwoot/services/ChatWootAppService.ts index 5033bc67..dbf65102 100644 --- a/src/apps/chatwoot/services/ChatWootAppService.ts +++ b/src/apps/chatwoot/services/ChatWootAppService.ts @@ -120,7 +120,7 @@ export class ChatWootAppService implements IAppService { } beforeSessionStart(app: App, session: WhatsappSession) { - this.chatWootWAHAQueueService.listenEvents(app.id, session); + this.chatWootWAHAQueueService.listenEvents(app, session); } afterSessionStart(app: App, session: WhatsappSession) { diff --git a/src/apps/chatwoot/services/ChatWootWAHAQueueService.ts b/src/apps/chatwoot/services/ChatWootWAHAQueueService.ts index 75360747..86c7457e 100644 --- a/src/apps/chatwoot/services/ChatWootWAHAQueueService.ts +++ b/src/apps/chatwoot/services/ChatWootWAHAQueueService.ts @@ -7,6 +7,9 @@ import { Queue } from 'bullmq'; import { QueueName } from '../consumers/QueueName'; import { QueueRegistry } from './QueueRegistry'; +import { App } from '@waha/apps/app_sdk/dto/app.dto'; +import { ChatWootAppConfig } from '@waha/apps/chatwoot/dto/config.dto'; +import { ChatWootConfigDefaults } from '@waha/apps/chatwoot/di/DIContainer'; /** * Service for managing ChatWoot queues for WAHA events @@ -63,13 +66,14 @@ export class ChatWootWAHAQueueService { /** * Configure ChatWoot event handling for a session */ - listenEvents(appId: string, session: WhatsappSession): void { - const events = ListenEventsForChatWoot(); + listenEvents(app: App, session: WhatsappSession): void { + const config = ChatWootConfigDefaults(app.config); + const events = ListenEventsForChatWoot(config); for (const event of events) { const obs$ = session.getEventObservable(event); obs$.subscribe(async (payload) => { const data = populateSessionInfo(event, session)(payload); - await this.addJobToQueue(event, data, appId); + await this.addJobToQueue(event, data, app.id); }); } }