[+] Expose information about the logged in client (#19)

This commit is contained in:
Pedro S. Lopez
2020-02-01 21:16:03 -04:00
committed by GitHub
parent 88c56b1371
commit ba8edc8d40
4 changed files with 46 additions and 3 deletions

View File

@@ -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 ClientInfo = require('./structures/ClientInfo');
const Message = require('./structures/Message');
/**
@@ -98,6 +99,11 @@ class Client extends EventEmitter {
//Load util functions (serializers, helper functions)
await page.evaluate(LoadUtils);
// Expose client info
this.info = new ClientInfo(this, await page.evaluate(() => {
return Store.Conn.serialize();
}));
// Register events
await page.exposeFunction('onAddMessageEvent', msg => {
if (!msg.isNewMsg) return;
@@ -115,12 +121,12 @@ class Client extends EventEmitter {
this.emit(Events.DISCONNECTED);
this.destroy();
}
})
});
await page.evaluate(() => {
Store.Msg.on('add', onAddMessageEvent);
Store.AppState.on('change:state', onAppStateChangedEvent);
}).catch(err => console.log(err.message));
});
this.pupBrowser = browser;
this.pupPage = page;

View File

@@ -0,0 +1,27 @@
'use strict';
const Base = require('./Base');
/**
* Current connection information
* @extends {Base}
*/
class ClientInfo extends Base {
constructor(client, data) {
super(client);
if(data) this._patch(data);
}
_patch(data) {
this.pushname = data.pushname;
this.me = data.me;
this.phone = data.phone;
this.platform = data.platform;
return super._patch(data);
}
}
module.exports = ClientInfo;