From 43386645907753699461e31f2005dda38604f0aa Mon Sep 17 00:00:00 2001 From: Muhamad Ristiyanto Date: Sat, 30 Oct 2021 10:18:26 +0700 Subject: [PATCH] Add getBlockedContacts method (#770) * add simple method to get blocked ids * Change getBlockedList to getBlockedIds * getBlockedIds to getBlockedContacts * typo * ESLint & other changes * :label: types * fix: no `this.client` * add tests Co-authored-by: Rajeh Taher Co-authored-by: Pedro Lopez Co-authored-by: Pedro S. Lopez --- index.d.ts | 3 +++ src/Client.js | 13 +++++++++++++ tests/client.js | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/index.d.ts b/index.d.ts index 7e7cc69..cc82a26 100644 --- a/index.d.ts +++ b/index.d.ts @@ -48,6 +48,9 @@ declare namespace WAWebJS { /** Logs out the client, closing the current session */ logout(): Promise + /** Get all blocked contacts by host account */ + getBlockedContacts(): Promise + /** Get chat instance by ID */ getChatById(chatId: string): Promise diff --git a/src/Client.js b/src/Client.js index 4394e07..11a5e1e 100644 --- a/src/Client.js +++ b/src/Client.js @@ -946,6 +946,19 @@ class Client extends EventEmitter { return Promise.all(chatIds.map(id => this.getChatById(id))); } + + /** + * Gets all blocked contacts by host account + * @returns {Promise>} + */ + async getBlockedContacts() { + const blockedContacts = await this.pupPage.evaluate(() => { + let chatIds = window.Store.Blocklist.models.map(a => a.id._serialized); + return Promise.all(chatIds.map(id => window.WWebJS.getContact(id))); + }); + + return blockedContacts.map(contact => ContactFactory.create(this.client, contact)); + } } module.exports = Client; diff --git a/tests/client.js b/tests/client.js index 6055c18..36bd425 100644 --- a/tests/client.js +++ b/tests/client.js @@ -384,6 +384,32 @@ END:VCARD`; expect(contact).to.exist; expect(contact).to.be.instanceOf(Contact); }); + + it('can block a contact', async function () { + const contact = await client.getContactById(remoteId); + await contact.block(); + + const refreshedContact = await client.getContactById(remoteId); + expect(refreshedContact.isBlocked).to.eql(true); + }); + + it('can get a list of blocked contacts', async function () { + const blockedContacts = await client.getBlockedContacts(); + expect(blockedContacts.length).to.be.greaterThanOrEqual(1); + + const contact = blockedContacts.find(c => c.id._serialized === remoteId); + expect(contact).to.exist; + expect(contact).to.be.instanceOf(Contact); + + }); + + it('can unblock a contact', async function () { + const contact = await client.getContactById(remoteId); + await contact.unblock(); + + const refreshedContact = await client.getContactById(remoteId); + expect(refreshedContact.isBlocked).to.eql(false); + }); }); describe('Numbers and Users', function () {