From 7324610a8407c28a051ffc5b80f570d817130bd2 Mon Sep 17 00:00:00 2001 From: Fabio Date: Thu, 5 Oct 2023 10:59:32 -0400 Subject: [PATCH] fix(config): autoDownload and UploadS3 in websocket --- README.md | 6 ++++++ src/config.ts | 4 ++++ src/types/ServerOptions.ts | 4 ++++ src/util/createSessionUtil.ts | 8 ++++++-- src/util/functions.ts | 11 +++++++++-- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b21a12a332..4a73558787 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,12 @@ This server use config.ts file to define some options, default values are: // 'event', 'from' ou 'type' to ignore and not send to webhook ignore: [], }, + websocket: { + // Just leave one active, here or on webhook.autoDownload + autoDownload: false, + // Just leave one active, here or on webhook.uploadS3, to avoid duplication in S3 + uploadS3: false, + }, // send data to chatwoot chatwoot: { sendQrCode: true, diff --git a/src/config.ts b/src/config.ts index f2a8aa61ea..1dad5cb557 100644 --- a/src/config.ts +++ b/src/config.ts @@ -24,6 +24,10 @@ export default { onSelfMessage: false, ignore: ['status@broadcast'], }, + websocket: { + autoDownload: false, + uploadS3: false, + }, chatwoot: { sendQrCode: true, sendStatus: true, diff --git a/src/types/ServerOptions.ts b/src/types/ServerOptions.ts index 5a78a3bdba..e0abb78099 100644 --- a/src/types/ServerOptions.ts +++ b/src/types/ServerOptions.ts @@ -25,6 +25,10 @@ export interface ServerOptions { onSelfMessage: boolean; ignore: string[]; }; + websocket: { + autoDownload: boolean; + uploadS3: boolean; + }; archive: { enable: boolean; waitTime: number; diff --git a/src/util/createSessionUtil.ts b/src/util/createSessionUtil.ts index 7c0232cf99..4446dbdcfc 100644 --- a/src/util/createSessionUtil.ts +++ b/src/util/createSessionUtil.ts @@ -19,7 +19,7 @@ import { Request } from 'express'; import { download } from '../controller/sessionController'; import { WhatsAppServer } from '../types/WhatsAppServer'; import chatWootClient from './chatWootClient'; -import { callWebHook, startHelper } from './functions'; +import { autoDownload, callWebHook, startHelper } from './functions'; import { clientsArray, eventEmitter } from './sessionUtil'; import Factory from './tokenStore/factory'; @@ -232,13 +232,17 @@ export default class CreateSessionUtil { }); }); - await client.onAnyMessage((message: any) => { + await client.onAnyMessage(async (message: any) => { message.session = client.session; if (message.type === 'sticker') { download(message, client, req.logger); } + if (req.serverOptions?.websocket?.autoDownload) { + await autoDownload(client, req, message); + } + req.io.emit('received-message', { response: message }); if (req.serverOptions.webhook.onSelfMessage && message.fromMe) callWebHook(client, req, 'onselfmessage', message); diff --git a/src/util/functions.ts b/src/util/functions.ts index 40dcd38382..c2d6ca365d 100644 --- a/src/util/functions.ts +++ b/src/util/functions.ts @@ -38,6 +38,10 @@ if (config.webhook.uploadS3) { mime = config.webhook.uploadS3 ? mimetypes : null; crypto = config.webhook.uploadS3 ? Crypto : null; } +if (config?.websocket?.uploadS3) { + mime = config.websocket.uploadS3 ? mimetypes : null; + crypto = config.websocket.uploadS3 ? Crypto : null; +} export function contactToArray(number: any, isGroup?: boolean) { const localArr: any = []; @@ -143,11 +147,14 @@ export async function callWebHook( } } -async function autoDownload(client: any, req: any, message: any) { +export async function autoDownload(client: any, req: any, message: any) { try { if (message && (message['mimetype'] || message.isMedia || message.isMMS)) { const buffer = await client.decryptFile(message); - if (req.serverOptions.webhook.uploadS3) { + if ( + req.serverOptions.webhook.uploadS3 || + req.serverOptions?.websocket?.uploadS3 + ) { const hashName = crypto.randomBytes(24).toString('hex'); if (