From 5dc6aa5660c5b53cdf518facf9fde06ed5b66f50 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Sun, 6 Sep 2020 01:20:50 -0400 Subject: [PATCH] timeouts --- src/bot.js | 31 +++++++++++++++++++++++++++++-- src/index.js | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/bot.js b/src/bot.js index 9170e48..a8488f6 100644 --- a/src/bot.js +++ b/src/bot.js @@ -19,6 +19,7 @@ class OcrccBot { this.config = botConfig this.client = matrix.createClient(this.config.MATRIX_SERVER_URL); this.joinedRooms = []; + this.inactivityTimers = {}; } createLocalStorage() { @@ -178,12 +179,20 @@ class OcrccBot { handleMessageEvent(event) { const content = event.getContent(); + const sender = event.getSender() + const roomId = event.getRoomId(); // do nothing if there's no content if (!content) { return; } + // if it's a chat message and the facilitator has joined, reset the inactivity timeout + const facilitatorId = this.localStorage.getItem(`${roomId}-facilitator`) + if (Boolean(facilitatorId) && sender !== this.config.BOT_USERID) { + this.setInactivityTimeout(roomId) + } + // bot commands if (content.body.startsWith("!bot")) { return this.handleBotCommand(event); @@ -443,7 +452,7 @@ class OcrccBot { const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})` this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); this.inviteFacilitators(room.roomId) - this.setTimeoutforFacilitators(room.roomId) + this.setTimeoutforFacilitator(room.roomId) } } catch(err) { logger.log("error", "ERROR JOINING ROOM => " + err) @@ -580,7 +589,7 @@ class OcrccBot { }) } - setTimeoutforFacilitators(roomId) { + setTimeoutforFacilitator(roomId) { setTimeout(() => { const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`) if (stillWaiting) { @@ -589,6 +598,24 @@ class OcrccBot { }, this.config.MAX_WAIT_TIME) } + setInactivityTimeout(roomId) { + const oldTimeout = this.inactivityTimers[roomId]; + + if (oldTimeout) { + clearTimeout(oldTimeout); + } + + const newTimeout = setTimeout(() => { + this.sendTextMessage( + roomId, + `This chat has been closed due to inactivity.` + ); + this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT) + }, this.config.MAX_INACTIVE) + + this.inactivityTimers[roomId] = newTimeout; + } + async setMessageListeners() { // encrypted messages this.client.on("Event.decrypted", (event, err) => { diff --git a/src/index.js b/src/index.js index a05c188..450e1b6 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ const { CAPTURE_TRANSCRIPTS, CHAT_NOT_AVAILABLE_MESSAGE, MAX_WAIT_TIME, + MAX_INACTIVE, } = process.env; const botConfig = { @@ -32,6 +33,7 @@ const botConfig = { CAPTURE_TRANSCRIPTS, CHAT_NOT_AVAILABLE_MESSAGE, MAX_WAIT_TIME, + MAX_INACTIVE, } import OcrccBot from './bot'