diff --git a/src/bot.js b/src/bot.js index 8fa37cd..664b182 100644 --- a/src/bot.js +++ b/src/bot.js @@ -18,7 +18,7 @@ const MAX_RETRIES = 3; class OcrccBot { constructor() { this.awaitingFacilitator = {}; - this.client = matrix.createClient(process.env.MATRIX_SERVER_URL); + this.client = null; this.joinedRooms = []; this.activeChatrooms = {}; } @@ -150,7 +150,7 @@ class OcrccBot { }) .catch(err => { this.handleBotCrash(roomId, err); - logger.log("error", `ERROR GETTING ROOM MEMBERS: ${err}`); + logger.log("error", `ERROR GETTING FACILITATORS: ${err}`); }); } @@ -281,15 +281,14 @@ class OcrccBot { } deleteOldDevices() { - const doDelete = (oldDevices, auth=null, retries=0) => { - if (retries > MAX_RETRIES) { - throw new Error("Exceeded max retries deleting old devices") - } - logger.log("info", `ATTEMPTING TO DELETE OLD DEVICES: ${oldDevices}`); - this.client.deleteMultipleDevices(oldDevices, auth) - .then(() => logger.log("info", "DELETED OLD DEVICES")) - .catch(err => { - if (err['errcode'] === undefined && err['data']) { + return this.client.getDevices().then(data => { + const currentDeviceId = this.client.getDeviceId(); + const allDeviceIds = data.devices.map(d => d.device_id); + const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId); + logger.log("info", `DELETING OLD DEVICES: ${oldDevices}`); + this.client + .deleteMultipleDevices(oldDevices) + .catch(err => { const auth = { session: err.data.session, type: "m.login.password", @@ -297,36 +296,25 @@ class OcrccBot { identifier: { type: "m.id.user", user: process.env.BOT_USERID }, password: process.env.BOT_PASSWORD }; - - doDelete(oldDevices, auth) - } else if (err['errcode'] === 'M_LIMIT_EXCEEDED') { - const retryCount = retries + 1 - const delay = err['retry_after_ms'] ? err['retry_after_ms'] : retryCount * 1000 - logger.log("error", `RATE LIMIT EXCEEDED, RETRYING IN ${delay} MS`); - setTimeout(() => { - doDelete(oldDevices, auth, retryCount) - }, delay) - } else { - logger.log("error", `ERROR DELETING OLD DEVICES ON RETRY ${retries}: ${JSON.stringify(err)}`) - doDelete(oldDevices, auth, retries + 1) - } - }) - } - - return this.client.getDevices() - .then(data => { - const currentDeviceId = this.client.getDeviceId(); - const allDeviceIds = data.devices.map(d => d.device_id); - const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId); - doDelete(oldDevices) - }) - .catch(err => { - this.handleBotCrash(undefined, err); - logger.log( - "error", - `ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}` - ) + this.client + .deleteMultipleDevices(oldDevices, auth) + .then(() => logger.log("info", "DELETED OLD DEVICES")) + .catch(err => { + if (err.errcode === "M_LIMIT_EXCEEDED") { + const delay = err.retry_after_ms || 2000 + logger.log("info", `RETRYING DELETE OLD DEVICES: ${oldDevices}`); + setTimeout(() => { + this.client.deleteMultipleDevices(oldDevices) + }, delay) + } else { + logger.log( + "error", + `ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}` + ) + } + }); }); + }); } setMembershipListeners() { @@ -430,8 +418,9 @@ class OcrccBot { start() { const localStorage = this.createLocalStorage(); + const tmpClient = matrix.createClient(process.env.MATRIX_SERVER_URL); - this.client + tmpClient .login("m.login.password", { user: process.env.BOT_USERNAME, password: process.env.BOT_PASSWORD, @@ -440,6 +429,7 @@ class OcrccBot { .then(data => { const accessToken = data.access_token; const deviceId = data.device_id; + logger.log('info', `LOGIN DATA ==> ${JSON.stringify(data)}`) // create new client with full options @@ -456,7 +446,9 @@ class OcrccBot { .catch(err => { logger.log("error", `ERROR WITH LOGIN: ${err}`); }) - .then(() => this.deleteOldDevices()) + .then(() => { + this.deleteOldDevices() + }) .then(() => this.client.initCrypto()) .catch(err => logger.log("error", `ERROR STARTING CRYPTO: ${err}`)) .then(() =>