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

Third batch: Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom #28511

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class ManageEventIndexDialog extends React.Component<IProps, ISta
let currentRoom: string | null = null;

if (room) currentRoom = room.name;
const roomStats = eventIndex.crawlingRooms();
const roomStats = await eventIndex.crawlingRooms();
const crawlingRoomsCount = roomStats.crawlingRooms.size;
const roomCount = roomStats.totalRooms.size;

Expand Down Expand Up @@ -112,7 +112,7 @@ export default class ManageEventIndexDialog extends React.Component<IProps, ISta
// probably succeed.
}

const roomStats = eventIndex.crawlingRooms();
const roomStats = await eventIndex.crawlingRooms();
crawlingRoomsCount = roomStats.crawlingRooms.size;
roomCount = roomStats.totalRooms.size;

Expand Down
4 changes: 2 additions & 2 deletions src/components/views/rooms/MemberTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export default class MemberTile extends React.Component<IProps, IState> {
};
}

public componentDidMount(): void {
public async componentDidMount(): Promise<void> {
const cli = MatrixClientPeg.safeGet();

const { roomId } = this.props.member;
if (roomId) {
const isRoomEncrypted = cli.isRoomEncrypted(roomId);
const isRoomEncrypted = Boolean(await cli.getCrypto()?.isEncryptionEnabledInRoom(roomId));
this.setState({
isRoomEncrypted,
});
Expand Down
11 changes: 8 additions & 3 deletions src/components/views/rooms/NewRoomIntro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details.
import React, { useContext } from "react";
import { EventType, Room, User, MatrixClient } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import { InlineSpinner } from "@vector-im/compound-web";

import MatrixClientContext from "../../../contexts/MatrixClientContext";
import RoomContext from "../../../contexts/RoomContext";
Expand All @@ -30,9 +31,9 @@ import { UIComponent } from "../../../settings/UIFeature";
import { privateShouldBeEncrypted } from "../../../utils/rooms";
import { LocalRoom } from "../../../models/LocalRoom";
import { shouldEncryptRoomWithSingle3rdPartyInvite } from "../../../utils/room/shouldEncryptRoomWithSingle3rdPartyInvite";
import { useIsEncrypted } from "../../../hooks/useIsEncrypted.ts";

function hasExpectedEncryptionSettings(matrixClient: MatrixClient, room: Room): boolean {
const isEncrypted: boolean = matrixClient.isRoomEncrypted(room.roomId);
function hasExpectedEncryptionSettings(matrixClient: MatrixClient, room: Room, isEncrypted: boolean): boolean {
const isPublic: boolean = room.getJoinRule() === "public";
return isPublic || !privateShouldBeEncrypted(matrixClient) || isEncrypted;
}
Expand All @@ -52,11 +53,15 @@ const determineIntroMessage = (room: Room, encryptedSingle3rdPartyInvite: boolea
const NewRoomIntro: React.FC = () => {
const cli = useContext(MatrixClientContext);
const { room, roomId } = useContext(RoomContext);
const isEncrypted = useIsEncrypted(cli, room);
const isEncryptionLoading = isEncrypted === null;

if (!room || !roomId) {
throw new Error("Unable to create a NewRoomIntro without room and roomId");
}

if (isEncryptionLoading) return <InlineSpinner />;

const isLocalRoom = room instanceof LocalRoom;
const dmPartner = isLocalRoom ? room.targets[0]?.userId : DMRoomMap.shared().getUserIdForRoomId(roomId);

Expand Down Expand Up @@ -282,7 +287,7 @@ const NewRoomIntro: React.FC = () => {

return (
<li className="mx_NewRoomIntro">
{!hasExpectedEncryptionSettings(cli, room) && (
{!hasExpectedEncryptionSettings(cli, room, isEncrypted) && (
<EventTileBubble
className="mx_cryptoEvent mx_cryptoEvent_icon_warning"
title={_t("room|intro|unencrypted_warning")}
Expand Down
20 changes: 10 additions & 10 deletions src/indexing/EventIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default class EventIndex extends EventEmitter {
const client = MatrixClientPeg.safeGet();

// We only index encrypted rooms locally.
if (!client.isRoomEncrypted(ev.getRoomId()!)) return;
if (!(await client.getCrypto()?.isEncryptionEnabledInRoom(ev.getRoomId()!))) return;

if (ev.isRedaction()) {
return this.redactEvent(ev);
Expand All @@ -223,7 +223,8 @@ export default class EventIndex extends EventEmitter {
};

private onRoomStateEvent = async (ev: MatrixEvent, state: RoomState): Promise<void> => {
if (!MatrixClientPeg.safeGet().isRoomEncrypted(state.roomId)) return;
const crypto = MatrixClientPeg.safeGet().getCrypto();
if (!(await crypto?.isEncryptionEnabledInRoom(state.roomId))) return;

if (ev.getType() === EventType.RoomEncryption && !(await this.isRoomIndexed(state.roomId))) {
logger.log("EventIndex: Adding a checkpoint for a newly encrypted room", state.roomId);
Expand Down Expand Up @@ -257,7 +258,7 @@ export default class EventIndex extends EventEmitter {
*/
private onTimelineReset = async (room: Room | undefined): Promise<void> => {
if (!room) return;
if (!MatrixClientPeg.safeGet().isRoomEncrypted(room.roomId)) return;
if (!(await MatrixClientPeg.safeGet().getCrypto()?.isEncryptionEnabledInRoom(room.roomId))) return;

logger.log("EventIndex: Adding a checkpoint because of a limited timeline", room.roomId);

Expand Down Expand Up @@ -950,10 +951,10 @@ export default class EventIndex extends EventEmitter {
}
}

public crawlingRooms(): {
public async crawlingRooms(): Promise<{
crawlingRooms: Set<string>;
totalRooms: Set<string>;
} {
}> {
const totalRooms = new Set<string>();
const crawlingRooms = new Set<string>();

Expand All @@ -966,13 +967,12 @@ export default class EventIndex extends EventEmitter {
}

const client = MatrixClientPeg.safeGet();
const crypto = client.getCrypto();
const rooms = client.getRooms();

const isRoomEncrypted = (room: Room): boolean => {
return client.isRoomEncrypted(room.roomId);
};

const encryptedRooms = rooms.filter(isRoomEncrypted);
const encryptedRooms = crypto
? await asyncFilter(rooms, (room) => crypto.isEncryptionEnabledInRoom(room.roomId))
: [];
encryptedRooms.forEach((room, index) => {
totalRooms.add(room.roomId);
});
Expand Down
16 changes: 12 additions & 4 deletions test/unit-tests/components/structures/RoomView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ describe("RoomView", () => {
});

describe("in state NEW", () => {
beforeEach(() => {
jest.spyOn(cli, "getCrypto").mockReturnValue(crypto);
jest.spyOn(cli.getCrypto()!, "getUserVerificationStatus").mockResolvedValue(
new UserVerificationStatus(false, true, false),
);
});

it("should match the snapshot", async () => {
const { container } = await renderRoomView();
expect(container).toMatchSnapshot();
Expand All @@ -362,11 +369,7 @@ describe("RoomView", () => {
beforeEach(() => {
// Not all the calls to cli.isRoomEncrypted are migrated, so we need to mock both.
mocked(cli.isRoomEncrypted).mockReturnValue(true);
jest.spyOn(cli, "getCrypto").mockReturnValue(crypto);
jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
jest.spyOn(cli.getCrypto()!, "getUserVerificationStatus").mockResolvedValue(
new UserVerificationStatus(false, true, false),
);
localRoom.encrypted = true;
localRoom.currentState.setStateEvents([
new MatrixEvent({
Expand Down Expand Up @@ -399,6 +402,11 @@ describe("RoomView", () => {
describe("in state ERROR", () => {
beforeEach(async () => {
localRoom.state = LocalRoomState.ERROR;

jest.spyOn(cli, "getCrypto").mockReturnValue(crypto);
jest.spyOn(cli.getCrypto()!, "getUserVerificationStatus").mockResolvedValue(
new UserVerificationStatus(false, true, false),
);
});

it("should match the snapshot", async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/unit-tests/components/views/rooms/MemberList-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
filterConsole,
flushPromises,
getMockClientWithEventEmitter,
mockClientMethodsCrypto,
mockClientMethodsRooms,
mockClientMethodsUser,
} from "../../../../test-utils";
Expand Down Expand Up @@ -361,6 +362,7 @@ describe("MemberList", () => {
client = getMockClientWithEventEmitter({
...mockClientMethodsUser(),
...mockClientMethodsRooms(),
...mockClientMethodsCrypto(),
getRoom: jest.fn(),
hasLazyLoadMembersEnabled: jest.fn(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("MemberTile", () => {

beforeEach(() => {
matrixClient = TestUtils.stubClient();
mocked(matrixClient.isRoomEncrypted).mockReturnValue(true);
jest.spyOn(matrixClient.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
member = new RoomMember("roomId", matrixClient.getUserId()!);
});

Expand Down
19 changes: 13 additions & 6 deletions test/unit-tests/components/views/rooms/NewRoomIntro-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Please see LICENSE files in the repository root for full details.
import React from "react";
import { render, screen } from "jest-matrix-react";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { waitFor } from "@testing-library/dom";

import { LocalRoom } from "../../../../../src/models/LocalRoom";
import { filterConsole, mkRoomMemberJoinEvent, mkThirdPartyInviteEvent, stubClient } from "../../../../test-utils";
Expand Down Expand Up @@ -50,13 +51,15 @@ describe("NewRoomIntro", () => {
renderNewRoomIntro(client, room);
});

it("should render the expected intro", () => {
it("should render the expected intro", async () => {
const expected = `This is the beginning of your direct message history with test_room.`;
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected);
await waitFor(() =>
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected),
);
});
});

it("should render as expected for a DM room with a single third-party invite", () => {
it("should render as expected for a DM room with a single third-party invite", async () => {
const room = new Room(roomId, client, client.getSafeUserId());
room.currentState.setStateEvents([
mkRoomMemberJoinEvent(client.getSafeUserId(), room.roomId),
Expand All @@ -66,7 +69,9 @@ describe("NewRoomIntro", () => {
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([room.roomId]));
renderNewRoomIntro(client, room);

expect(screen.getByText("Once everyone has joined, you’ll be able to chat")).toBeInTheDocument();
await waitFor(() =>
expect(screen.getByText("Once everyone has joined, you’ll be able to chat")).toBeInTheDocument(),
);
expect(
screen.queryByText(
"Only the two of you are in this conversation, unless either of you invites anyone to join.",
Expand All @@ -83,9 +88,11 @@ describe("NewRoomIntro", () => {
renderNewRoomIntro(client, localRoom);
});

it("should render the expected intro", () => {
it("should render the expected intro", async () => {
const expected = `Send your first message to invite test_room to chat`;
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected);
await waitFor(() =>
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import { defer, IDeferred } from "matrix-js-sdk/src/utils";
import EventIndexPanel from "../../../../../src/components/views/settings/EventIndexPanel";
import EventIndexPeg from "../../../../../src/indexing/EventIndexPeg";
import EventIndex from "../../../../../src/indexing/EventIndex";
import { clearAllModals, flushPromises, getMockClientWithEventEmitter } from "../../../../test-utils";
import {
clearAllModals,
flushPromises,
getMockClientWithEventEmitter,
mockClientMethodsCrypto,
} from "../../../../test-utils";
import SettingsStore from "../../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../../src/settings/SettingLevel";

describe("<EventIndexPanel />", () => {
getMockClientWithEventEmitter({
getRooms: jest.fn().mockReturnValue([]),
...mockClientMethodsCrypto(),
});

const getComponent = () => render(<EventIndexPanel />);
Expand Down
Loading