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

Commit

Permalink
Make Jitsi widgets in video rooms immutable (#8244)
Browse files Browse the repository at this point in the history
* Make Jitsi widgets in video rooms immutable

* Test video room creation
  • Loading branch information
robintown authored Apr 7, 2022
1 parent e54eb81 commit 5fbb25c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 33 deletions.
20 changes: 16 additions & 4 deletions src/createRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
[RoomCreateTypeField]: opts.roomType,
};

// In video rooms, allow all users to send video member updates
// Video rooms require custom power levels
if (opts.roomType === RoomType.ElementVideo) {
createOpts.power_level_content_override = {
events: {
// Allow all users to send video member updates
[VIDEO_CHANNEL_MEMBER]: 0,
// Make widgets immutable, even to admins
"im.vector.modular.widgets": 200,
// Annoyingly, we have to reiterate all the defaults here
[EventType.RoomName]: 50,
[EventType.RoomAvatar]: 50,
Expand All @@ -141,6 +144,10 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
[EventType.RoomServerAcl]: 100,
[EventType.RoomEncryption]: 100,
},
users: {
// Temporarily give ourselves the power to set up a widget
[client.getUserId()]: 200,
},
};
}
}
Expand Down Expand Up @@ -259,10 +266,15 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
if (opts.parentSpace) {
return SpaceStore.instance.addRoomToSpace(opts.parentSpace, roomId, [client.getDomain()], opts.suggested);
}
}).then(() => {
// Set up video rooms with a Jitsi widget
}).then(async () => {
if (opts.roomType === RoomType.ElementVideo) {
return addVideoChannel(roomId, createOpts.name);
// Set up video rooms with a Jitsi widget
await addVideoChannel(roomId, createOpts.name);

// Reset our power level back to admin so that the widget becomes immutable
const room = client.getRoom(roomId);
const plEvent = room?.currentState.getStateEvents(EventType.RoomPowerLevels, "");
await client.setPowerLevel(roomId, client.getUserId(), 100, plEvent);
}
}).then(function() {
// NB createRoom doesn't block on the client seeing the echo that the
Expand Down
29 changes: 0 additions & 29 deletions test/createRoom-test.js

This file was deleted.

100 changes: 100 additions & 0 deletions test/createRoom-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { mocked } from "jest-mock";
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { IDevice } from "matrix-js-sdk/src/crypto/deviceinfo";
import { RoomType } from "matrix-js-sdk/src/@types/event";

import { stubClient, setupAsyncStoreWithClient } from "./test-utils";
import { MatrixClientPeg } from "../src/MatrixClientPeg";
import WidgetStore from "../src/stores/WidgetStore";
import WidgetUtils from "../src/utils/WidgetUtils";
import { VIDEO_CHANNEL, VIDEO_CHANNEL_MEMBER } from "../src/utils/VideoChannelUtils";
import createRoom, { canEncryptToAllUsers } from '../src/createRoom';

describe("createRoom", () => {
let client: MatrixClient;
beforeEach(() => {
stubClient();
client = MatrixClientPeg.get();
});

it("sets up video rooms correctly", async () => {
setupAsyncStoreWithClient(WidgetStore.instance, client);
jest.spyOn(WidgetUtils, "waitForRoomWidget").mockResolvedValue();

const userId = client.getUserId();
const roomId = await createRoom({ roomType: RoomType.ElementVideo });

const [[{
power_level_content_override: {
users: {
[userId]: userPower,
},
events: {
"im.vector.modular.widgets": widgetPower,
[VIDEO_CHANNEL_MEMBER]: videoMemberPower,
},
},
}]] = mocked(client.createRoom).mock.calls as any;
const [[widgetRoomId, widgetStateKey, , widgetId]] = mocked(client.sendStateEvent).mock.calls;

// We should have had enough power to be able to set up the Jitsi widget
expect(userPower).toBeGreaterThanOrEqual(widgetPower);
// and should have actually set it up
expect(widgetRoomId).toEqual(roomId);
expect(widgetStateKey).toEqual("im.vector.modular.widgets");
expect(widgetId).toEqual(VIDEO_CHANNEL);

// All members should be able to update their connected devices
expect(videoMemberPower).toEqual(0);
// Jitsi widget should be immutable for admins
expect(widgetPower).toBeGreaterThan(100);
// and we should have been reset back to admin
expect(client.setPowerLevel).toHaveBeenCalledWith(roomId, userId, 100, undefined);
});
});

describe("canEncryptToAllUsers", () => {
const trueUser = {
"@goodUser:localhost": {
"DEV1": {} as unknown as IDevice,
"DEV2": {} as unknown as IDevice,
},
};
const falseUser = {
"@badUser:localhost": {},
};

let client: MatrixClient;
beforeEach(() => {
stubClient();
client = MatrixClientPeg.get();
});

it("returns true if all devices have crypto", async () => {
mocked(client.downloadKeys).mockResolvedValue(trueUser);
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost"]);
expect(response).toBe(true);
});

it("returns false if not all users have crypto", async () => {
mocked(client.downloadKeys).mockResolvedValue({ ...trueUser, ...falseUser });
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost", "@badUser:localhost"]);
expect(response).toBe(false);
});
});
3 changes: 3 additions & 0 deletions test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export function createTestClient(): MatrixClient {
getRoomHierarchy: jest.fn().mockReturnValue({
rooms: [],
}),
createRoom: jest.fn().mockResolvedValue({ room_id: "!1:example.org" }),
setPowerLevel: jest.fn().mockResolvedValue(undefined),

// Used by various internal bits we aren't concerned with (yet)
sessionStore: {
Expand Down Expand Up @@ -135,6 +137,7 @@ export function createTestClient(): MatrixClient {
setPushRuleActions: jest.fn().mockResolvedValue(undefined),
relations: jest.fn().mockRejectedValue(undefined),
isCryptoEnabled: jest.fn().mockReturnValue(false),
downloadKeys: jest.fn(),
fetchRoomEvent: jest.fn(),
} as unknown as MatrixClient;
}
Expand Down

0 comments on commit 5fbb25c

Please sign in to comment.