From a64817f56268993053a4a0324a93dd4f50b98ff0 Mon Sep 17 00:00:00 2001 From: Cleber Wilson Date: Mon, 2 Sep 2024 11:10:39 -0300 Subject: [PATCH] added route: `/chat/deleteChat/{instanceName}` --- docs/swagger.yaml | 30 ++++++++++++++-- src/whatsapp/controllers/chat.controller.ts | 4 +++ src/whatsapp/routers/chat.router.ts | 12 ++++++- src/whatsapp/services/whatsapp.service.ts | 40 +++++++++++++++++++-- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index e82f8644..ce14f226 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -295,8 +295,6 @@ paths: summary: Fetch a Single Instance description: | This endpoint is used to fetch details of a specific instance identified by the `{instance}` path parameter. - security: - - apikeyAuth: [] parameters: - name: instance in: path @@ -1461,6 +1459,34 @@ paths: description: Successful response content: application/json: {} + + /chat/deleteChat/{instanceName}: + delete: + tags: + - Chat Controller + summary: Delete Message + description: | + This endpoint is used to delete a chat in the WhatsApp API. Deleting a chat removes all messages and chat history associated with it. + It requires the `instanceName` parameter in the path and the `chatId` parameter in the query. + parameters: + - name: instanceName + in: path + schema: + type: string + required: true + description: '- required' + example: 'codechat_v1' + - name: chatId + in: query + schema: + type: string + required: true + description: ID of the chat to be deleted + responses: + '200': + description: Successful response + content: + application/json: {} /chat/fetchProfilePictureUrl/{instanceName}: post: diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index aabfbf28..57b342bb 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -71,6 +71,10 @@ export class ChatController { return await this.waMonitor.waInstances.get(instanceName).archiveChat(data); } + public async deleteChat({ instanceName }: InstanceDto, data: string) { + return await this.waMonitor.waInstances.get(instanceName).deleteChat(data); + } + public async deleteMessage({ instanceName }: InstanceDto, data: DeleteMessage) { return await this.waMonitor.waInstances.get(instanceName).deleteMessage(data); } diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index 2fa87088..aef19bfd 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -128,7 +128,17 @@ export function ChatRouter(chatController: ChatController, ...guards: RequestHan execute: (instance, data) => chatController.deleteMessage(instance, data), }); - return res.status(HttpStatus.CREATED).json(response); + return res.status(HttpStatus.OK).json(response); + }) + .delete(routerPath('deleteChat'), ...guards, async (req, res) => { + const instance = req.params as unknown as InstanceDto; + const query = req.query as Record; + if (!query?.chatId) { + return res.status(HttpStatus.BAD_REQUEST).json({ message: 'chatId is required' }); + } + const response = await chatController.deleteChat(instance, query?.chatId); + + return res.status(HttpStatus.OK).json(response); }) .post(routerPath('fetchProfilePictureUrl'), ...guards, async (req, res) => { const response = await dataValidate({ diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 4774474e..1421c1ca 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -47,7 +47,6 @@ import makeWASocket, { delay, DisconnectReason, downloadMediaMessage, - fetchLatestBaileysVersion, generateWAMessageFromContent, getContentType, getDevice, @@ -79,7 +78,7 @@ import { } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config'; -import { readFileSync } from 'fs'; +import { lstat, readFileSync } from 'fs'; import { join } from 'path'; import axios, { AxiosError } from 'axios'; import qrcode, { QRCodeToDataURLOptions } from 'qrcode'; @@ -1778,6 +1777,39 @@ export class WAStartupService { } } + public async deleteChat(chatId: string) { + try { + const lastMessage = await this.repository.message.findFirst({ + where: { keyRemoteJid: this.createJid(chatId) }, + orderBy: { messageTimestamp: 'desc' }, + }); + if (!lastMessage) { + throw new Error('Chat not found'); + } + + await this.client.chatModify( + { + delete: true, + lastMessages: [ + { + key: { + id: lastMessage.keyId, + fromMe: lastMessage.keyFromMe, + remoteJid: lastMessage.keyRemoteJid, + }, + messageTimestamp: lastMessage.messageTimestamp, + }, + ], + }, + lastMessage.keyRemoteJid, + ); + + return { deletedAt: new Date(), chatId: lastMessage.keyRemoteJid }; + } catch (error) { + throw new BadRequestException('Error while deleting chat', error?.message); + } + } + public async readMessages(data: ReadMessageIdDto) { const keys: proto.IMessageKey[] = []; try { @@ -1844,7 +1876,7 @@ export class WAStartupService { if (!everyOne) { await this.client.chatModify( { - delete: true, + delete: false as any, lastMessages: [ { key: { @@ -1869,6 +1901,8 @@ export class WAStartupService { remoteJid: message.keyRemoteJid, }, }); + + return { deletedAt: new Date(), message }; } catch (error) { throw new InternalServerErrorException( 'Error while deleting message for everyone',