From 8de4f27bcb92f2722c74d0398c921a3f291fc044 Mon Sep 17 00:00:00 2001 From: Marco Bartelt Date: Wed, 7 Dec 2022 11:05:04 +0100 Subject: [PATCH] handles not existing RoomPowerLevels event gracefully --- src/components/views/settings/AddPrivilegedUsers.tsx | 10 ++++++++++ .../views/settings/AddPrivilegedUsers-test.tsx | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/components/views/settings/AddPrivilegedUsers.tsx b/src/components/views/settings/AddPrivilegedUsers.tsx index cf9099c522c..f85699c7413 100644 --- a/src/components/views/settings/AddPrivilegedUsers.tsx +++ b/src/components/views/settings/AddPrivilegedUsers.tsx @@ -52,6 +52,16 @@ export const AddPrivilegedUsers: React.FC = ({ room, de const userIds = getUserIdsFromCompletions(selectedUsers); const powerLevelEvent = room.currentState.getStateEvents(EventType.RoomPowerLevels, ""); + // `RoomPowerLevels` event should exist, but technically it is not guaranteed. + if (powerLevelEvent === null) { + Modal.createDialog(ErrorDialog, { + title: _t("Error"), + description: _t("Failed to change power level"), + }); + + return; + } + try { await client.setPowerLevel(room.roomId, userIds, powerLevel, powerLevelEvent); setSelectedUsers([]); diff --git a/test/components/views/settings/AddPrivilegedUsers-test.tsx b/test/components/views/settings/AddPrivilegedUsers-test.tsx index 21d1b6b0ca2..67258e47df8 100644 --- a/test/components/views/settings/AddPrivilegedUsers-test.tsx +++ b/test/components/views/settings/AddPrivilegedUsers-test.tsx @@ -17,11 +17,12 @@ import React from 'react'; import { act, fireEvent, render, waitFor } from '@testing-library/react'; import userEvent from "@testing-library/user-event"; import { mocked } from "jest-mock"; -import { RoomMember } from "matrix-js-sdk/src/matrix"; +import { RoomMember, EventType } from "matrix-js-sdk/src/matrix"; import { getMockClientWithEventEmitter, makeRoomWithStateEvents, + mkEvent, } from "../../../test-utils"; import MatrixClientContext from '../../../../src/contexts/MatrixClientContext'; import { @@ -56,6 +57,13 @@ describe('', () => { return member; }; + (room.currentState.getStateEvents as unknown) = (_eventType: string, _stateKey: string) => { + return mkEvent({ + type: EventType.RoomPowerLevels, + content: {}, + user: 'user_id', + }); + }; const getComponent = () =>