wait for initial sync before adding listeners

This commit is contained in:
Sharon Kennedy 2020-07-01 00:51:29 -04:00
parent 14ed711b6a
commit 404f440f48

View File

@ -81,7 +81,7 @@ class OcrccBot {
}); });
await this.sendMessage(roomId, content); await this.sendMessage(roomId, content);
default: default:
logger.log("error", `ERROR SENDING MESSAGE: ${err}`); logger.log("error", `ERROR SENDING MESSAGE ${content.body}: ${err}`);
break; break;
} }
} }
@ -474,26 +474,31 @@ class OcrccBot {
member.membership === "leave" && member.membership === "leave" &&
member.userId !== this.config.BOT_USERID member.userId !== this.config.BOT_USERID
) { ) {
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`) // ensure bot is still in the room
if (member.userId === facilitatorId) { const roomData = await this.client.getJoinedRooms()
this.sendTextMessage( const joinedRooms = roomData["joined_rooms"]
member.roomId, const isBotInRoom = joinedRooms.includes(member.roomId)
`${member.name} has left the chat.`
);
}
// leave if there is nobody in the room
const room = this.client.getRoom(member.roomId) const room = this.client.getRoom(member.roomId)
if (!room) return if (!room) return
// leave if there is nobody in the room
const memberCount = room.getJoinedMemberCount() const memberCount = room.getJoinedMemberCount()
if (memberCount === 1 && isBotInRoom) { // just the bot left
if (memberCount === 1) { // just the bot left
logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`); logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
this.deleteTranscript(member.userId, member.roomId); this.deleteTranscript(member.userId, member.roomId);
this.localStorage.removeItem(`${member.roomId}-facilitator`) this.localStorage.removeItem(`${member.roomId}-facilitator`)
this.localStorage.removeItem(`${member.roomId}-transcript`) this.localStorage.removeItem(`${member.roomId}-transcript`)
this.client.leave(member.roomId) return this.client.leave(member.roomId)
}
// notify room if the facilitator has left
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
if (isBotInRoom && member.userId === facilitatorId) {
this.sendTextMessage(
member.roomId,
`${member.name} has left the chat.`
);
} }
} }
}) })
@ -551,12 +556,18 @@ class OcrccBot {
}; };
this.client = matrix.createClient(opts); this.client = matrix.createClient(opts);
await this.client.initCrypto()
this.client.startClient({ initialSyncLimit: 0 })
this.client.once('sync', async (state, prevState, data) => {
logger.log("info", `SYNC STATUS: ${state}`)
if (state === 'PREPARED') {
await this.deleteOldDevices() await this.deleteOldDevices()
await this.trackJoinedRooms() await this.trackJoinedRooms()
await this.client.initCrypto()
await this.setMembershipListeners(); await this.setMembershipListeners();
await this.setMessageListeners(); await this.setMessageListeners();
this.client.startClient({ initialSyncLimit: 0 }) }
});
} catch(err) { } catch(err) {
this.handleBotCrash(undefined, err); this.handleBotCrash(undefined, err);
logger.log("error", `ERROR INITIALIZING CLIENT: ${err}`); logger.log("error", `ERROR INITIALIZING CLIENT: ${err}`);