forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.layer.ts
497 lines (456 loc) · 13.1 KB
/
listener.layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import { EventEmitter } from 'events';
import { Page, Browser } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { ExposedFn } from '../helpers/exposed.enum';
import {
Ack,
Chat,
LiveLocation,
Message,
Reaction,
ParticipantEvent,
PicTumb,
ChatStatus
} from '../model';
import { SocketState, SocketStream } from '../model/enum';
import { InterfaceChangeMode } from '../model';
import { InterfaceMode } from '../model/enum/interface-mode';
import { InterfaceState } from '../model/enum/interface-state';
import { ProfileLayer } from './profile.layer';
import { callbackWile } from '../helpers';
declare global {
interface Window {
onMessage: any;
onAnyMessage: any;
onMessageEdit: any;
onMessageDelete: any;
onMessageReaction: any;
onStateChange: any;
onIncomingCall: any;
onAck: any;
onStreamChange: any;
onFilePicThumb: any;
onChatState: any;
onUnreadMessage: any;
onInterfaceChange: any;
onAddedToGroup: any;
func: any;
onLiveLocation: any;
waitNewMessages: any;
onPoll: any;
}
}
const callonMessage = new callbackWile();
const callOnack = new callbackWile();
export class ListenerLayer extends ProfileLayer {
private listenerEmitter = new EventEmitter();
constructor(
public browser: Browser,
public page: Page,
session?: string,
options?: CreateConfig
) {
super(browser, page, session, options);
this.page.on('close', () => {
this.cancelAutoClose();
this.spin('Page Closed', 'fail');
});
}
public async initialize() {
const functions = [...Object.values(ExposedFn)];
for (const func of functions) {
const has = await this.page
.evaluate((func) => typeof window[func] === 'function', func)
.catch(() => false);
if (!has) {
await this.page
.exposeFunction(func, (...args: any) =>
this.listenerEmitter.emit(func, ...args)
)
.catch(() => {});
}
}
await this.addMsg();
await this.page
.evaluate(() => {
window.WAPI.onInterfaceChange((e: any) => {
window.onInterfaceChange(e);
});
window.WAPI.onStreamChange((e: any) => {
window.onStreamChange(e);
});
window.WAPI.onChatState((e: any) => {
window.onChatState(e);
});
window.WAPI.onStateChange((e: any) => {
window.onStateChange(e);
});
window.WAPI.onUnreadMessage((e: any) => {
window.onUnreadMessage(e);
});
window.WAPI.waitNewMessages(false, (data: any) => {
data.forEach((message: any) => {
window.onMessage(message);
});
});
window.WAPI.onAddedToGroup((e: any) => {
window.onAddedToGroup(e);
});
window.WAPI.onAck((e: any) => {
window.onAck(e);
});
window.WAPI.onMessageEdit((e: any) => {
window.onMessageEdit(e);
});
window.WAPI.onMessageDelete((e: any) => {
window.onMessageDelete(e);
});
window.WAPI.onMessageReaction((e: any) => {
window.onMessageReaction(e);
});
window.WAPI.onPoll((e: any) => {
window.onPoll(e);
});
})
.catch(() => {});
}
public async addMsg() {
this.page
.evaluate(() => {
let isHeroEqual = {};
// Install the new message listener (add event)
window.Store.Msg.on('add', async (newMessage) => {
if (!Object.is(isHeroEqual, newMessage)) {
isHeroEqual = newMessage;
if (newMessage && newMessage.isNewMsg) {
const processMessageObj = await window.WAPI.processMessageObj(
newMessage,
true,
false
);
window.onAnyMessage(processMessageObj);
}
}
});
// Install the changed message / deleted message listener (change:body change:caption events)
window.Store.Msg.on(
'change:body change:caption',
async (newMessage) => {
if (newMessage && newMessage.isNewMsg) {
const processMessageObj = await window.WAPI.processMessageObj(
newMessage,
true,
false
);
// Edit or Delete?
if (newMessage.type == 'revoked') {
window.onMessageDelete(processMessageObj);
} else {
window.onMessageEdit(processMessageObj);
}
}
}
);
// Install the message reaction listener
// This is a strange one - seems like the way to do it is to override the WhatsApp WAWebAddonReactionTableMode.reactionTableMode.bulkUpsert function
const module = window.Store.Reaction.reactionTableMode;
const ogMethod = module.bulkUpsert;
module.bulkUpsert = ((...args) => {
if (args[0].length > 0) {
window.onMessageReaction(args[0][0]);
}
return ogMethod(...args);
}).bind(module);
})
.catch(() => {});
}
public async onPoll(fn: (ack: any) => void) {
this.listenerEmitter.on(ExposedFn.onPoll, (e) => {
fn(e);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onPoll, (e) => {
fn(e);
});
}
};
}
/**
* @event Listens to all new messages
* @param fn
*/
public async onAnyMessage(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnAnyMessage, (msg) => {
fn(msg);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnAnyMessage, (msg) => {
fn(msg);
});
}
};
}
/**
* @event Listens for edited message
* @param fn
*/
public async onMessageEdit(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageEdit, (msg) => {
fn(msg);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessageEdit, (msg) => {
fn(msg);
});
}
};
}
/**
* @event Listens for deleted message
* @param fn
*/
public async onMessageDelete(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageDelete, (msg) => {
fn(msg);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessageDelete, (msg) => {
fn(msg);
});
}
};
}
/**
* @event Listens for reactions to messages
* @param fn
*/
public async onMessageReaction(fn: (reaction: Reaction) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageReaction, (reaction) => {
fn(reaction);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessageReaction, (reaction) => {
fn(reaction);
});
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onStateChange(fn: (state: SocketState) => void) {
this.listenerEmitter.on(ExposedFn.onStateChange, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onStateChange, fn);
}
};
}
/**
* @returns Returns chat state
*/
public async onChatState(fn: (state: ChatStatus) => void) {
this.listenerEmitter.on(ExposedFn.onChatState, (state: ChatStatus) => {
fn(state);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onChatState, fn);
}
};
}
////////////////////////////////////////////////////
/**
* @returns Returns the current state of the connection
*/
public async onStreamChange(fn: (state: SocketStream) => void) {
this.listenerEmitter.on(ExposedFn.onStreamChange, (state: SocketStream) => {
fn(state);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onStreamChange, fn);
}
};
}
/**
* @event Listens to interface mode change See {@link InterfaceState} and {@link InterfaceMode} for details
* @returns A disposable object to cancel the event
*/
public async onInterfaceChange(
fn: (state: {
displayInfo: InterfaceState;
mode: InterfaceMode;
info: InterfaceState;
}) => void | InterfaceChangeMode | Promise<any>
) {
this.listenerEmitter.on(ExposedFn.onInterfaceChange, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onInterfaceChange, fn);
}
};
}
//////////////////////////////////////PRO
/**
* @returns Returns new UnreadMessage
*/
public async onUnreadMessage(fn: (unread: Message) => void) {
this.listenerEmitter.on(ExposedFn.onUnreadMessage, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onUnreadMessage, fn);
}
};
}
/**
* @returns Returns new PicThumb
*/
public async onFilePicThumb(fn: (pic: PicTumb) => void) {
this.listenerEmitter.on(ExposedFn.onFilePicThumb, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onFilePicThumb, fn);
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onMessage(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessage, (state: Message) => {
if (!callonMessage.checkObj(state.from, state.id)) {
callonMessage.addObjects(state.from, state.id);
fn(state);
}
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessage, (state: Message) => {
if (!callonMessage.checkObj(state.from, state.id)) {
callonMessage.addObjects(state.from, state.id);
fn(state);
}
});
}
};
}
/**
* @event Listens to messages acknowledgement Changes
* @returns Observable stream of messages
*/
public async onAck(fn: (ack: Ack) => void) {
this.listenerEmitter.on(ExposedFn.onAck, (e: Ack) => {
if (!callOnack.checkObj(e.ack, e.id._serialized)) {
let key = callOnack.getObjKey(e.id._serialized);
if (key) {
callOnack.module[key].id = e.ack;
fn(e);
} else {
callOnack.addObjects(e.ack, e.id._serialized);
fn(e);
}
}
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onAck, (e: Ack) => {
if (!callOnack.checkObj(e.ack, e.id._serialized)) {
let key = callOnack.getObjKey(e.id._serialized);
if (key) {
callOnack.module[key].id = e.ack;
fn(e);
} else {
callOnack.addObjects(e.ack, e.id._serialized);
fn(e);
}
}
});
}
};
}
/**
* @event Listens to live locations from a chat that already has valid live locations
* @param chatId the chat from which you want to subscribes to live location updates
* @param fn callback that takes in a LiveLocation
* @returns boolean, if returns false then there were no valid live locations in the chat of chatId
* @emits <LiveLocation> LiveLocation
*/
public async onLiveLocation(
chatId: string,
fn: (liveLocationChangedEvent: LiveLocation) => void
) {
const method = 'onLiveLocation_' + chatId.replace('_', '').replace('_', '');
return this.page
.exposeFunction(method, (liveLocationChangedEvent: LiveLocation) =>
fn(liveLocationChangedEvent)
)
.then((_) =>
this.page.evaluate(
({ chatId, method }) => {
//@ts-ignore
return WAPI.onLiveLocation(chatId, window[method]);
},
{ chatId, method }
)
);
}
/**
* @event Listens to participants changed
* @param to group id: [email protected]
* @param to callback
* @returns Stream of ParticipantEvent
*/
public async onParticipantsChanged(
groupId: string,
fn: (participantChangedEvent: ParticipantEvent) => void
) {
const method =
'onParticipantsChanged_' + groupId.replace('_', '').replace('_', '');
return this.page
.exposeFunction(method, (participantChangedEvent: ParticipantEvent) =>
fn(participantChangedEvent)
)
.then((_) =>
this.page.evaluate(
({ groupId, method }) => {
//@ts-ignore
WAPI.onParticipantsChanged(groupId, window[method]);
},
{ groupId, method }
)
);
}
/**
* @event Fires callback with Chat object every time the host phone is added to a group.
* @param to callback
* @returns Observable stream of Chats
*/
public async onAddedToGroup(fn: (chat: Chat) => any) {
this.listenerEmitter.on('onAddedToGroup', fn);
return {
dispose: () => {
this.listenerEmitter.off('onAddedToGroup', fn);
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onIncomingCall(fn: (call: any) => any) {
this.listenerEmitter.on('onIncomingCall', fn);
return {
dispose: () => {
this.listenerEmitter.off('onIncomingCall', fn);
}
};
}
}