Skip to content
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
1 change: 0 additions & 1 deletion apps/web/playwright/e2e/crypto/toasts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ test.describe("'Turn on key storage' toast", () => {
test("should show toast if key storage is off but account data is missing", async ({ app, page }) => {
// Given the backup is disabled but we didn't set account data saying that is expected
await disableKeyBackup(app);
await botClient.setAccountData("m.org.matrix.custom.backup_disabled", {} as any as { disabled: boolean });
await botClient.setAccountData("m.key_backup", {} as any as { enabled: boolean });

// Wait for the account data setting to stick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { CryptoEvent } from "matrix-js-sdk/src/crypto-api";
import { logger } from "matrix-js-sdk/src/logger";

import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext";
import {
DeviceListener,
ACCOUNT_DATA_KEY_M_KEY_BACKUP,
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
} from "../../../../device-listener";
import { DeviceListener, ACCOUNT_DATA_KEY_M_KEY_BACKUP } from "../../../../device-listener";
import { useEventEmitterAsyncState } from "../../../../hooks/useEventEmitter";
import { resetKeyBackupAndWait } from "../../../../utils/crypto/resetKeyBackup";

Expand Down Expand Up @@ -118,9 +114,6 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {

// Set the flag to say that the user wants backup enabled
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: true });
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
disabled: false,
});
} else {
logger.info("User requested disabling key backup");
// This method will delete the key backup as well as server side recovery keys and other
Expand All @@ -129,9 +122,6 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {

// Set the flag to say that the user wants backup disabled
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
disabled: true,
});
}
});
} finally {
Expand Down
10 changes: 2 additions & 8 deletions apps/web/src/device-listener/DeviceListenerCurrentDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
* disable server side key backups.
*/
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP = "m.key_backup";
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE = "m.org.matrix.custom.backup_disabled";

/**
* Account data key to indicate whether the user has chosen to enable or disable recovery.
Expand Down Expand Up @@ -152,7 +151,6 @@ export class DeviceListenerCurrentDevice {
*/
public async recordKeyBackupDisabled(): Promise<void> {
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, { disabled: true });
}

/**
Expand Down Expand Up @@ -306,12 +304,9 @@ export class DeviceListenerCurrentDevice {
const keyBackup = await this.client.getAccountDataFromServer(ACCOUNT_DATA_KEY_M_KEY_BACKUP);
if (keyBackup) {
return keyBackup.enabled === false;
} else {
return false;
}

const keyBackupDisabledUnstable = await this.client.getAccountDataFromServer(
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
);
return !!keyBackupDisabledUnstable?.disabled;
}

/**
Expand Down Expand Up @@ -361,7 +356,6 @@ export class DeviceListenerCurrentDevice {
ev.getType().startsWith("m.cross_signing.") ||
ev.getType() === "m.megolm_backup.v1" ||
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP ||
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE ||
ev.getType() === RECOVERY_ACCOUNT_DATA_KEY
) {
this.deviceListener.recheck();
Expand Down
28 changes: 3 additions & 25 deletions apps/web/test/unit-tests/DeviceListener-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
} from "matrix-js-sdk/src/crypto-api";
import { type CryptoSessionStateChange } from "@matrix-org/analytics-events/types/typescript/CryptoSessionStateChange";

import {
DeviceListener,
ACCOUNT_DATA_KEY_M_KEY_BACKUP,
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
RECOVERY_ACCOUNT_DATA_KEY,
} from "../../src/device-listener";
import { DeviceListener, ACCOUNT_DATA_KEY_M_KEY_BACKUP, RECOVERY_ACCOUNT_DATA_KEY } from "../../src/device-listener";
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
import * as SetupEncryptionToast from "../../src/toasts/SetupEncryptionToast";
import * as UnverifiedSessionToast from "../../src/toasts/UnverifiedSessionToast";
Expand Down Expand Up @@ -327,9 +322,7 @@

// @ts-ignore implementing a function with complex return type
mockClient!.getAccountDataFromServer.mockImplementation(async (key) => {
if (key === ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE) {
return { disabled: true };
} else if (key == ACCOUNT_DATA_KEY_M_KEY_BACKUP) {
if (key == ACCOUNT_DATA_KEY_M_KEY_BACKUP) {
return { enabled: false };
} else if (key === RECOVERY_ACCOUNT_DATA_KEY) {
return null;
Expand Down Expand Up @@ -623,10 +616,6 @@
const instance = await createAndStart();
await instance.recordKeyBackupDisabled();

expect(mockClient.setAccountData).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
disabled: true,
});

expect(mockClient.setAccountData).toHaveBeenCalledWith("m.key_backup", {
enabled: false,
});
Expand Down Expand Up @@ -661,7 +650,7 @@

it("shows the 'Turn on key storage' toast if we never explicitly turned off key storage", async () => {
// Given key backup is off but the account data saying we turned it off is not set
// (m.key_backup or m.org.matrix.custom.backup_disabled)
// (m.key_backup)
mockClient.getAccountData.mockReturnValue(undefined);

// When we launch the DeviceListener
Expand All @@ -675,11 +664,9 @@
// Given key backup is off but the account data says we turned it on (this should not happen - the
// account data should only be updated if we turn on key storage)
mockClient.getAccountData.mockImplementation((eventType) => {
switch (eventType) {

Check warning on line 667 in apps/web/test/unit-tests/DeviceListener-test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "switch" statement by "if" statements to increase readability.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ6IIMGbj0xu4nLhkynl&open=AZ6IIMGbj0xu4nLhkynl&pullRequest=33695
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
return new MatrixEvent({ content: { enabled: true } });
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
return new MatrixEvent({ content: { disabled: false } });
default:
return undefined;
}
Expand Down Expand Up @@ -724,11 +711,9 @@
it("does not show the 'Turn on key storage' toast if we turned on key storage", async () => {
// Given key backup is on and the account data says we turned it on
mockClient.getAccountData.mockImplementation((eventType) => {
switch (eventType) {

Check warning on line 714 in apps/web/test/unit-tests/DeviceListener-test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "switch" statement by "if" statements to increase readability.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ6IIMGbj0xu4nLhkynm&open=AZ6IIMGbj0xu4nLhkynm&pullRequest=33695
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
return new MatrixEvent({ content: { enabled: true } });
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
return new MatrixEvent({ content: { disabled: false } });
default:
return undefined;
}
Expand All @@ -745,11 +730,9 @@
// Given key backup is on but the account data saying we turned it off is set (this should never
// happen - it should only be set when we turn off key storage or dismiss the toast)
mockClient.getAccountData.mockImplementation((eventType) => {
switch (eventType) {

Check warning on line 733 in apps/web/test/unit-tests/DeviceListener-test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "switch" statement by "if" statements to increase readability.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ6IIMGbj0xu4nLhkynn&open=AZ6IIMGbj0xu4nLhkynn&pullRequest=33695
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
return new MatrixEvent({ content: { enabled: false } });
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
return new MatrixEvent({ content: { disabled: true } });
default:
return undefined;
}
Expand Down Expand Up @@ -1301,10 +1284,7 @@

it("does not show the 'set up recovery' toast if the user has chosen to disable key storage", async () => {
mockClient!.getAccountData.mockImplementation((k: string) => {
switch (k) {

Check warning on line 1287 in apps/web/test/unit-tests/DeviceListener-test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "switch" statement by "if" statements to increase readability.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ6IIMGbj0xu4nLhkyno&open=AZ6IIMGbj0xu4nLhkyno&pullRequest=33695
case "m.org.matrix.custom.backup_disabled":
return new MatrixEvent({ content: { disabled: true } });

case "m.key_backup":
return new MatrixEvent({ content: { enabled: false } });

Expand Down Expand Up @@ -1443,11 +1423,9 @@

function mockKeyBackupFromServer(client: MockedObject<MatrixClient>, enabled: boolean) {
client.getAccountDataFromServer.mockImplementation(async (eventType: string) => {
switch (eventType) {

Check warning on line 1426 in apps/web/test/unit-tests/DeviceListener-test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "switch" statement by "if" statements to increase readability.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ6IIMGbj0xu4nLhkynp&open=AZ6IIMGbj0xu4nLhkynp&pullRequest=33695
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
return { enabled } as any;
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
return { disabled: !enabled } as any;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ describe("KeyStoragePanelViewModel", () => {
);

await result.current.setEnabled(true);
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
disabled: false,
});

expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.key_backup", {
enabled: true,
Expand All @@ -146,9 +143,6 @@ describe("KeyStoragePanelViewModel", () => {
await result.current.setEnabled(false);

expect(mocked(matrixClient.getCrypto()!.disableKeyStorage)).toHaveBeenCalled();
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
disabled: true,
});
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.key_backup", {
enabled: false,
});
Expand Down
Loading