From b89f9d6b359f7ea6d219c39fa64a9d79633b70be Mon Sep 17 00:00:00 2001 From: devlikepro Date: Thu, 18 Jul 2024 11:34:40 +0300 Subject: [PATCH] [core] Add chats /archive and /unarchive fixes #434 --- src/api/chats.controller.ts | 23 ++++++++++++++++++++ src/core/abc/session.abc.ts | 8 +++++++ src/core/engines/noweb/session.noweb.core.ts | 21 ++++++++++++++++++ src/core/engines/webjs/session.webjs.core.ts | 10 +++++++++ 4 files changed, 62 insertions(+) diff --git a/src/api/chats.controller.ts b/src/api/chats.controller.ts index 0263f30a..416f1267 100644 --- a/src/api/chats.controller.ts +++ b/src/api/chats.controller.ts @@ -4,6 +4,7 @@ import { Delete, Get, Param, + Post, Put, Query, UsePipes, @@ -98,6 +99,28 @@ class ChatsController { ) { return session.editMessage(chatId, messageId, body); } + + @Post(':chatId/archive') + @SessionApiParam + @ChatIdApiParam + @ApiOperation({ summary: 'Archive the chat' }) + archiveChat( + @SessionParam session: WhatsappSession, + @Param('chatId') chatId: string, + ) { + return session.chatsArchiveChat(chatId); + } + + @Post(':chatId/unarchive') + @SessionApiParam + @ChatIdApiParam + @ApiOperation({ summary: 'Unarchive the chat' }) + unarchiveChat( + @SessionParam session: WhatsappSession, + @Param('chatId') chatId: string, + ) { + return session.chatsUnarchiveChat(chatId); + } } export { ChatsController }; diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index f929279f..7df0a296 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -324,6 +324,14 @@ export abstract class WhatsappSession { throw new NotImplementedByEngineError(); } + public chatsArchiveChat(chatId: string): Promise { + throw new NotImplementedByEngineError(); + } + + public chatsUnarchiveChat(chatId: string): Promise { + throw new NotImplementedByEngineError(); + } + /** * Contacts methods */ diff --git a/src/core/engines/noweb/session.noweb.core.ts b/src/core/engines/noweb/session.noweb.core.ts index a2598b34..e43e4ab1 100644 --- a/src/core/engines/noweb/session.noweb.core.ts +++ b/src/core/engines/noweb/session.noweb.core.ts @@ -11,6 +11,7 @@ import makeWASocket, { isRealMessage, jidNormalizedUser, makeCacheableSignalKeyStore, + MinimalMessage, NewsletterMetadata, normalizeMessageContent, PresenceData, @@ -700,6 +701,26 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { return chats; } + protected async chatsPutArchive( + chatId: string, + archive: boolean, + ): Promise { + const jid = toJID(chatId); + const messages = await this.store.getMessagesByJid(jid, 1); + return await this.sock.chatModify( + { archive: archive, lastMessages: messages }, + jid, + ); + } + + public chatsArchiveChat(chatId: string): Promise { + return this.chatsPutArchive(chatId, true); + } + + public chatsUnarchiveChat(chatId: string): Promise { + return this.chatsPutArchive(chatId, false); + } + /** * Contacts methods */ diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 21b637ea..49b56e70 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -468,6 +468,16 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { return chat.clearMessages(); } + public chatsArchiveChat(chatId: string): Promise { + const id = this.ensureSuffix(chatId); + return this.whatsapp.archiveChat(id); + } + + public chatsUnarchiveChat(chatId: string): Promise { + const id = this.ensureSuffix(chatId); + return this.whatsapp.unarchiveChat(id); + } + /** * Contacts methods */