diff --git a/src/bot.js b/src/bot.js index 0e7c317..529853f 100644 --- a/src/bot.js +++ b/src/bot.js @@ -106,7 +106,7 @@ class OcrccBot { async inviteFacilitators(roomId) { this.localStorage.setItem(`${roomId}-waiting`, 'true') - let chatOffline = true; + let invitations = [] try { const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID) @@ -116,15 +116,17 @@ class OcrccBot { const user = this.client.getUser(memberId); if ( user && - (user.presence === "online") && + (user.presence !== "offline") && memberId !== this.config.BOT_USERID ) { - chatOffline = false; + invitations.push(memberId) this.inviteUserToRoom(roomId, memberId); } }); - if (chatOffline) { + if (invitations.length > 0) { + this.localStorage.setItem(`${roomId}-invitations`, invitations) + } else { logger.log('info', "NO FACILITATORS ONLINE") this.sendNotice(roomId, "CHAT_OFFLINE") } @@ -388,61 +390,78 @@ class OcrccBot { try { 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: ${room.roomId})` - ); + const currentDate = new Date() + const chatDate = currentDate.toLocaleDateString() + const chatTime = currentDate.toLocaleTimeString() + const roomId = room.roomId.split(':')[0] + const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})` + this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); this.inviteFacilitators(room.roomId) } catch(err) { logger.log("error", "ERROR JOINING ROOM => " + err) } } - // When a facilitator joins a support session, make them a moderator - // revoke the other invitations if ( member.membership === "join" && member.userId !== this.config.BOT_USERID && this.localStorage.getItem(`${member.roomId}-waiting`) ) { - this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId) - const event = { - getType: () => { - return "m.room.power_levels"; - }, - getContent: () => { - return { - users: { - [this.config.BOT_USERID]: 100, - [member.userId]: 50 - } - }; - } - }; - this.client.setPowerLevel(member.roomId, member.userId, 50, event); - this.sendTextMessage( - member.roomId, - `${member.name} has joined the chat.` - ); - this.sendTextMessage( - this.config.FACILITATOR_ROOM_ID, - `${member.name} joined the chat (Room ID: ${member.roomId})` - ); - this.uninviteFacilitators(member.roomId); - if (this.config.CAPTURE_TRANSCRIPTS) { - const currentDate = new Date(); - const dateOpts = { - year: "numeric", - month: "short", - day: "numeric" + // make sure it's a facilitator joining + const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID) + const members = Object.keys(roomMembers["joined"]); + const isFacilitator = members.includes(member.userId) + + if (isFacilitator) { + // made facilitator a moderator in the room + this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId) + const event = { + getType: () => { + return "m.room.power_levels"; + }, + getContent: () => { + return { + users: { + [this.config.BOT_USERID]: 100, + [member.userId]: 50 + } + }; + } }; - const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts); - const chatTime = currentDate.toLocaleTimeString("en-GB", { - timeZone: "America/New_York" - }); - const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`; - const filepath = path.resolve(path.join("transcripts", filename)); - this.localStorage.setItem(`${member.roomId}-transcript`, filepath) + this.client.setPowerLevel(member.roomId, member.userId, 50, event); + + // 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} joined the chat at ${chatTime} (room ID: ${roomId})` + this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); + + // send notification to chat room + this.sendTextMessage( + member.roomId, + `${member.name} has joined the chat.` + ); + + // revoke the other invitations + this.uninviteFacilitators(member.roomId); + + // set transcript file + if (this.config.CAPTURE_TRANSCRIPTS) { + const currentDate = new Date(); + const dateOpts = { + year: "numeric", + month: "short", + day: "numeric" + }; + const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts); + const chatTime = currentDate.toLocaleTimeString("en-GB", { + timeZone: "America/New_York" + }); + const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`; + const filepath = path.resolve(path.join("transcripts", filename)); + this.localStorage.setItem(`${member.roomId}-transcript`, filepath) + } } } @@ -472,7 +491,7 @@ class OcrccBot { this.client.leave(member.roomId) } } - }); + }) } async setMessageListeners() {