From 39d97343a5f54a4e8b01f946e34ef5403f0dc93a Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Wed, 22 Apr 2020 15:51:02 -0400 Subject: [PATCH] leave empty rooms --- src/bot.js | 102 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/src/bot.js b/src/bot.js index 2d89cf5..2dec374 100644 --- a/src/bot.js +++ b/src/bot.js @@ -42,21 +42,26 @@ class OcrccBot { async sendMessage(roomId, content) { logger.log("info", `SENDING MESSAGE: ${content.body}`) - return this.client.sendMessage(roomId, content).catch(err => { + try { + await this.client.sendMessage(roomId, content) + } catch(err) { switch (err["name"]) { case "UnknownDeviceError": Object.keys(err.devices).forEach(userId => { Object.keys(err.devices[userId]).map(async deviceId => { - await this.client.setDeviceVerified(userId, deviceId, true); + try { + await this.client.setDeviceVerified(userId, deviceId, true); + } catch(err) { + logger.log("error", `ERROR VERIFYING DEVICE: ${err}`); + } }); }); return this.sendMessage(roomId, content); - break; default: logger.log("error", `ERROR SENDING MESSAGE: ${err}`); break; } - }); + } } inviteUserToRoom(roomId, member) { @@ -98,7 +103,7 @@ class OcrccBot { }); if (chatOffline) { - logger.log('info', "CHAT OFFLINE!") + logger.log('info', "NO FACILITATORS ONLINE") this.sendTextMessage(roomId, this.config.CHAT_OFFLINE_MESSAGE); } @@ -185,6 +190,9 @@ class OcrccBot { const command = content.body.substring("!bot".length).trim(); switch (command) { + case "purge rooms": + this.leaveEmptyRooms(senderId); + break; case "transcript": this.sendTranscript(senderId, roomId); break; @@ -216,6 +224,26 @@ class OcrccBot { } } + async leaveEmptyRooms(senderId) { + try { + const roomData = await this.client.getJoinedRooms() + const joinedRoomsIds = roomData["joined_rooms"] + joinedRoomsIds.forEach(async roomId => { + const room = this.client.getRoom(roomId) + if (room && room.getJoinedMemberCount() === 1) { + try { + logger.log('info', "LEAVING EMPTY ROOM => " + roomId) + await this.client.leave(roomId) + } catch(err) { + logger.log('error', "ERROR LEAVING EMPTY ROOM => " + err) + } + } + }) + } catch(err) { + logger.log("error", `ERROR GETTING JOINED ROOMS: ${err}`); + } + } + async sendTranscript(senderId, roomId) { try { const transcriptFile = this.activeChatrooms[roomId] @@ -278,22 +306,10 @@ class OcrccBot { } } - async leaveOldRooms() { + async trackJoinedRooms() { const roomData = await this.client.getJoinedRooms() - const joinedRoomsIds = roomData["joined_rooms"] - this.joinedRooms = joinedRoomsIds - - logger.log("info", `LEAVING ROOMS ${joinedRoomsIds}`) - - joinedRoomsIds.forEach(async(roomId) => { - if (roomId === this.config.FACILITATOR_ROOM_ID) return; - - try { - await this.client.leave(roomId) - } catch(err) { - logger.log("error", `ERROR LEAVING ROOM => ${err}`) - } - }) + this.joinedRooms = roomData["joined_rooms"] + logger.log("info", "JOINED ROOMS => " + this.joinedRooms) } async setMembershipListeners() { @@ -304,16 +320,17 @@ class OcrccBot { member.userId === this.config.BOT_USERID && !this.joinedRooms.includes(member.roomId) ) { + try { - logger.log("info", "Auto-joining room " + member.roomId); const room = await this.client.joinRoom(member.roomId) + logger.log("info", "AUTO JOINED ROOM" + room.roomId) this.sendTextMessage( this.config.FACILITATOR_ROOM_ID, - `A support seeker requested a chat (Room ID: ${member.roomId})` + `A support seeker requested a chat (Room ID: ${room.roomId})` ); - this.inviteFacilitators(member.roomId) + this.inviteFacilitators(room.roomId) } catch(err) { - logger.log("error", err); + logger.log("error", "ERROR JOINING ROOM =>" + err) } } @@ -369,19 +386,23 @@ class OcrccBot { if ( member.membership === "leave" && - member.userId !== this.config.BOT_USERID && - this.activeChatrooms[member.roomId] && - member.userId === this.activeChatrooms[member.roomId].facilitator + member.userId !== this.config.BOT_USERID ) { - this.sendTextMessage( - member.roomId, - `${member.name} has left the chat.` - ); + if (this.activeChatrooms[member.roomId] && member.userId === this.activeChatrooms[member.roomId].facilitator) { + this.sendTextMessage( + member.roomId, + `${member.name} has left the chat.` + ); + } + + // leave if there is nobody in the room const room = this.client.getRoom(member.roomId) + if (!room) return + const memberCount = room.getJoinedMemberCount() - if (memberCount === 1) { + if (memberCount === 1) { // just the bot logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`); this.client.leave(event.roomId) } @@ -407,6 +428,18 @@ class OcrccBot { }); } + async leaveOldRooms() { + const roomData = await this.client.getJoinedRooms() + + roomData["joined_rooms"].forEach(async roomId => { + try { + await this.client.leave(roomId) + } catch(err) { + logger.log('error', "ERROR LEAVING ROOM => " + err) + } + }) + } + async start() { const localStorage = this.createLocalStorage(); @@ -429,10 +462,9 @@ class OcrccBot { this.client = matrix.createClient(opts); await this.deleteOldDevices() - await this.leaveOldRooms(); + await this.trackJoinedRooms() await this.client.initCrypto() - - this.setMembershipListeners(); + await this.setMembershipListeners(); this.setMessageListeners(); this.client.startClient({ initialSyncLimit: 0 }) } catch(err) {