Skip to content

Commit

Permalink
feat: block/unblock contacts (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed Oct 26, 2020
1 parent 9b096db commit 20e07c4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ declare namespace WAWebJS {
isUser: boolean,
/** Indicates if the number is registered on WhatsApp */
isWAContact: boolean,
/** Indicates if you have blocked this contact */
isBlocked: boolean,
/** @todo verify labels type. didn't have any documentation */
labels?: string[],
/** The contact's name, as saved by the current user */
Expand All @@ -627,6 +629,11 @@ declare namespace WAWebJS {
* Will return null when getting chat for currently logged in user.
*/
getChat: () => Promise<Chat>,

/** Blocks this contact from WhatsApp */
block: () => Promise<boolean>,
/** Unlocks this contact from WhatsApp */
unblock: () => Promise<boolean>,
}

export interface ContactId {
Expand Down
36 changes: 36 additions & 0 deletions src/structures/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class Contact extends Base {
*/
this.isMyContact = data.isMyContact;

/**
* Indicates if you have blocked this contact
* @type {boolean}
*/
this.isBlocked = data.isBlocked;

return super._patch(data);
}

Expand All @@ -116,6 +122,36 @@ class Contact extends Base {

return await this.client.getChatById(this.id._serialized);
}

/**
* Blocks this contact from WhatsApp
* @returns {Promise<boolean>}
*/
async block() {
if(this.isGroup) return false;

await this.client.pupPage.evaluate(async (contactId) => {
const contact = window.Store.Contact.get(contactId);
await window.Store.BlockContact.blockContact(contact);
}, this.id._serialized);

return true;
}

/**
* Unblocks this contact from WhatsApp
* @returns {Promise<boolean>}
*/
async unblock() {
if(this.isGroup) return false;

await this.client.pupPage.evaluate(async (contactId) => {
const contact = window.Store.Contact.get(contactId);
await window.Store.BlockContact.unblockContact(contact);
}, this.id._serialized);

return true;
}

}

Expand Down
2 changes: 2 additions & 0 deletions src/util/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.UserConstructor = window.mR.findModule((module) => (module.default && module.default.prototype && module.default.prototype.isServer && module.default.prototype.isUser) ? module.default : null)[0].default;
window.Store.Validators = window.mR.findModule('findLinks')[0];
window.Store.WidFactory = window.mR.findModule('createWid')[0];
window.Store.BlockContact = window.mR.findModule('blockContact')[0];
};

exports.LoadUtils = () => {
Expand Down Expand Up @@ -246,6 +247,7 @@ exports.LoadUtils = () => {
res.isGroup = contact.isGroup;
res.isWAContact = contact.isWAContact;
res.isMyContact = contact.isMyContact;
res.isBlocked = contact.isContactBlocked;
res.userid = contact.userid;

return res;
Expand Down

0 comments on commit 20e07c4

Please sign in to comment.