Skip to content

Commit 6512ef4

Browse files
committed
Фиксы
1 parent 221abe7 commit 6512ef4

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/core.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,34 @@ class BaseClient extends EventEmitter {
1515
#databasePath;
1616
#logLevel;
1717

18-
constructor({ phone, uri, headers, token, sendFakeTelemetry, host, port, proxy, workDir, sessionName, registration, firstName, lastName, deviceId, reconnect, reconnectDelay }) {
18+
constructor({ phone, uri, headers, token, sendFakeTelemetry, host, port, proxy, workDir, sessionName, registration, firstName, lastName, deviceId, reconnect, reconnectDelay } = {}) {
1919
super();
2020

21-
this.#databasePath = path.join(workDir, sessionName);
22-
2321
if (typeof sendFakeTelemetry === "undefined") {
2422
sendFakeTelemetry = true;
2523
}
2624
if (typeof workDir === "undefined") {
2725
workDir = ".";
2826
}
2927
if (typeof sessionName === "undefined") {
30-
sessionName = "session.db";
28+
sessionName = "session.json";
29+
}
30+
if (!fs.existsSync(workDir)) {
31+
fs.mkdirSync(workDir, {
32+
"recursive": true
33+
});
34+
}
35+
this.#databasePath = path.join(workDir, sessionName);
36+
if (!fs.existsSync(this.#databasePath)) {
37+
fs.writeFileSync(this.#databasePath, "{}");
3138
}
39+
3240
if (typeof token === "undefined") {
3341
var database = JSON.parse(fs.readFileSync(this.#databasePath).toString("utf-8"));
3442
token = database.token;
3543
deviceId = database.deviceId;
36-
} else if (typeof deviceId === "undefined") {
44+
}
45+
if (!deviceId) {
3746
deviceId = uuid.v4();
3847
}
3948

@@ -78,11 +87,6 @@ class BaseClient extends EventEmitter {
7887
this._connection = null;
7988
this._seq = 0;
8089
this._users = {};
81-
if (!fs.existsSync(workDir)) {
82-
fs.mkdirSync(workDir, {
83-
"recursive": true
84-
});
85-
}
8690
this.#logLevel = 2;
8791
}
8892

@@ -219,7 +223,7 @@ class BaseClient extends EventEmitter {
219223
"userAgent": this.userAgent
220224
});
221225
data = data.payload;
222-
for (var chat of data.chats) {
226+
for (var chat of (data.chats || [])) {
223227
if (chat.type == "DIALOG") {
224228
this.dialogs.append(new Dialog(chat));
225229
} else if (chat.type == "CHAT") {
@@ -228,7 +232,7 @@ class BaseClient extends EventEmitter {
228232
this.channels.append(new Channel(chat));
229233
}
230234
}
231-
for (var user of data.contacts) {
235+
for (var user of (data.contacts || [])) {
232236
this.contacts.push(new User(user));
233237
}
234238
if (data.profile && data.profile.contact) {
@@ -239,7 +243,7 @@ class BaseClient extends EventEmitter {
239243
}
240244

241245
class MaxClient extends BaseClient {
242-
constructor({ uri, ...options }) {
246+
constructor({ uri, ...options } = {}) {
243247
if (typeof uri === "undefined") {
244248
uri = "wss://ws-api.oneme.ru/websocket";
245249
}
@@ -258,13 +262,15 @@ class MaxClient extends BaseClient {
258262
}
259263
});
260264
this._connection.on("close", (code, reason) => {
265+
this.isConnected = false;
261266
this._log("info", `WebSocket connection closed with error: ${code}, ${reason}; exiting recv loop`);
262267
if (this.reconnect) {
263268
setTimeout(() => this.start(), this.reconnectDelay * 1000);
264269
}
265270
});
266271
return new Promise(res => {
267272
this._connection.on("open", () => {
273+
this.isConnected = true;
268274
this._log("info", "WebSocket connected, starting handshake");
269275
this._handshake().then(() => res());
270276
});
@@ -296,7 +302,7 @@ class MaxClient extends BaseClient {
296302

297303
// TODO
298304
class SocketMaxClient extends BaseClient {
299-
constructor({ host, port, ...options }) {
305+
constructor({ host, port, ...options } = {}) {
300306
if (typeof host === "undefined") {
301307
host = "api.oneme.ru";
302308
}

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare class BaseClient extends EventEmitter {
1111
port?: number = 443
1212
proxy?: any
1313
workDir?: string = "."
14-
sessionName?: string = "session.db"
14+
sessionName?: string = "session.json"
1515
registration?: boolean = false
1616
firstName?: string
1717
lastName?: string

0 commit comments

Comments
 (0)