ensure joined user is facilitator before uninviting other facilitators

This commit is contained in:
Sharon Kennedy 2020-06-11 21:53:57 -04:00
parent 4cddeae508
commit f8a1698c56

View File

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