@@ -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
241245class 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
298304class SocketMaxClient extends BaseClient {
299- constructor ( { host, port, ...options } ) {
305+ constructor ( { host, port, ...options } = { } ) {
300306 if ( typeof host === "undefined" ) {
301307 host = "api.oneme.ru" ;
302308 }
0 commit comments