From 6c882a44cc8eabea1ee4feab81595462dab338c6 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Tue, 1 Dec 2020 13:21:16 -0500 Subject: [PATCH] latest build --- dist/bot.js | 111 ++++++++++++++++++++++++++++++-------------------- dist/setup.js | 2 +- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/dist/bot.js b/dist/bot.js index 6e0f441..9d48e67 100644 --- a/dist/bot.js +++ b/dist/bot.js @@ -112,14 +112,6 @@ class OcrccBot { } } - inviteUserToRoom(roomId, member) { - try { - this.client.invite(roomId, member); - } catch (err) { - this.handleBotCrash(roomId, err); - } - } - kickUserFromRoom(roomId, member) { try { this.client.kick(roomId, member, this.config.kickReason); @@ -130,27 +122,50 @@ class OcrccBot { } } + async inviteFacilitatorIfOnline(roomId, memberId) { + const user = this.client.getUser(memberId); + + if (user && user.presence !== "offline" && memberId !== this.config.botUserId) { + try { + this.client.invite(roomId, memberId); + + _logger.default.log("info", `CHAT INVITATION SENT TO ${memberId} FOR ROOM ${roomId}`); + + return memberId; + } catch (err) { + this.handleBotCrash(roomId, err); + return null; + } + } else { + return null; + } + } + async inviteFacilitators(roomId) { try { this.localStorage.setItem(`${roomId}-waiting`, 'true'); let invitations = []; const roomMembers = await this.client.getJoinedRoomMembers(this.config.facilitatorRoomId); const members = Object.keys(roomMembers["joined"]); - members.forEach(memberId => { - const user = this.client.getUser(memberId); - if (user && user.presence !== "offline" && memberId !== this.config.botUserId) { - invitations.push(memberId); - this.inviteUserToRoom(roomId, memberId); - } - }); + for (const memberId of members) { + const invited = await this.inviteFacilitatorIfOnline(roomId, memberId); + invitations.push(invited); + } - if (invitations.length > 0) { + if (invitations.filter(i => i).length > 0) { this.localStorage.setItem(`${roomId}-invitations`, invitations); } else { - _logger.default.log('info', "NO FACILITATORS ONLINE"); + this.sendBotSignal(roomId, BOT_SIGNAL_CHAT_OFFLINE); // send notification to Support Chat Notifications room - this.sendBotSignal(roomId, BOT_SIGNAL_CHAT_OFFLINE); + const currentDate = new Date(); + const closedTime = currentDate.toLocaleTimeString(); + const roomRef = roomId.split(':')[0]; + const notification = `No facilitators were online, chat closed at ${closedTime} (room ID: ${roomRef})`; + + _logger.default.log('info', `NO FACILITATORS ONLINE, CHAT CLOSED AT ${closedTime} (room ID: ${roomRef})`); + + this.sendTextMessage(this.config.facilitatorRoomId, notification); } } catch (err) { this.handleBotCrash(roomId, err); @@ -182,6 +197,7 @@ class OcrccBot { handleBotCrash(roomId, error) { if (roomId) { this.sendTextMessage(roomId, this.config.botErrorMessage); + this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT); } this.sendTextMessage(this.config.facilitatorRoomId, `${this.config.botDisplayName} ran into an error: ${error}. Please verify that the chat service is working.`); @@ -510,16 +526,31 @@ class OcrccBot { } if (member.membership === "leave" && member.userId !== this.config.botUserId) { + // notify room if the facilitator has left + try { + const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`); + + if (member.userId === facilitatorId) { + this.sendTextMessage(member.roomId, `${member.name} has left the chat.`); // send notification to Support Chat Notifications room + + const currentDate = new Date(); + const chatTime = currentDate.toLocaleTimeString(); + const roomId = member.roomId.split(':')[0]; + const notification = `${member.name} left the chat at ${chatTime} (room ID: ${roomId})`; + await this.sendTextMessage(this.config.facilitatorRoomId, notification); + } + } catch (err) { + _logger.default.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`); + } + const room = this.client.getRoom(member.roomId); if (!room) return; const roomMembers = await room.getJoinedMembers(); // array - - const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.facilitatorRoomId); // object - - const isBotInRoom = Boolean(roomMembers.find(member => member.userId === this.config.botUserId)); // leave if there is nobody in the room + // leave if there is nobody in the room try { const memberCount = roomMembers.length; + const isBotInRoom = Boolean(roomMembers.find(member => member.userId === this.config.botUserId)); if (memberCount === 1 && isBotInRoom) { // just the bot left @@ -533,27 +564,12 @@ class OcrccBot { } } catch (err) { return _logger.default.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`); - } // notify room if the facilitator has left - - - try { - const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`); - - if (isBotInRoom && member.userId === facilitatorId) { - this.sendTextMessage(member.roomId, `${member.name} has left the chat.`); // send notification to Support Chat Notifications room - - const currentDate = new Date(); - const chatTime = currentDate.toLocaleTimeString(); - const roomId = member.roomId.split(':')[0]; - const notification = `${member.name} left the chat at ${chatTime} (room ID: ${roomId})`; - await this.sendTextMessage(this.config.facilitatorRoomId, notification); - } - } catch (err) { - _logger.default.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`); } // send signal to close the chat if there are no facilitators in the room try { + const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.facilitatorRoomId); // object + const facilitators = facilitatorRoomMembers['joined']; let facilitatorInRoom = false; roomMembers.forEach(member => { @@ -579,12 +595,18 @@ class OcrccBot { const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`); if (stillWaiting) { - _logger.default.log("info", `FACILITATOR DID NOT JOIN CHAT WITHIN TIME LIMIT, SENDING SIGNAL TO END CHAT`); - await this.sendTextMessage(roomId, this.config.chatNotAvailableMessage); - this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT); + this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT); // send notification to Support Chat Notifications room + + const currentDate = new Date(); + const closedTime = currentDate.toLocaleTimeString(); + const roomRef = roomId.split(':')[0]; + const notification = `No facilitators joined the chat within the maximum wait time, chat closed at ${closedTime} (room ID: ${roomRef})`; + this.sendTextMessage(this.config.facilitatorRoomId, notification); + + _logger.default.log("info", `NO FACILITATORS JOINED THE CHAT WITHIN THE MAXIMUM WAIT TIME, CHAT CLOSED AT ${closedTime} (room ID: ${roomRef})`); } - }, this.config.maxWaitTime); + }, this.config.maxWaitTime * 1000); // convert seconds to milliseconds } setInactivityTimeout(roomId) { @@ -599,7 +621,8 @@ class OcrccBot { await this.sendTextMessage(roomId, this.config.chatInactiveMessage); this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT); - }, this.config.maxInactiveTime); + }, this.config.maxInactiveTime * 1000); // convert seconds to milliseconds + this.inactivityTimers[roomId] = newTimeout; } diff --git a/dist/setup.js b/dist/setup.js index b355b22..c05f019 100644 --- a/dist/setup.js +++ b/dist/setup.js @@ -24,7 +24,7 @@ const getSettings = async () => { return Object.entries(fields).reduce((settingsObj, [k, v]) => { const [scope, key] = k.split('_'); - if (scope === 'platfrom') { + if (scope === 'platform') { settingsObj[key] = v; }