-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
profile.layer.ts
154 lines (143 loc) · 3.7 KB
/
profile.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
import { Page, Browser } from 'puppeteer';
import { HostLayer } from './host.layer';
import * as path from 'path';
const { exec } = require('child_process');
const fs = require('fs');
import {
base64MimeType,
fileToBase64,
downloadFileToBase64,
resizeImg
} from '../helpers';
import { CreateConfig } from '../../config/create-config';
export class ProfileLayer extends HostLayer {
constructor(
public browser: Browser,
public page: Page,
session?: string,
options?: CreateConfig
) {
super(browser, page, session, options);
}
public async clearToken() {
await this.page.evaluate(() => {
localStorage.clear();
window.location.reload();
});
}
/**
* @param time duration of silence
* @param type kind of silence "hours" "minutes" "year"
* To remove the silence, just enter the contact parameter
*/
public sendMute(id: string, time: number, type: string): Promise<object> {
return new Promise(async (resolve, reject) => {
const result = await this.page.evaluate(
(id, time, type) => WAPI.sendMute(id, time, type),
id,
time,
type
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
});
}
/**
* Change the theme
* @param string types "dark" or "light"
*/
public setTheme(type: string) {
return this.page.evaluate((type) => WAPI.setTheme(type), type);
}
/**
* Sets current user profile status
* @param status
*/
public async setProfileStatus(status: string) {
return await this.page.evaluate(
({ status }) => {
WAPI.setMyStatus(status);
},
{ status }
);
}
/**
* Sets the user's current profile photo
* @param name
*/
public async setProfilePic(path: string, to?: string) {
let b64 = await downloadFileToBase64(path, [
'image/gif',
'image/png',
'image/jpg',
'image/jpeg',
'image/webp'
]);
if (!b64) {
b64 = await fileToBase64(path);
}
if (b64) {
const buff = Buffer.from(
b64.replace(/^data:image\/(png|jpe?g|webp);base64,/, ''),
'base64'
);
const mimeInfo = base64MimeType(b64);
if (!mimeInfo || mimeInfo.includes('image')) {
let _webb64_96 = await resizeImg(buff, { width: 96, height: 96 }),
_webb64_640 = await resizeImg(buff, { width: 640, height: 640 });
let obj = { a: _webb64_640, b: _webb64_96 };
return await this.page.evaluate(
({ obj, to }) => WAPI.setProfilePic(obj, to),
{
obj,
to
}
);
} else {
console.log('Not an image, allowed formats png, jpeg and webp');
return false;
}
}
}
/**
* Sets current user profile name
* @param name
*/
public async setProfileName(name: string) {
return this.page.evaluate(
({ name }) => {
WAPI.setMyName(name);
},
{ name }
);
}
public async delProfile() {
if (!this.page.isClosed()) {
await this.page.evaluate(() => WAPI.logout()).catch(() => {});
await this.page.close().catch(() => {});
await this.browser.close().catch(() => {});
const folderSession = path.join(
path.resolve(
process.cwd(),
this.options.mkdirFolderToken,
this.options.folderNameToken,
this.session
)
);
if (fs.existsSync(folderSession)) {
try {
fs.rmSync(folderSession, {
recursive: true,
force: true
});
} catch {
exec(`rm -Rf ${folderSession}`).catch(() => {});
}
}
}
}
}