Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
handles not existing RoomPowerLevels event gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodGuyMarco committed Dec 8, 2022
1 parent 360a7bd commit 8de4f27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/components/views/settings/AddPrivilegedUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export const AddPrivilegedUsers: React.FC<AddPrivilegedUsersProps> = ({ 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([]);
Expand Down
10 changes: 9 additions & 1 deletion test/components/views/settings/AddPrivilegedUsers-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,6 +57,13 @@ describe('<AddPrivilegedUsers />', () => {

return member;
};
(room.currentState.getStateEvents as unknown) = (_eventType: string, _stateKey: string) => {
return mkEvent({
type: EventType.RoomPowerLevels,
content: {},
user: 'user_id',
});
};

const getComponent = () =>
<MatrixClientContext.Provider value={mockClient}>
Expand Down

0 comments on commit 8de4f27

Please sign in to comment.