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>
This commit is contained in:
parent
e7265a34b9
commit
0fa6ebb66d
5 changed files with 42 additions and 3 deletions
|
|
@ -450,7 +450,7 @@ client.on('message', async msg => {
|
||||||
*/
|
*/
|
||||||
const result = await msg.pin(60); // Will pin a message for 1 minute
|
const result = await msg.pin(60); // Will pin a message for 1 minute
|
||||||
console.log(result); // True if the operation completed successfully, false otherwise
|
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
|
* Get user device count by ID
|
||||||
* Each WaWeb Connection counts as one device, and the phone (if exists) counts as one
|
* 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);
|
let deviceCount = await client.getContactDeviceCount(msg.from);
|
||||||
await msg.reply(`You have *${deviceCount}* devices connected`);
|
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.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
7
index.d.ts
vendored
7
index.d.ts
vendored
|
|
@ -183,7 +183,10 @@ declare namespace WAWebJS {
|
||||||
* So for a non-enterprise user with one WaWeb connection it should return "2"
|
* So for a non-enterprise user with one WaWeb connection it should return "2"
|
||||||
* @param {string} contactId
|
* @param {string} contactId
|
||||||
*/
|
*/
|
||||||
getContactDeviceCount(contactId: string): Promise<Number>
|
getContactDeviceCount(contactId: string): Promise<Number>
|
||||||
|
|
||||||
|
/** Sync history conversation of the Chat */
|
||||||
|
syncHistory(chatId: string): Promise<boolean>
|
||||||
|
|
||||||
/** Changes and returns the archive state of the Chat */
|
/** Changes and returns the archive state of the Chat */
|
||||||
unarchiveChat(chatId: string): Promise<boolean>
|
unarchiveChat(chatId: string): Promise<boolean>
|
||||||
|
|
@ -1440,6 +1443,8 @@ declare namespace WAWebJS {
|
||||||
getLabels: () => Promise<Label[]>,
|
getLabels: () => Promise<Label[]>,
|
||||||
/** Add or remove labels to this Chat */
|
/** Add or remove labels to this Chat */
|
||||||
changeLabels: (labelIds: Array<string | number>) => Promise<void>
|
changeLabels: (labelIds: Array<string | number>) => Promise<void>
|
||||||
|
/** Sync history conversation of the Chat */
|
||||||
|
syncHistory: () => Promise<boolean>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MessageSearchOptions {
|
export interface MessageSearchOptions {
|
||||||
|
|
|
||||||
|
|
@ -1739,6 +1739,24 @@ class Client extends EventEmitter {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync chat history conversation
|
||||||
|
* @param {string} chatId
|
||||||
|
* @return {Promise<boolean>} 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;
|
module.exports = Client;
|
||||||
|
|
|
||||||
|
|
@ -270,6 +270,14 @@ class Chat extends Base {
|
||||||
async changeLabels(labelIds) {
|
async changeLabels(labelIds) {
|
||||||
return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]);
|
return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync chat history conversation
|
||||||
|
* @return {Promise<boolean>} True if operation completed successfully, false otherwise.
|
||||||
|
*/
|
||||||
|
async syncHistory() {
|
||||||
|
return this.client.syncHistory(this.id._serialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Chat;
|
module.exports = Chat;
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,7 @@ exports.ExposeStore = () => {
|
||||||
window.Store.BotSecret = window.require('WAWebBotMessageSecret');
|
window.Store.BotSecret = window.require('WAWebBotMessageSecret');
|
||||||
window.Store.BotProfiles = window.require('WAWebBotProfileCollection');
|
window.Store.BotProfiles = window.require('WAWebBotProfileCollection');
|
||||||
window.Store.DeviceList = window.require('WAWebApiDeviceList');
|
window.Store.DeviceList = window.require('WAWebApiDeviceList');
|
||||||
|
window.Store.HistorySync = window.require('WAWebSendNonMessageDataRequest');
|
||||||
if (window.compareWwebVersions(window.Debug.VERSION, '>=', '2.3000.1014111620'))
|
if (window.compareWwebVersions(window.Debug.VERSION, '>=', '2.3000.1014111620'))
|
||||||
window.Store.AddonReactionTable = window.require('WAWebAddonReactionTableMode').reactionTableMode;
|
window.Store.AddonReactionTable = window.require('WAWebAddonReactionTableMode').reactionTableMode;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue