From cdede835afa7f39f7257dfe9ab363d09a63e2362 Mon Sep 17 00:00:00 2001 From: tuyuribr <45042245+tuyuribr@users.noreply.github.com> Date: Sat, 17 Jul 2021 01:05:53 -0300 Subject: [PATCH] Include Calls (#720) * Get Call module * The call event * Update onCall index.d.ts --- index.d.ts | 43 ++++++++++++++++++++++++++ src/Client.js | 21 ++++++++++++- src/structures/Call.js | 68 +++++++++++++++++++++++++++++++++++++++++ src/structures/index.js | 3 +- src/util/Constants.js | 3 +- src/util/Injected.js | 1 + 6 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/structures/Call.js diff --git a/index.d.ts b/index.d.ts index 0a11f90..a30fc76 100644 --- a/index.d.ts +++ b/index.d.ts @@ -227,6 +227,12 @@ declare namespace WAWebJS { qr: string ) => void): this + /** Emitted when a call is received */ + on(event: 'call', listener: ( + /** The call that started */ + call: Call + ) => void): this + /** Emitted when the client has initialized and is ready to receive messages */ on(event: 'ready', listener: () => void): this } @@ -1044,6 +1050,43 @@ declare namespace WAWebJS { /** Order Created At*/ createdAt: number; } + + /** + * Represents a Call on WhatsApp + * + * @example + * Call { + * id: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + * from: '5511999999@c.us', + * timestamp: 1625003709, + * isVideo: false, + * isGroup: false, + * fromMe: false, + * canHandleLocally: false, + * webClientShouldHandle: false, + * participants: [] + * } + */ + export interface Call { + /** Call Id */ + id: string, + /** from */ + from?: string, + /** Unix timestamp for when the call was created*/ + timestamp: number, + /** Is video */ + isVideo: boolean, + /** Is Group */ + isGroup: boolean, + /** Indicates if the call was sent by the current user */ + fromMe: boolean, + /** indicates if the call can be handled in waweb */ + canHandleLocally: boolean, + /** indicates if the call should be handled in waweb */ + webClientShouldHandle: boolean, + /** Object with participants */ + participants: object + } } export = WAWebJS diff --git a/src/Client.js b/src/Client.js index 67955cf..3fb9555 100644 --- a/src/Client.js +++ b/src/Client.js @@ -11,7 +11,7 @@ const { WhatsWebURL, DefaultOptions, Events, WAState } = require('./util/Constan const { ExposeStore, LoadUtils } = require('./util/Injected'); const ChatFactory = require('./factories/ChatFactory'); const ContactFactory = require('./factories/ContactFactory'); -const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification , Label } = require('./structures'); +const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification , Label, Call } = require('./structures'); /** * Starting point for interacting with the WhatsApp Web API * @extends {EventEmitter} @@ -370,6 +370,24 @@ class Client extends EventEmitter { this.emit(Events.BATTERY_CHANGED, { battery, plugged }); }); + await page.exposeFunction('onIncomingCall', (call) => { + /** + * Emitted when a call is received + * @event Client#incoming_call + * @param {object} call + * @param {number} call.id - Call id + * @param {string} call.peerJid - Who called + * @param {boolean} call.isVideo - if is video + * @param {boolean} call.isGroup - if is group + * @param {boolean} call.canHandleLocally - if we can handle in waweb + * @param {boolean} call.outgoing - if is outgoing + * @param {boolean} call.webClientShouldHandle - If Waweb should handle + * @param {object} call.participants - Participants + */ + const cll = new Call(this,call); + this.emit(Events.INCOMING_CALL, cll); + }); + await page.evaluate(() => { window.Store.Msg.on('add', (msg) => { if (msg.isNewMsg) window.onAddMessageEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(window.WWebJS.getMessageModel(msg)); }); @@ -379,6 +397,7 @@ class Client extends EventEmitter { window.Store.Msg.on('remove', (msg) => { if (msg.isNewMsg) window.onRemoveMessageEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); }); window.Store.Conn.on('change:battery', (state) => { window.onBatteryStateChangedEvent(state); }); + window.Store.Call.on('add', (call) => { window.onIncomingCall(call); }); }); /** diff --git a/src/structures/Call.js b/src/structures/Call.js new file mode 100644 index 0000000..1e6644d --- /dev/null +++ b/src/structures/Call.js @@ -0,0 +1,68 @@ +'use strict'; + +const Base = require('./Base'); + +/** + * Represents a Call on WhatsApp + * @extends {Base} + */ +class Call extends Base { + constructor(client, data) { + super(client); + + if (data) this._patch(data); + } + + _patch(data) { + /** + * Call ID + * @type {string} + */ + this.id = data.id; + /** + * From + * @type {string} + */ + this.from = data.peerJid; + /** + * Unix timestamp for when the call was created + * @type {number} + */ + this.timestamp = data.offerTime; + /** + * Is video + * @type {boolean} + */ + this.isVideo = data.isVideo; + /** + * Is Group + * @type {boolean} + */ + this.isGroup = data.isGroup; + /** + * Indicates if the call was sent by the current user + * @type {boolean} + */ + this.fromMe = data.outgoing; + /** + * Indicates if the call can be handled in waweb + * @type {boolean} + */ + this.canHandleLocally = data.canHandleLocally; + /** + * Indicates if the call Should be handled in waweb + * @type {boolean} + */ + this.webClientShouldHandle = data.webClientShouldHandle; + /** + * Object with participants + * @type {object} + */ + this.participants = data.participants; + + return super._patch(data); + } + +} + +module.exports = Call; \ No newline at end of file diff --git a/src/structures/index.js b/src/structures/index.js index b9381c3..bdb393e 100644 --- a/src/structures/index.js +++ b/src/structures/index.js @@ -13,5 +13,6 @@ module.exports = { GroupNotification: require('./GroupNotification'), Label: require('./Label.js'), Order: require('./Order'), - Product: require('./Product') + Product: require('./Product'), + Call: require('./Call') }; \ No newline at end of file diff --git a/src/util/Constants.js b/src/util/Constants.js index 0fc3966..d973290 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -50,7 +50,8 @@ exports.Events = { QR_RECEIVED: 'qr', DISCONNECTED: 'disconnected', STATE_CHANGED: 'change_state', - BATTERY_CHANGED: 'change_battery' + BATTERY_CHANGED: 'change_battery', + INCOMING_CALL: 'incoming_call' }; /** diff --git a/src/util/Injected.js b/src/util/Injected.js index c2a8358..905b59b 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -36,6 +36,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.QueryOrder = window.mR.findModule('queryOrder')[0]; window.Store.QueryProduct = window.mR.findModule('queryProduct')[0]; window.Store.DownloadManager = window.mR.findModule('DownloadManager')[0].default; + window.Store.Call = window.mR.findModule('CallCollection')[0].default; }; exports.LoadUtils = () => {