forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retriever.layer.ts
452 lines (420 loc) · 15.3 KB
/
retriever.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
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN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/m: +NNy. /yyyNMMMMN. dNNo`.yN- d.oNMMMMMMMMd d- mNh-`.`+mN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMN/ . +NMMN- oNMMMMMNdN. dMMMd:`/. ds.dNMMMMMMm::M- dMMNy/dMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMN/ +NMMMN- /yyyyyys d. dMMMMNo` dNy-+ymmmho-+NN- dMMMMMMMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMNyNMMMMN+::::::::::m+/mMMMMMMd: dMMNho///+ymMMN+/mMMMMMMMMNs/hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMNsmMMMMMMMMMMMMMMNNNNMMNNNMMNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { tokenSession } from '../../config/tokenSession.config';
import { Chat, WhatsappProfile } from '../model';
import { SenderLayer } from './sender.layer';
import { checkValuesSender } from '../helpers/layers-interface';
export class RetrieverLayer extends SenderLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
super(page, session, options);
}
/**
* Returns a list of mute and non-mute users
* @param type return type: all, toMute and noMute.
* @returns obj
*/
public async getListMutes(type?: string): Promise<object> {
return await this.page.evaluate(
(type: string) => WAPI.getListMute(type),
type
);
}
/**
* Returns browser session token
* @returns obj [token]
*/
public async getSessionTokenBrowser(
removePath?: boolean
): Promise<tokenSession> {
if (removePath === true) {
await this.page.evaluate(() => {
window['pathSession'] = true;
});
}
return await this.page.evaluate(() => WAPI.getSessionTokenBrowser());
}
/**
* Receive the current theme
* @returns string light or dark
*/
public async getTheme() {
return await this.page.evaluate(() => WAPI.getTheme());
}
/**
* Receive all blocked contacts
* @returns array of [0,1,2,3....]
*/
public async getBlockList() {
return await this.page.evaluate(() => WAPI.getBlockList());
}
/**
* Retrieves all chats
* @returns array of [Chat]
*/
public async getAllChats() {
return await this.page.evaluate(() => {
let chats = WAPI.getAllChats();
return chats;
});
}
/**
* Retrieves all chats new messages
* @returns array of [Chat]
*/
public async getAllChatsNewMsg() {
return await this.page.evaluate(() => {
let chats = WAPI.getAllChatsWithNewMsg();
return chats;
});
}
/**
* Retrieves all chats Contacts
* @returns array of [Chat]
*/
public async getAllChatsContacts() {
return await this.page.evaluate(() => {
let chats = WAPI.getAllChats(),
filter = chats.filter((chat) => chat.kind === 'chat');
return filter;
});
}
/**
* Checks if a number is a valid WA number
* @param contactId, you need to include the @c.us at the end.
* @returns contact detial as promise
*/
public async checkNumberStatus(contactId: string): Promise<WhatsappProfile> {
return new Promise(async (resolve, reject) => {
const result: WhatsappProfile = await this.page.evaluate(
(contactId) => WAPI.checkNumberStatus(contactId),
contactId
);
if (result && result['status'] !== 200) {
reject(result);
} else {
resolve(result);
}
});
}
/**
* Retrieves all chats with messages
* @returns array of [Chat]
*/
public async getAllChatsWithMessages(withNewMessageOnly = false) {
return this.page.evaluate(
(withNewMessageOnly: boolean) =>
WAPI.getAllChatsWithMessages(withNewMessageOnly),
withNewMessageOnly
);
}
/**
* Retrieve all contact new messages
* @returns array of groups
*/
public async getChatContactNewMsg() {
// prettier-ignore
const chats = await this.page.evaluate(() => WAPI.getAllChatsWithNewMsg());
return chats.filter((chat) => chat.kind === 'chat');
}
/**
* Retrieves contact detail object of given contact id
* @param contactId
* @returns contact detial as promise
*/
public async getContact(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.getContact(contactId),
contactId
);
}
/**
* Retrieves all contacts
* @returns array of [Contact]
*/
public async getAllContacts() {
return await this.page.evaluate(() => WAPI.getAllContacts());
}
/**
* Retrieve all groups
* @category Group
* @returns array of groups
*/
public async getAllGroups(withNewMessagesOnly = false): Promise<Chat[]> {
return this.page.evaluate(
async ({ withNewMessagesOnly }) => {
const chats = await WPP.chat.list({
onlyGroups: true,
onlyWithUnreadMessage: withNewMessagesOnly
});
const groups = await Promise.all(
chats.map((c) => WPP.group.ensureGroup(c.id))
);
return groups.map((g) => WAPI._serializeChatObj(g));
},
{ withNewMessagesOnly }
);
}
/**
* Retrieves all chats Transmission list
* @returns array of [Chat]
*/
public async getAllChatsTransmission() {
return await this.page.evaluate(() => {
let chats = WAPI.getAllChats();
return chats.filter((chat) => chat.kind === 'broadcast');
});
}
/**
* Retrieves chat object of given contact id
* @param contactId
* @returns contact detial as promise
*/
public async getChatById(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.getChatById(contactId),
contactId
);
}
/**
* Retrieves chat object of given contact id
* @param contactId
* @returns contact detial as promise
* @deprecated
*/
public async getChat(contactId: string) {
return this.getChatById(contactId);
}
/**
* Retrieves delivery / reads statistics of a given message
* @param msgId
* @returns message detial as promise
*/
public async getMessageInfo(msgId: string) {
return this.page.evaluate(
(msgId) => WAPI.getMessageInfo(msgId),
msgId
);
}
/**
* Retrieves chat picture
* @param chatId Chat id
* @returns url of the chat picture or undefined if there is no picture for the chat.
*/
public async getProfilePicFromServer(chatId: string) {
return this.page.evaluate(
(chatId) => WAPI.getProfilePicFromServer(chatId),
chatId
);
}
/**
* Load more messages in chat object from server. Use this in a while loop
* @param contactId
* @returns contact detial as promise
* @deprecated
*/
public async loadEarlierMessages(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.loadEarlierMessages(contactId),
contactId
);
}
/**
* Retrieves status of given contact
* @param contactId
*/
public async getStatus(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.getStatus(contactId),
contactId
);
}
/**
* Checks if a number is a valid whatsapp number
* @param contactId, you need to include the @c.us at the end.
* @returns contact detial as promise
*/
public async getNumberProfile(contactId: string) {
return new Promise(async (resolve, reject) => {
const typeFunction = 'getNumberProfile';
const type = 'string';
const check = [
{
param: 'contactId',
type: type,
value: contactId,
function: typeFunction,
isUser: true
}
];
const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}
const result = this.page.evaluate(
(contactId: string) => WAPI.getNumberProfile(contactId),
contactId
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
});
}
/**
* check if it's beta
* @returns boolean
*/
public async isBeta() {
return await this.page.evaluate(() => WAPI.isBeta());
}
/**
* Retrieves all undread Messages
* @param includeMe
* @param includeNotifications
* @param useUnreadCount
* @returns any
* @deprecated
*/
public async getUnreadMessages(
includeMe: boolean,
includeNotifications: boolean,
useUnreadCount: boolean
) {
return await this.page.evaluate(
({ includeMe, includeNotifications, useUnreadCount }) =>
WAPI.getUnreadMessages(includeMe, includeNotifications, useUnreadCount),
{ includeMe, includeNotifications, useUnreadCount }
);
}
/**
* Retrieves all unread messages (where ack is -1)
* @returns list of messages
*/
public async getAllUnreadMessages() {
return this.page.evaluate(() => WAPI.getAllUnreadMessages());
}
/**
* Retrieves all new messages (where isNewMsg is true)
* @returns List of messages
* @deprecated Use getAllUnreadMessages
*/
public async getAllNewMessages() {
return await this.page.evaluate(() => WAPI.getAllNewMessages());
}
/**
* Retrieves all messages already loaded in a chat
* For loading every message use loadAndGetAllMessagesInChat
* @param chatId, the chat to get the messages from
* @param includeMe, include my own messages? boolean
* @param includeNotifications
* @returns any
*/
public async getAllMessagesInChat(
chatId: string,
includeMe: boolean,
includeNotifications: boolean
) {
return await this.page.evaluate(
({ chatId, includeMe, includeNotifications }) =>
WAPI.getAllMessagesInChat(chatId, includeMe, includeNotifications),
{ chatId, includeMe, includeNotifications }
);
}
/**
* Loads and Retrieves all Messages in a chat
* @param chatId, the chat to get the messages from
* @param includeMe, include my own messages? boolean
* @param includeNotifications
* @returns any
*/
public async loadAndGetAllMessagesInChat(
chatId: string,
includeMe = false,
includeNotifications = false
) {
return await this.page.evaluate(
({ chatId, includeMe, includeNotifications }) =>
WAPI.loadAndGetAllMessagesInChat(
chatId,
includeMe,
includeNotifications
),
{ chatId, includeMe, includeNotifications }
);
}
/**
* Checks if a CHAT contact is online.
* @param chatId chat id: [email protected]
*/
public async getChatIsOnline(chatId: string): Promise<boolean> {
return await this.page.evaluate(
(chatId: string) => WAPI.getChatIsOnline(chatId),
chatId
);
}
/**
* Retrieves the last seen of a CHAT.
* @param chatId chat id: [email protected]
*/
public async getLastSeen(chatId: string): Promise<number | boolean> {
return await this.page.evaluate(
(chatId: string) => WAPI.getLastSeen(chatId),
chatId
);
}
}