update bot ot use groups

This commit is contained in:
Sharon Kennedy 2020-03-20 17:43:12 -04:00
parent e48b001486
commit 42866ecfc7

View File

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