diff --git a/src/Client.js b/src/Client.js index fe07da8..7eb2770 100644 --- a/src/Client.js +++ b/src/Client.js @@ -9,6 +9,7 @@ const Util = require('./util/Util'); const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./util/Constants'); const { ExposeStore, LoadUtils } = require('./util/Injected'); const ChatFactory = require('./factories/ChatFactory'); +const ContactFactory = require('./factories/ContactFactory'); const ClientInfo = require('./structures/ClientInfo'); const Message = require('./structures/Message'); @@ -210,6 +211,29 @@ class Client extends EventEmitter { return ChatFactory.create(this, chat); } + /** + * Get all current contact instances + */ + async getContacts() { + let contacts = await this.pupPage.evaluate(() => { + return window.WWebJS.getContacts(); + }); + + return contacts.map(contact => ContactFactory.create(this, contact)); + } + + /** + * Get contact instance by ID + * @param {string} contactId + */ + async getContactById(contactId) { + let contact = await this.pupPage.evaluate(contactId => { + return window.WWebJS.getContact(contactId); + }, contactId); + + return ContactFactory.create(this, contact); + } + /** * Accepts an invite by code * @param {string} inviteCode diff --git a/src/factories/ContactFactory.js b/src/factories/ContactFactory.js new file mode 100644 index 0000000..a40e90b --- /dev/null +++ b/src/factories/ContactFactory.js @@ -0,0 +1,16 @@ +'use strict'; + +const PrivateContact = require('../structures/PrivateContact'); +const BusinessContact = require('../structures/BusinessContact'); + +class ContactFactory { + static create(client, data) { + if(data.isBusiness) { + return new BusinessContact(client, data); + } + + return new PrivateContact(client, data); + } +} + +module.exports = ContactFactory; \ No newline at end of file diff --git a/src/structures/BusinessContact.js b/src/structures/BusinessContact.js new file mode 100644 index 0000000..2f85c64 --- /dev/null +++ b/src/structures/BusinessContact.js @@ -0,0 +1,18 @@ +'use strict'; + +const Contact = require('./Contact'); + +/** + * Represents a Business Contact on WhatsApp + * @extends {Contact} + */ +class BusinessContact extends Contact { + _patch(data) { + this.businessProfile = data.businessProfile; + + return super._patch(data); + } + +} + +module.exports = BusinessContact; \ No newline at end of file diff --git a/src/structures/Contact.js b/src/structures/Contact.js new file mode 100644 index 0000000..bff983e --- /dev/null +++ b/src/structures/Contact.js @@ -0,0 +1,35 @@ +'use strict'; + +const Base = require('./Base'); + +/** + * Represents a Contact on WhatsApp + * @extends {Base} + */ +class Contact extends Base { + constructor(client, data) { + super(client); + + if(data) this._patch(data); + } + + _patch(data) { + this.id = data.id; + this.isBusiness = data.isBusiness; + this.isEnterprise = data.isEnterprise; + this.labels = data.labels; + this.name = data.name; + this.pushname = data.pushname; + this.sectionHeader = data.sectionHeader; + this.shortName = data.shortName; + this.statusMute = data.statusMute; + this.type = data.type; + this.verifiedLevel = data.verifiedLevel; + this.verifiedName = data.verifiedName; + + return super._patch(data); + } + +} + +module.exports = Contact; \ No newline at end of file diff --git a/src/structures/Message.js b/src/structures/Message.js index c2e086f..f52fb52 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -41,6 +41,13 @@ class Message extends Base { return this.client.getChatById(this._getChatId()); } + /** + * Returns the Contact this message was sent from + */ + getContact() { + return this.client.getContactById(this._getChatId()); + } + /** * Returns the quoted message, if any */ diff --git a/src/structures/PrivateContact.js b/src/structures/PrivateContact.js new file mode 100644 index 0000000..0247967 --- /dev/null +++ b/src/structures/PrivateContact.js @@ -0,0 +1,13 @@ +'use strict'; + +const Contact = require('./Contact'); + +/** + * Represents a Private Contact on WhatsApp + * @extends {Contact} + */ +class PrivateContact extends Contact { + +} + +module.exports = PrivateContact; \ No newline at end of file diff --git a/src/util/Injected.js b/src/util/Injected.js index 1c36cbe..55d530d 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -68,6 +68,27 @@ exports.LoadUtils = () => { return chats.map(chat => window.WWebJS.getChatModel(chat)); }; + window.WWebJS.getContactModel = contact => { + let res = contact.serialize(); + res.isBusiness = contact.isBusiness; + + if (contact.businessProfile) { + res.businessProfile = contact.businessProfile.serialize(); + } + + return res; + }; + + window.WWebJS.getContact = contactId => { + const contact = window.Store.Contact.get(contactId); + return window.WWebJS.getContactModel(contact); + }; + + window.WWebJS.getContacts = () => { + const contacts = window.Store.Contact.models; + return contacts.map(contact => window.WWebJS.getContactModel(contact)); + }; + window.WWebJS.downloadBuffer = (url) => { return new Promise(function(resolve, reject) { let xhr = new XMLHttpRequest();