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": {
"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",

View File

@ -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"
},

View File

@ -30,7 +30,6 @@ export class Client extends EventEmitter {
public conversations: Map<string, skypeHttp.Conversation | null> = new Map();
private api: skypeHttp.Api;
private handledIds: ExpireSet<string>;
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();
}
}

View File

@ -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);
}