forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.layer.ts
237 lines (219 loc) · 6.09 KB
/
controls.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
import { Page, Browser } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { UILayer } from './ui.layer';
import { checkValuesSender } from '../helpers/layers-interface';
export class ControlsLayer extends UILayer {
constructor(
public browser: Browser,
public page: Page,
session?: string,
options?: CreateConfig
) {
super(browser, page, session, options);
}
/**
* Unblock contact
* @param contactId {string} id '[email protected]'
* @returns boolean
*/
public async unblockContact(contactId: string) {
return this.page.evaluate(
(contactId: string) => WAPI.unblockContact(contactId),
contactId
);
}
/**
* Block contact
* @param contactId {string} id '[email protected]'
* @returns boolean
*/
public async blockContact(contactId: string) {
return this.page.evaluate(
(contactId: string) => WAPI.blockContact(contactId),
contactId
);
}
/**
* Mark unread chat
* @param contactId {string} id '[email protected]'
* @returns bollean
*/
public async markUnseenMessage(contactId: string) {
return new Promise(async (resolve, reject) => {
const typeFunction = 'markUnseenMessage';
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 = await this.page.evaluate(
(contactId: string) => WAPI.markUnseenMessage(contactId),
contactId
);
if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}
/**
* Mark chat as read ✔️✔️
* @param contactId {string} id '[email protected]'
* @returns boolean
*/
public async markMarkSeenMessage(contactId: string) {
return new Promise(async (resolve, reject) => {
const typeFunction = 'markMarkSeenMessage';
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 = await this.page.evaluate(
(contactId: string) => WAPI.markMarkSeenMessage(contactId),
contactId
);
if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}
/**
* Deletes the given chat
* @param chatId {string} id '[email protected]'
* @returns boolean
*/
public async deleteChat(chatId: string) {
return await this.page.evaluate(
(chatId) => WAPI.deleteConversation(chatId),
chatId
);
}
/**
* Archive and unarchive chat messages with true or false
* @param chatId {string} id '[email protected]'
* @param option {boolean} true or false
* @returns boolean
*/
public async archiveChat(chatId: string, option: boolean) {
return this.page.evaluate(
({ chatId, option }) => WAPI.archiveChat(chatId, option),
{ chatId, option }
);
}
/**
* Pin and Unpin chat messages with true or false
* @param chatId {string} id '[email protected]'
* @param option {boolean} true or false
* @param nonExistent {boolean} Pin chat, non-existent (optional)
* @returns object
*/
public async pinChat(chatId: string, option: boolean, nonExistent?: boolean) {
return new Promise(async (resolve, reject) => {
const result = await this.page.evaluate(
({ chatId, option, nonExistent }) => {
return WAPI.pinChat(chatId, option, nonExistent);
},
{ chatId, option, nonExistent }
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
});
}
/**
* Deletes all messages of given chat
* @param chatId
* @returns boolean
*/
public async clearChatMessages(chatId: string) {
return this.page.evaluate(
(chatId) => WAPI.clearChatMessages(chatId),
chatId
);
}
/**
* Deletes message of given message id
* @param chatId The chat id from which to delete the message.
* @param messageId The specific message id of the message to be deleted
* @param onlyLocal If it should only delete locally (message remains on the other recipienct's phone). Defaults to false.
*/
public async deleteMessage(
chatId: string,
messageId: string[]
): Promise<Object> {
return new Promise(async (resolve, reject) => {
const typeFunction = 'deleteMessage';
const type = 'string';
const check = [
{
param: 'chatId',
type: type,
value: chatId,
function: typeFunction,
isUser: true
},
{
param: 'messageId',
type: 'object',
value: messageId,
function: typeFunction,
isUser: true
}
];
const validating = checkValuesSender(check);
if (typeof validating === 'object') {
return reject(validating);
}
const result = await this.page.evaluate(
({ chatId, messageId }) => WAPI.deleteMessages(chatId, messageId),
{ chatId, messageId }
);
if (result['erro'] === true) {
return reject(result);
} else {
return resolve(result);
}
});
}
/**
* Archive and unarchive chat messages with true or false
* @param chatId {string} id '[email protected]'
* @param option {boolean} true or false
* @returns boolean
*/
public async setMessagesAdminsOnly(chatId: string, option: boolean) {
return this.page.evaluate(
({ chatId, option }) => WAPI.setMessagesAdminsOnly(chatId, option),
{ chatId, option }
);
}
public async reload() {
await this.page.evaluate(() => {
window.location.reload();
});
}
}