Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in useIsEncrypted #28282

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/hooks/useIsEncrypted.ts
Original file line number Diff line number Diff line change
@@ -6,24 +6,25 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/

import { useCallback, useState } from "react";
import { MatrixClient, MatrixEvent, Room, RoomStateEvent, EventType } from "matrix-js-sdk/src/matrix";
import { MatrixClient, MatrixEvent, Room, EventType } from "matrix-js-sdk/src/matrix";

import { useTypedEventEmitter } from "./useEventEmitter";
import { useRoomState } from "./useRoomState.ts";
import { useAsyncMemo } from "./useAsyncMemo.ts";

// Hook to simplify watching whether a Matrix room is encrypted, returns undefined if room is undefined
export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | undefined {
const [isEncrypted, setIsEncrypted] = useState(room ? cli.isRoomEncrypted(room.roomId) : undefined);
// Hook to simplify watching whether a Matrix room is encrypted, returns null if room is undefined or the state is loading
export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | null {
const encryptionStateEvent: MatrixEvent | undefined = useRoomState(
room,
(roomState) => roomState.getStateEvents(EventType.RoomEncryption)?.[0],
);
return useAsyncMemo(
async () => {
const crypto = cli.getCrypto();
if (!room || !crypto) return null;

const update = useCallback(
(event: MatrixEvent) => {
if (room && event.getType() === EventType.RoomEncryption) {
setIsEncrypted(cli.isRoomEncrypted(room.roomId));
}
return crypto.isEncryptionEnabledInRoom(room.roomId);
},
[cli, room],
[room, encryptionStateEvent],
null,
);
useTypedEventEmitter(room?.currentState, RoomStateEvent.Events, update);

return isEncrypted;
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ import * as settingsHooks from "../../../../../src/hooks/useSettings";
import Modal from "../../../../../src/Modal";
import RightPanelStore from "../../../../../src/stores/right-panel/RightPanelStore";
import { RightPanelPhases } from "../../../../../src/stores/right-panel/RightPanelStorePhases";
import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../../test-utils";
import { flushPromises, stubClient } from "../../../../test-utils";
import { PollHistoryDialog } from "../../../../../src/components/views/dialogs/PollHistoryDialog";
import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks";
import { _t } from "../../../../../src/languageHandler";
@@ -56,16 +56,7 @@ describe("<RoomSummaryCard />", () => {
};

beforeEach(() => {
mockClient = getMockClientWithEventEmitter({
...mockClientMethodsUser(userId),
getAccountData: jest.fn(),
isRoomEncrypted: jest.fn(),
getOrCreateFilter: jest.fn().mockResolvedValue({ filterId: 1 }),
getRoom: jest.fn(),
isGuest: jest.fn().mockReturnValue(false),
deleteRoomTag: jest.fn().mockResolvedValue({}),
setRoomTag: jest.fn().mockResolvedValue({}),
});
mockClient = mocked(stubClient());
room = new Room(roomId, mockClient, userId);
const roomCreateEvent = new MatrixEvent({
type: "m.room.create",
Original file line number Diff line number Diff line change
@@ -134,6 +134,7 @@ beforeEach(() => {
getUserDeviceInfo: jest.fn(),
userHasCrossSigningKeys: jest.fn().mockResolvedValue(false),
getUserVerificationStatus: jest.fn(),
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
} as unknown as CryptoApi);

mockClient = mocked({
@@ -148,7 +149,6 @@ beforeEach(() => {
on: jest.fn(),
off: jest.fn(),
isSynapseAdministrator: jest.fn().mockResolvedValue(false),
isRoomEncrypted: jest.fn().mockReturnValue(false),
doesServerSupportUnstableFeature: jest.fn().mockReturnValue(false),
doesServerSupportExtendedProfiles: jest.fn().mockResolvedValue(false),
getExtendedProfileProperty: jest.fn().mockRejectedValue(new Error("Not supported")),
@@ -660,7 +660,7 @@ describe("<UserInfo />", () => {

describe("with an encrypted room", () => {
beforeEach(() => {
mockClient.isRoomEncrypted.mockReturnValue(true);
jest.spyOn(mockClient.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
});

it("renders unverified user info", async () => {

Unchanged files with check annotations Beta

await settings.locator("#roomAliases").getByText("Add", { exact: true }).click();
// 2. wait for the new setting to apply ...
await expect(settings.locator("#canonicalAlias")).toHaveValue(`#${longString}:localhost`);

Check failure on line 47 in playwright/e2e/settings/general-room-settings-tab.spec.ts

GitHub Actions / Run Tests 5/6

settings/general-room-settings-tab.spec.ts:39:9 › General room settings tab › long address should not cause dialog to overflow

1) settings/general-room-settings-tab.spec.ts:39:9 › General room settings tab › long address should not cause dialog to overflow Error: Timed out 5000ms waiting for expect(locator).toHaveValue(expected) Locator: locator('.mx_Dialog').filter({ has: locator('.mx_RoomSettingsDialog') }).locator('#canonicalAlias') Expected string: "#abcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksd:localhost" Received string: "" Call log: - expect.toHaveValue with timeout 5000ms - waiting for locator('.mx_Dialog').filter({ has: locator('.mx_RoomSettingsDialog') }).locator('#canonicalAlias') - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" 45 | 46 | // 2. wait for the new setting to apply ... > 47 | await expect(settings.locator("#canonicalAlias")).toHaveValue(`#${longString}:localhost`); | ^ 48 | 49 | // 3. Check if the dialog overflows 50 | const dialogBoundingBox = await page.locator(".mx_Dialog").boundingBox(); at /home/runner/work/element-web/element-web/playwright/e2e/settings/general-room-settings-tab.spec.ts:47:59

Check failure on line 47 in playwright/e2e/settings/general-room-settings-tab.spec.ts

GitHub Actions / Run Tests 5/6

settings/general-room-settings-tab.spec.ts:39:9 › General room settings tab › long address should not cause dialog to overflow

1) settings/general-room-settings-tab.spec.ts:39:9 › General room settings tab › long address should not cause dialog to overflow Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveValue(expected) Locator: locator('.mx_Dialog').filter({ has: locator('.mx_RoomSettingsDialog') }).locator('#canonicalAlias') Expected string: "#abcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksdabcasdhjasjhdaj1jh1asdhasjdhajsdhjavhjksd:localhost" Received string: "" Call log: - expect.toHaveValue with timeout 5000ms - waiting for locator('.mx_Dialog').filter({ has: locator('.mx_RoomSettingsDialog') }).locator('#canonicalAlias') - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" - locator resolved to <select type="text" id="canonicalAlias" label="Main address" placeholder="Main address">…</select> - unexpected value "" 45 | 46 | // 2. wait for the new setting to apply ... > 47 | await expect(settings.locator("#canonicalAlias")).toHaveValue(`#${longString}:localhost`); | ^ 48 | 49 | // 3. Check if the dialog overflows 50 | const dialogBoundingBox = await page.locator(".mx_Dialog").boundingBox(); at /home/runner/work/element-web/element-web/playwright/e2e/settings/general-room-settings-tab.spec.ts:47:59
// 3. Check if the dialog overflows
const dialogBoundingBox = await page.locator(".mx_Dialog").boundingBox();