forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 2
/
retriever.layer.ts
389 lines (360 loc) · 9.89 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
import { Page, Browser } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { WhatsappProfile } from '../model';
import { SenderLayer } from './sender.layer';
import { checkValuesSender } from '../helpers/layers-interface';
export class RetrieverLayer extends SenderLayer {
constructor(
public browser: Browser,
public page: Page,
session?: string,
options?: CreateConfig
) {
super(browser, page, session, options);
}
/**
* Return messages by dates!
* @param {string} id contact number id
* @param {string} type
types:
lowerThan: Return all messages after the date informed;
higherThan: Return all messages before the date informed;
equal: Return all messages from the informed date;
full: Return all messages, with two new stringdate parameters, dateNumeric;
* @param {string} date Pass the example date 00/00/0000 or 00-00-0000
* @param {string} date Pass the example time 00:00 24 hours
*/
public async getAllMessagesDate(
chatId: string,
type: string,
idateStart: string,
time: string,
limit: number
) {
return await this.page.evaluate(
({ chatId, type, idateStart, time, limit }) =>
WAPI.getAllMessagesDate(chatId, type, idateStart, time, limit),
{ chatId, type, idateStart, time, limit }
);
}
public async getNewMessageId(chatId: string) {
return new Promise(async (resolve, reject) => {
const typeFunction = 'getNewMessageId';
const type = 'string';
const check = [
{
param: 'text',
type: type,
value: chatId,
function: typeFunction,
isUser: true
}
];
const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}
const result = await this.page.evaluate(
(chatId: string) => WAPI.getNewMessageId(chatId),
chatId
);
if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}
/**
* Returns a list of mute and non-mute users
* @param type return type: all, toMute and noMute.
* @returns obj
*/
public async getListMutes(type?: string) {
return await this.page.evaluate(
(type: string) => WAPI.getListMute(type),
type
);
}
/**
* Returns state connection
* @returns obj
*/
public async getStateConnection() {
return await this.page.evaluate(() => WAPI.getStateConnection());
}
/**
* 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(async () => {
let chats = WAPI.getAllChats(),
filter = (await 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['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());
}
/**
* Retrieves all chats Transmission list
* @returns array of [Chat]
*/
public async getAllChatsTransmission() {
return await this.page.evaluate(async () => {
let chats = WAPI.getAllChats();
return (await 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 await 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 await this.getChatById(contactId);
}
/**
* 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: string) => WAPI.loadEarlierMessages(contactId),
contactId
);
}
/**
* Retrieves status of given contact
* @param contactId
*/
public async getStatus(contactId: string) {
return this.page.evaluate(
(contactId: string) => 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
*/
public async getUnreadMessages(unread?: boolean) {
return await this.page.evaluate(
(unread) => WAPI.getUnreadMessages(unread),
unread
);
}
/**
* 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
);
}
}