feat: add or remove labels (#1915)
* add or remove labels * Strings use singlequote * add event chat change labels * Revert "add event chat change labels"
This commit is contained in:
parent
8e8d0c9b3e
commit
8fc6e6098c
2 changed files with 55 additions and 0 deletions
|
|
@ -1332,6 +1332,35 @@ class Client extends EventEmitter {
|
|||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change labels in chats
|
||||
* @param {Array<number|string>} labelIds
|
||||
* @param {Array<string>} chatIds
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async addOrRemoveLabels(labelIds, chatIds) {
|
||||
|
||||
return this.pupPage.evaluate(async (labelIds, chatIds) => {
|
||||
if (['smba', 'smbi'].indexOf(window.Store.Conn.platform) === -1) {
|
||||
throw '[LT01] Only Whatsapp business';
|
||||
}
|
||||
const labels = window.WWebJS.getLabels().filter(e => labelIds.find(l => l == e.id) !== undefined);
|
||||
const chats = window.Store.Chat.filter(e => chatIds.includes(e.id._serialized));
|
||||
|
||||
let actions = labels.map(label => ({id: label.id, type: 'add'}));
|
||||
|
||||
chats.forEach(chat => {
|
||||
(chat.labels || []).forEach(n => {
|
||||
if (!actions.find(e => e.id == n)) {
|
||||
actions.push({id: n, type: 'remove'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return await window.Store.Label.addOrRemoveLabels(actions, chats);
|
||||
}, labelIds, chatIds);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Client;
|
||||
|
|
|
|||
|
|
@ -261,6 +261,15 @@ class Chat extends Base {
|
|||
async getLabels() {
|
||||
return this.client.getChatLabels(this.id._serialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or remove labels to this Chat
|
||||
* @param {Array<number|string>} labelIds
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async changeLabels(labelIds) {
|
||||
return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Chat;
|
||||
|
|
|
|||
Loading…
Reference in a new issue