Skip to content

Commit

Permalink
ReplaceReplace MatrixCient..isRoomEncrypted by `MatrixClient.Crypto…
Browse files Browse the repository at this point in the history
…Api.isEncryptionEnabledInRoom` in `shouldSkipSetupEncryption.ts`
  • Loading branch information
florianduros committed Nov 13, 2024
1 parent 6aa6d61 commit f074ace
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
} else if (
(await cli.doesServerSupportUnstableFeature("org.matrix.e2e_cross_signing")) &&
!shouldSkipSetupEncryption(cli)
!(await shouldSkipSetupEncryption(cli))
) {
// if cross-signing is not yet set up, do so now if possible.
this.setStateForNewView({ view: Views.E2E_SETUP });
Expand Down
11 changes: 9 additions & 2 deletions src/utils/crypto/shouldSkipSetupEncryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ Please see LICENSE files in the repository root for full details.
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import { shouldForceDisableEncryption } from "./shouldForceDisableEncryption";
import { asyncSome } from "../arrays.ts";

/**
* If encryption is force disabled AND the user is not in any encrypted rooms
* skip setting up encryption
* @param client
* @returns {boolean} true when we can skip settings up encryption
*/
export const shouldSkipSetupEncryption = (client: MatrixClient): boolean => {
export const shouldSkipSetupEncryption = async (client: MatrixClient): Promise<boolean> => {
const isEncryptionForceDisabled = shouldForceDisableEncryption(client);
return isEncryptionForceDisabled && !client.getRooms().some((r) => client.isRoomEncrypted(r.roomId));
const crypto = client.getCrypto();
if (!crypto) return true;

return (
isEncryptionForceDisabled &&
!(await asyncSome(client.getRooms(), ({ roomId }) => crypto.isEncryptionEnabledInRoom(roomId)))
);
};
10 changes: 6 additions & 4 deletions test/unit-tests/components/structures/MatrixChat-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe("<MatrixChat />", () => {
matrixRTC: createStubMatrixRTC(),
getDehydratedDevice: jest.fn(),
whoami: jest.fn(),
isRoomEncrypted: jest.fn(),
logout: jest.fn(),
getDeviceId: jest.fn(),
getKeyBackupVersion: jest.fn().mockResolvedValue(null),
Expand Down Expand Up @@ -1011,6 +1010,7 @@ describe("<MatrixChat />", () => {
userHasCrossSigningKeys: jest.fn().mockResolvedValue(false),
// This needs to not finish immediately because we need to test the screen appears
bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise),
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
};
loginClient.getCrypto.mockReturnValue(mockCrypto as any);
});
Expand Down Expand Up @@ -1058,9 +1058,11 @@ describe("<MatrixChat />", () => {
},
});

loginClient.isRoomEncrypted.mockImplementation((roomId) => {
return roomId === encryptedRoom.roomId;
});
jest.spyOn(loginClient.getCrypto()!, "isEncryptionEnabledInRoom").mockImplementation(
async (roomId) => {
return roomId === encryptedRoom.roomId;
},
);
});

it("should go straight to logged in view when user is not in any encrypted rooms", async () => {
Expand Down

0 comments on commit f074ace

Please sign in to comment.