pull in user settings, fix race conditions on facilitator invitation, add more logging and notifications.

This commit is contained in:
Sharon Kennedy
2020-12-01 13:20:13 -05:00
parent d3747cc654
commit 666f2361c8
4 changed files with 80 additions and 53 deletions

View File

@@ -90,14 +90,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)
@@ -114,9 +106,14 @@ class OcrccBot {
(user.presence !== "offline") &&
memberId !== this.config.botUserId
) {
invitations.push(memberId)
this.inviteUserToRoom(roomId, memberId);
return memberId
try {
this.client.invite(roomId, memberId)
logger.log("info", `CHAT INVITATION SENT TO ${memberId} FOR ROOM ${roomId}`)
return memberId
} catch(err) {
this.handleBotCrash(roomId, err);
return null
}
} else {
return null
}
@@ -137,8 +134,15 @@ class OcrccBot {
if (invitations.filter(i => i).length > 0) {
this.localStorage.setItem(`${roomId}-invitations`, invitations)
} else {
logger.log('info', "NO FACILITATORS ONLINE")
this.sendBotSignal(roomId, BOT_SIGNAL_CHAT_OFFLINE)
// send notification to Support Chat Notifications room
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.log('info', `NO FACILITATORS ONLINE, CHAT CLOSED AT ${closedTime} (room ID: ${roomRef})`)
this.sendTextMessage(this.config.facilitatorRoomId, notification);
}
} catch(err) {
@@ -174,6 +178,7 @@ class OcrccBot {
handleBotCrash(roomId, error) {
if (roomId) {
this.sendTextMessage(roomId, this.config.botErrorMessage);
this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT)
}
this.sendTextMessage(
@@ -539,32 +544,11 @@ class OcrccBot {
member.membership === "leave" &&
member.userId !== this.config.botUserId
) {
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
try {
const memberCount = roomMembers.length
if (memberCount === 1 && isBotInRoom) { // just the bot left
logger.log("info", `LEAVING EMPTY ROOM: ${member.roomId}`);
this.deleteTranscript(member.userId, member.roomId);
this.localStorage.removeItem(`${member.roomId}-facilitator`)
this.localStorage.removeItem(`${member.roomId}-transcript`)
this.localStorage.removeItem(`${member.roomId}-waiting`)
return await this.client.leave(member.roomId)
}
} catch(err) {
return logger.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) {
if (member.userId === facilitatorId) {
this.sendTextMessage(
member.roomId,
`${member.name} has left the chat.`
@@ -580,8 +564,29 @@ class OcrccBot {
logger.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
// 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
logger.log("info", `LEAVING EMPTY ROOM: ${member.roomId}`);
this.deleteTranscript(member.userId, member.roomId);
this.localStorage.removeItem(`${member.roomId}-facilitator`)
this.localStorage.removeItem(`${member.roomId}-transcript`)
this.localStorage.removeItem(`${member.roomId}-waiting`)
return await this.client.leave(member.roomId)
}
} catch(err) {
return logger.log("error", `ERROR LEAVING EMPTY 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;
@@ -607,14 +612,21 @@ class OcrccBot {
setTimeout(async() => {
const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`)
if (stillWaiting) {
logger.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)
// 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.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) {
@@ -631,7 +643,7 @@ class OcrccBot {
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;
}

View File

@@ -20,7 +20,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;
}