From 64424694379cc80823a6e3385369178e31195236 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 17 Feb 2019 21:14:31 -0400 Subject: [PATCH] Basic Chat and Message model implementation --- example.js | 2 +- package-lock.json | 2 +- package.json | 2 +- src/client/Client.js | 25 ++++++++++++++++++++----- src/index.js | 7 ++++--- src/models/Base.js | 23 +++++++++++++++++++++++ src/models/Chat.js | 23 +++++++++++++++++++++-- src/models/Message.js | 22 ++++++++++++++++++++-- src/util/Constants.js | 16 ++++++++++++++++ src/util/Injected.js | 14 +++++++++++--- 10 files changed, 118 insertions(+), 18 deletions(-) diff --git a/example.js b/example.js index 562a2a8..02bd03c 100644 --- a/example.js +++ b/example.js @@ -19,7 +19,7 @@ client.on('ready', () => { client.on('message', (msg) => { console.log('MESSAGE RECEIVED', msg); - if (!msg.id.fromMe && msg.body == 'ping') { + if (msg.body == 'ping') { client.sendMessage(msg.from, 'pong'); } }) diff --git a/package-lock.json b/package-lock.json index 46c7c30..63ff206 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "whatsapp-web.js", - "version": "1.0.0", + "version": "0.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 27c0551..904e409 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "whatsapp-web.js", - "version": "1.0.0", + "version": "0.0.1", "description": "Library for interacting with the WhatsApp Web API ", "main": "./src/index.js", "scripts": { diff --git a/src/client/Client.js b/src/client/Client.js index 06c34ab..935ce41 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -4,7 +4,9 @@ const EventEmitter = require('events'); const puppeteer = require('puppeteer'); const Util = require('../util/Util'); const { WhatsWebURL, UserAgent, DefaultOptions, Events } = require('../util/Constants'); -const { ExposeStore, MarkAllRead } = require('../util/Injected'); +const { ExposeStore, LoadExtraProps } = require('../util/Injected'); +const Chat = require('../models/Chat'); +const Message = require('../models/Message') /** * Starting point for interacting with the WhatsApp Web API @@ -39,18 +41,23 @@ class Client extends EventEmitter { // Check Store Injection await page.waitForFunction('window.Store != undefined'); + + // Load extra serialized props + const models = [Chat, Message]; + for (let model of models) { + await page.evaluate(LoadExtraProps, model.WAppModel, model.extraFields); + } await page.exposeFunction('onAddMessageEvent', (msg) => { - this.emit('message', msg); + if (msg.id.fromMe) return; + + this.emit('message', new Message(this, msg)); }); await page.evaluate(() => { Store.Msg.on('add', onAddMessageEvent); }) - // // Mark all chats as read - // await page.evaluate(MarkAllRead); - this.pupBrowser = browser; this.pupPage = page; @@ -67,6 +74,14 @@ class Client extends EventEmitter { }, chatId, message) } + async getChatById(chatId) { + let chat = await this.pupPage.evaluate(chatId => { + return Store.Chat.get(chatId).serialize(); + }, chatId); + + return new Chat(this, chat); + } + } diff --git a/src/index.js b/src/index.js index fc01db6..0aa2c7b 100644 --- a/src/index.js +++ b/src/index.js @@ -2,8 +2,9 @@ module.exports = { Client: require('./client/Client'), - version: require('../package.json').version + version: require('../package.json').version, // Models - -} \ No newline at end of file + Chat: require('./models/Chat'), + Message: require('./models/Message') +}; \ No newline at end of file diff --git a/src/models/Base.js b/src/models/Base.js index b2e3735..4ad9f9c 100644 --- a/src/models/Base.js +++ b/src/models/Base.js @@ -11,6 +11,29 @@ class Base { */ Object.defineProperty(this, 'client', { value: client }); } + + _clone() { + return Object.assign(Object.create(this), this); + } + + _patch(data) { return data; } + + /** + * Name that represents this model in the WhatsApp Web Store + * @readonly + */ + static get WAppModel() { + return this.name; + } + + /** + * Extra fields to add to model serialization + * @readonly + */ + static get extraFields() { + return []; + } + } module.exports = Base; \ No newline at end of file diff --git a/src/models/Chat.js b/src/models/Chat.js index 5e71800..53aa888 100644 --- a/src/models/Chat.js +++ b/src/models/Chat.js @@ -2,8 +2,6 @@ const Base = require('./Base'); -// TODO - /** * Represents a Chat on WhatsApp * @extends {Base} @@ -12,7 +10,28 @@ class Chat extends Base { constructor(client, data) { super(client); + if(data) this._patch(data); + } + _patch(data) { + this.id = data.id; + + this.isGroup = data.isGroup; + this.isReadOnly = data.isReadOnly; + this.name = data.name; + this.unreadCount = data.unreadCount; + this.timestamp = data.t; + } + + sendMessage(message) { + return this.client.sendMessage(this.id._serialized, message); + } + + static get extraFields() { + return [ + 'formattedTitle', + 'isGroup' + ]; } } diff --git a/src/models/Message.js b/src/models/Message.js index 005993b..3cd6752 100644 --- a/src/models/Message.js +++ b/src/models/Message.js @@ -2,8 +2,6 @@ const Base = require('./Base'); -// TODO - /** * Represents a Message on WhatsApp * @extends {Base} @@ -12,7 +10,27 @@ class Message extends Base { constructor(client, data) { super(client); + if(data) this._patch(data); + } + _patch(data) { + this.id = data.id; + this.body = data.body; + this.type = data.type; + this.timestame = data.t; + this.from = data.from; + this.to = data.to; + this.author = data.author; + this.isForwarded = data.isForwarded; + this.broadcast = data.broadcast; + } + + getChat() { + return this.client.getChatById(this.from); + } + + static get WAppModel() { + return 'Msg'; } } diff --git a/src/util/Constants.js b/src/util/Constants.js index 1522835..5860018 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -21,4 +21,20 @@ exports.Events = { READY: 'ready', MESSAGE_CREATE: 'message', QR_RECEIVED: 'qr' +} + +exports.MessageTypes = { + TEXT: 'chat', + AUDIO: 'audio', + VOICE: 'ptt', + IMAGE: 'image', + VIDEO: 'video', + DOCUMENT: 'document', + STICKER: 'sticker' +} + +exports.ChatTypes = { + SOLO: 'solo', + GROUP: 'group', + UNKNOWN: 'unknown' } \ No newline at end of file diff --git a/src/util/Injected.js b/src/util/Injected.js index 4bde774..6e0a6a6 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -4,7 +4,7 @@ * Exposes the internal Store to the WhatsApp Web client */ exports.ExposeStore = () => { - setTimeout(function () { + setTimeout(function() { function getAllModules() { return new Promise((resolve) => { const id = _.uniqueId("fakeModule_"); @@ -38,17 +38,25 @@ exports.ExposeStore = () => { return webpackJsonp([], null, [id]); } - var store_id = 0; + let store_id = 0; function init() { window.Store = _requireById(store_id).default; } - setTimeout(function () { + setTimeout(function() { init(); }, 5000); } +/** + * Adds extra props to the serialization of a model + */ +exports.LoadExtraProps = (model, props) => { + console.log(model, props); + Store[model].models[0].__props = Store[model].models[0].__props.concat(props); +} + exports.MarkAllRead = () => { let Chats = Store.Chat.models;