diff --git a/__mocks__/matrix-js-sdk.js b/__mocks__/matrix-js-sdk.js index 7412779..508557e 100644 --- a/__mocks__/matrix-js-sdk.js +++ b/__mocks__/matrix-js-sdk.js @@ -151,7 +151,7 @@ export const mockGetJoinedRoomMembers = jest.fn(() => { export const mockGetGroupUsers = jest.fn(() => { return Promise.resolve({ - chunk: { user_id: 'user_id_1', user_id: 'user_id_2' } + chunk: [{ user_id: 'user_id_1'}, { user_id: 'user_id_2' }] }) }); diff --git a/src/bot.js b/src/bot.js index f4f6f3a..b044e5f 100644 --- a/src/bot.js +++ b/src/bot.js @@ -14,7 +14,6 @@ import logger from "./logger"; class OcrccBot { constructor(botConfig) { this.config = botConfig - this.awaitingFacilitator = {}; this.client = matrix.createClient(this.config.MATRIX_SERVER_URL); this.joinedRooms = []; } @@ -146,11 +145,15 @@ class OcrccBot { const groupUsers = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID) const roomMembers = await this.client.getJoinedRoomMembers(roomId) - const membersIds = Object.keys(roomMembers["joined"]); - const facilitatorsIds = groupUsers.chunk.map(f => f.user_id); + const roomMemberIds = Object.keys(roomMembers["joined"]); + const groupMemberIds = groupUsers["chunk"] + + if (!roomMemberIds || !groupMemberIds) return; + + const facilitatorsIds = groupMemberIds.map(f => f.user_id); facilitatorsIds.forEach(f => { - if (!membersIds.includes(f)) { + if (!roomMemberIds.includes(f)) { this.kickUserFromRoom(roomId, f); } }); diff --git a/src/bot.test.js b/src/bot.test.js index d8b5bc9..17838c4 100644 --- a/src/bot.test.js +++ b/src/bot.test.js @@ -37,6 +37,39 @@ import { import OcrccBot from './bot' +const ENCRYPTION_CONFIG = { algorithm: "m.megolm.v1.aes-sha2" }; +const KICK_REASON = "A facilitator has already joined this chat."; +const BOT_ERROR_MESSAGE = + "Something went wrong on our end, please restart the chat and try again."; +const MAX_RETRIES = 3; +const { + MATRIX_SERVER_URL, + BOT_USERNAME, + BOT_USERID, + BOT_PASSWORD, + BOT_DISPLAY_NAME, + FACILITATOR_GROUP_ID, + FACILITATOR_ROOM_ID, + CHAT_OFFLINE_MESSAGE, + CAPTURE_TRANSCRIPTS +} = process.env; + +const botConfig = { + ENCRYPTION_CONFIG, + KICK_REASON, + BOT_ERROR_MESSAGE, + MAX_RETRIES, + MATRIX_SERVER_URL, + BOT_USERNAME, + BOT_USERID, + BOT_PASSWORD, + BOT_DISPLAY_NAME, + FACILITATOR_GROUP_ID, + FACILITATOR_ROOM_ID, + CHAT_OFFLINE_MESSAGE, + CAPTURE_TRANSCRIPTS +} + const mockAppendFileSync = jest.fn() fs.appendFileSync = mockAppendFileSync @@ -71,22 +104,21 @@ describe('OcrccBot', () => { mockGetGroupUsers.mockClear() }) + test('constructor should inititialize class variables', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) expect(bot.joinedRooms).toEqual([]) - expect(bot.awaitingFacilitator).toEqual({}) - expect(bot.activeChatrooms).toEqual({}) }) test('#createLocalStorage should have correct storage location', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) const localStorage = bot.createLocalStorage() const localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", `matrix-chatbot-${process.env.BOT_USERNAME}`)); expect(localStorage._location).toBe(localStoragePath) }) test('#sendMessage should send a text message', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -108,7 +140,7 @@ describe('OcrccBot', () => { }) test('#inviteUserToRoom should add member to room and retry on rate limit error', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -123,7 +155,7 @@ describe('OcrccBot', () => { }) test('#kickUserFromRoom should remove member from room and retry on rate limit error', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -138,7 +170,7 @@ describe('OcrccBot', () => { }) test('#inviteFacilitators should invite all members from Facilitator room', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -161,7 +193,7 @@ describe('OcrccBot', () => { }) test('#uninviteFacilitators should remove all members that have not accepted the invite', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -184,7 +216,7 @@ describe('OcrccBot', () => { }) test('#handleBotCrash should notify rooms', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -203,10 +235,10 @@ describe('OcrccBot', () => { }) test('#writeToTranscript should parse event and write to transcript file', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() - bot.activeChatrooms['test_room_id'] = { transcriptFile: '__mocks__/test_transcript.txt' } + bot.localStorage.setItem(`test_room_id-transcript`, '__mocks__/test_transcript.txt') waitForExpect(() => { expect(mockStartClient).toHaveBeenCalled() @@ -227,7 +259,7 @@ describe('OcrccBot', () => { }) test('#deleteOldDevices should delete old sessions', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => { @@ -252,7 +284,7 @@ describe('OcrccBot', () => { // TODO test listeners for membership events and message events test('#start should start bot and set up listeners', () => { - const bot = new OcrccBot() + const bot = new OcrccBot(botConfig) bot.start() waitForExpect(() => {