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 () {