hopefully fix some connection bugs

This commit is contained in:
Sorunome 2020-03-26 19:05:37 +01:00
parent dc333129da
commit f29399e86e
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
4 changed files with 57 additions and 22 deletions

4
package-lock.json generated
View File

@ -3619,8 +3619,8 @@
} }
}, },
"skype-http": { "skype-http": {
"version": "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#e8a92f8bc4929443bbba9755b6dd1ce90026bf4e", "from": "git://github.com/Sorunome/skype-http.git#3ee89d05d3ff78d7adae17fcba544cdb9b2c8966",
"requires": { "requires": {
"@types/cheerio": "^0.22.12", "@types/cheerio": "^0.22.12",
"@types/escape-html": "0.0.20", "@types/escape-html": "0.0.20",

View File

@ -21,7 +21,7 @@
"mx-puppet-bridge": "0.0.35-1", "mx-puppet-bridge": "0.0.35-1",
"node-emoji": "^1.10.0", "node-emoji": "^1.10.0",
"node-html-parser": "^1.2.13", "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", "tslint": "^5.17.0",
"typescript": "^3.7.4" "typescript": "^3.7.4"
}, },

View File

@ -30,7 +30,6 @@ export class Client extends EventEmitter {
public conversations: Map<string, skypeHttp.Conversation | null> = new Map(); public conversations: Map<string, skypeHttp.Conversation | null> = new Map();
private api: skypeHttp.Api; private api: skypeHttp.Api;
private handledIds: ExpireSet<string>; private handledIds: ExpireSet<string>;
private lastContactsDate: Date = new Date();
private contactsInterval: NodeJS.Timeout | null = null; private contactsInterval: NodeJS.Timeout | null = null;
constructor( constructor(
private loginUsername: string, private loginUsername: string,
@ -76,19 +75,40 @@ export class Client extends EventEmitter {
connectedWithAuth = false; 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) { if (connectedWithAuth) {
let resolved = false; let resolved = false;
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
const TIMEOUT_SUCCESS = 5000; const TIMEOUT_SUCCESS = 5000;
setTimeout(() => { setTimeout(() => {
if (resolved) { if (resolved) {
return; return;
} }
resolved = true; resolved = true;
registerErrorHandler();
resolve(); resolve();
}, TIMEOUT_SUCCESS); }, TIMEOUT_SUCCESS);
this.api.once("error", async () => { this.api.once("error", async () => {
@ -107,12 +127,20 @@ export class Client extends EventEmitter {
verbose: true, verbose: true,
}); });
await this.startupApi(); await this.startupApi();
registerErrorHandler();
resolve(); resolve();
} catch (err) { } catch (err) {
reject(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(); const contacts = await this.api.getContacts();
for (const contact of contacts) { for (const contact of contacts) {
this.contacts.set(contact.mri, contact); this.contacts.set(contact.mri, contact);
} }
this.lastContactsDate = new Date();
const conversations = await this.api.getConversations(); const conversations = await this.api.getConversations();
for (const conversation of conversations) { for (const conversation of conversations) {
this.conversations.set(conversation.id, conversation); this.conversations.set(conversation.id, conversation);
@ -296,11 +318,17 @@ export class Client extends EventEmitter {
} }
private async updateContacts() { private async updateContacts() {
const contacts = await this.api.getContacts(true); log.verbose("Getting contacts diff....");
for (const contact of contacts) { try {
this.contacts.set(contact.mri, contact); const contacts = await this.api.getContacts(true);
this.emit("updateContact", contact); 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();
} }
} }

View File

@ -166,10 +166,17 @@ export class Skype {
log.error("Error while handling presence event", err); log.error("Error while handling presence event", err);
} }
}); });
client.on("updateContact", async (contact: SkypeContact) => { client.on("updateContact", async (oldContact: SkypeContact | null, newContact: SkypeContact) => {
try { try {
const remoteUser = this.getUserParams(puppetId, contact); let update = oldContact === null;
await this.puppet.updateUser(remoteUser); 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) { } catch (err) {
log.error("Error while handling updateContact event", err); log.error("Error while handling updateContact event", err);
} }