From f29399e86ee60e57e857ce6e704210e61bcba541 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Thu, 26 Mar 2020 19:05:37 +0100 Subject: [PATCH] hopefully fix some connection bugs --- package-lock.json | 4 ++-- package.json | 2 +- src/client.ts | 60 ++++++++++++++++++++++++++++++++++------------- src/skype.ts | 13 +++++++--- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1c23204..37c2e01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3619,8 +3619,8 @@ } }, "skype-http": { - "version": "git://github.com/Sorunome/skype-http.git#e8a92f8bc4929443bbba9755b6dd1ce90026bf4e", - "from": "git://github.com/Sorunome/skype-http.git#e8a92f8bc4929443bbba9755b6dd1ce90026bf4e", + "version": "git://github.com/Sorunome/skype-http.git#3ee89d05d3ff78d7adae17fcba544cdb9b2c8966", + "from": "git://github.com/Sorunome/skype-http.git#3ee89d05d3ff78d7adae17fcba544cdb9b2c8966", "requires": { "@types/cheerio": "^0.22.12", "@types/escape-html": "0.0.20", diff --git a/package.json b/package.json index bf957bf..c938be0 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "mx-puppet-bridge": "0.0.35-1", "node-emoji": "^1.10.0", "node-html-parser": "^1.2.13", - "skype-http": "git://github.com/Sorunome/skype-http#e8a92f8bc4929443bbba9755b6dd1ce90026bf4e", + "skype-http": "git://github.com/Sorunome/skype-http#3ee89d05d3ff78d7adae17fcba544cdb9b2c8966", "tslint": "^5.17.0", "typescript": "^3.7.4" }, diff --git a/src/client.ts b/src/client.ts index 483cf8c..d8418d6 100644 --- a/src/client.ts +++ b/src/client.ts @@ -30,7 +30,6 @@ export class Client extends EventEmitter { public conversations: Map = new Map(); private api: skypeHttp.Api; private handledIds: ExpireSet; - private lastContactsDate: Date = new Date(); private contactsInterval: NodeJS.Timeout | null = null; constructor( private loginUsername: string, @@ -76,19 +75,40 @@ export class Client extends EventEmitter { connectedWithAuth = false; } - await this.startupApi(); + try { + await this.startupApi(); + } catch (err) { + if (!connectedWithAuth) { + throw err; + } + this.api = await skypeHttp.connect({ + credentials: { + username: this.loginUsername, + password: this.password, + }, + verbose: true, + }); + connectedWithAuth = false; + await this.startupApi(); + } + + const registerErrorHandler = () => { + this.api.on("error", (err: Error) => { + log.error("An error occured", err); + this.emit("error", err); + }); + }; - await this.api.listen(); - await this.api.setStatus("Online"); if (connectedWithAuth) { let resolved = false; - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { const TIMEOUT_SUCCESS = 5000; setTimeout(() => { if (resolved) { return; } resolved = true; + registerErrorHandler(); resolve(); }, TIMEOUT_SUCCESS); this.api.once("error", async () => { @@ -107,12 +127,20 @@ export class Client extends EventEmitter { verbose: true, }); await this.startupApi(); + registerErrorHandler(); resolve(); } catch (err) { reject(err); } }); + await this.api.listen(); + }).then(async () => { + await this.api.setStatus("Online"); }); + } else { + registerErrorHandler(); + await this.api.listen(); + await this.api.setStatus("Online"); } } @@ -273,16 +301,10 @@ export class Client extends EventEmitter { } }); - this.api.on("error", (err: Error) => { - log.error("An error occured", err); - this.emit("error", err); - }); - const contacts = await this.api.getContacts(); for (const contact of contacts) { this.contacts.set(contact.mri, contact); } - this.lastContactsDate = new Date(); const conversations = await this.api.getConversations(); for (const conversation of conversations) { this.conversations.set(conversation.id, conversation); @@ -296,11 +318,17 @@ export class Client extends EventEmitter { } private async updateContacts() { - const contacts = await this.api.getContacts(true); - for (const contact of contacts) { - this.contacts.set(contact.mri, contact); - this.emit("updateContact", contact); + log.verbose("Getting contacts diff...."); + try { + const contacts = await this.api.getContacts(true); + const MANY_CONTACTS = 5; + for (const contact of contacts) { + const oldContact = this.contacts.get(contact.mri) || null; + this.contacts.set(contact.mri, contact); + this.emit("updateContact", oldContact, contact); + } + } catch (err) { + log.error("Failed to get contacts diff", err); } - this.lastContactsDate = new Date(); } } diff --git a/src/skype.ts b/src/skype.ts index ab8dc53..df14d8d 100644 --- a/src/skype.ts +++ b/src/skype.ts @@ -166,10 +166,17 @@ export class Skype { log.error("Error while handling presence event", err); } }); - client.on("updateContact", async (contact: SkypeContact) => { + client.on("updateContact", async (oldContact: SkypeContact | null, newContact: SkypeContact) => { try { - const remoteUser = this.getUserParams(puppetId, contact); - await this.puppet.updateUser(remoteUser); + let update = oldContact === null; + const newUser = this.getUserParams(puppetId, newContact); + if (oldContact) { + const oldUser = this.getUserParams(puppetId, oldContact); + update = oldUser.name !== newUser.name || oldUser.avatarUrl !== newUser.avatarUrl; + } + if (update) { + await this.puppet.updateUser(newUser); + } } catch (err) { log.error("Error while handling updateContact event", err); }