mirror of
https://github.com/plantroon/mx-puppet-xmpp.git
synced 2024-12-27 17:11:40 +00:00
hopefully fix some connection bugs
This commit is contained in:
parent
dc333129da
commit
f29399e86e
4
package-lock.json
generated
4
package-lock.json
generated
@ -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",
|
||||
|
@ -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"
|
||||
},
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
13
src/skype.ts
13
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user