From 0fa6ebb66d7bce62653369aecc469a7a66eca0a3 Mon Sep 17 00:00:00 2001 From: Kang Date: Fri, 20 Sep 2024 16:29:08 +0800 Subject: [PATCH] Sync history conversation of the Chat (#3281) * Sync historical message * Add documentation * Code simplify * feat: add syncHistory method in Chat class * docs: update docs * chore: update examples * fix: handle asynchronous function properly * refactor: include await --------- Co-authored-by: alechkos <93551621+alechkos@users.noreply.github.com> --- example.js | 9 ++++++++- index.d.ts | 7 ++++++- src/Client.js | 20 +++++++++++++++++++- src/structures/Chat.js | 8 ++++++++ src/util/Injected/Store.js | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/example.js b/example.js index 4776bff..3f21a2e 100644 --- a/example.js +++ b/example.js @@ -450,7 +450,7 @@ client.on('message', async msg => { */ const result = await msg.pin(60); // Will pin a message for 1 minute console.log(result); // True if the operation completed successfully, false otherwise - }else if (msg.body === '!howManyConnections'){ + } else if (msg.body === '!howManyConnections') { /** * Get user device count by ID * Each WaWeb Connection counts as one device, and the phone (if exists) counts as one @@ -458,6 +458,13 @@ client.on('message', async msg => { */ let deviceCount = await client.getContactDeviceCount(msg.from); await msg.reply(`You have *${deviceCount}* devices connected`); + } else if (msg.body === '!syncHistory') { + const isSynced = await client.syncHistory(msg.from); + // Or through the Chat object: + // const chat = await client.getChatById(msg.from); + // const isSynced = await chat.syncHistory(); + + await msg.reply(isSynced ? 'Historical chat is syncing..' : 'There is no historical chat to sync.'); } }); diff --git a/index.d.ts b/index.d.ts index 9699230..fd08c43 100644 --- a/index.d.ts +++ b/index.d.ts @@ -183,7 +183,10 @@ declare namespace WAWebJS { * So for a non-enterprise user with one WaWeb connection it should return "2" * @param {string} contactId */ - getContactDeviceCount(contactId: string): Promise + getContactDeviceCount(contactId: string): Promise + + /** Sync history conversation of the Chat */ + syncHistory(chatId: string): Promise /** Changes and returns the archive state of the Chat */ unarchiveChat(chatId: string): Promise @@ -1440,6 +1443,8 @@ declare namespace WAWebJS { getLabels: () => Promise, /** Add or remove labels to this Chat */ changeLabels: (labelIds: Array) => Promise + /** Sync history conversation of the Chat */ + syncHistory: () => Promise } export interface MessageSearchOptions { diff --git a/src/Client.js b/src/Client.js index 25aa73b..d923453 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1739,6 +1739,24 @@ class Client extends EventEmitter { } return 0; } + + /** + * Sync chat history conversation + * @param {string} chatId + * @return {Promise} True if operation completed successfully, false otherwise. + */ + async syncHistory(chatId) { + return await this.pupPage.evaluate(async (chatId) => { + const chat = await window.WWebJS.getChat(chatId); + if (chat.endOfHistoryTransferType === 0) { + await window.Store.HistorySync.sendPeerDataOperationRequest(3, { + chatId: chat.id + }); + return true; + } + return false; + }, chatId); + } } -module.exports = Client; \ No newline at end of file +module.exports = Client; diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 9c09422..2dc1380 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -270,6 +270,14 @@ class Chat extends Base { async changeLabels(labelIds) { return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]); } + + /** + * Sync chat history conversation + * @return {Promise} True if operation completed successfully, false otherwise. + */ + async syncHistory() { + return this.client.syncHistory(this.id._serialized); + } } module.exports = Chat; diff --git a/src/util/Injected/Store.js b/src/util/Injected/Store.js index a028bed..2da5871 100644 --- a/src/util/Injected/Store.js +++ b/src/util/Injected/Store.js @@ -98,6 +98,7 @@ exports.ExposeStore = () => { window.Store.BotSecret = window.require('WAWebBotMessageSecret'); window.Store.BotProfiles = window.require('WAWebBotProfileCollection'); window.Store.DeviceList = window.require('WAWebApiDeviceList'); + window.Store.HistorySync = window.require('WAWebSendNonMessageDataRequest'); if (window.compareWwebVersions(window.Debug.VERSION, '>=', '2.3000.1014111620')) window.Store.AddonReactionTable = window.require('WAWebAddonReactionTableMode').reactionTableMode;