mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
Added Contact Model (#34)
This commit is contained in:
@@ -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
|
||||
|
||||
16
src/factories/ContactFactory.js
Normal file
16
src/factories/ContactFactory.js
Normal file
@@ -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;
|
||||
18
src/structures/BusinessContact.js
Normal file
18
src/structures/BusinessContact.js
Normal file
@@ -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;
|
||||
35
src/structures/Contact.js
Normal file
35
src/structures/Contact.js
Normal file
@@ -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;
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
13
src/structures/PrivateContact.js
Normal file
13
src/structures/PrivateContact.js
Normal file
@@ -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;
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user