Basic Chat and Message model implementation

This commit is contained in:
Pedro Lopez 2019-02-17 21:14:31 -04:00
commit 6442469437

View file

@ -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);
}
}