From a8cc6cc207d726870a7667a0f78f475142c75e63 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Fri, 19 Jan 2024 10:00:22 +0000 Subject: [PATCH] Broaden support for matrix spec versions (#12154) Something of a compainion to https://github.com/matrix-org/matrix-js-sdk/pull/4014, but also covering the issues discussed at https://github.com/matrix-org/matrix-js-sdk/issues/3915#issuecomment-1865221366. In short: we should not reject servers which only implement recent versions of the spec. Doing so holds back the ecosystem by requiring all new servers to implement features that nobody actually uses any more. --- src/Lifecycle.ts | 55 +++++++++++++++++++++++------------------- test/Lifecycle-test.ts | 5 +--- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 0f2435ffe8..1d6577ca39 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -23,7 +23,7 @@ import { InvalidStoreError } from "matrix-js-sdk/src/errors"; import { IEncryptedPayload } from "matrix-js-sdk/src/crypto/aes"; import { QueryDict } from "matrix-js-sdk/src/utils"; import { logger } from "matrix-js-sdk/src/logger"; -import { MINIMUM_MATRIX_VERSION } from "matrix-js-sdk/src/version-support"; +import { MINIMUM_MATRIX_VERSION, SUPPORTED_MATRIX_VERSIONS } from "matrix-js-sdk/src/version-support"; import { IMatrixClientCreds, MatrixClientPeg } from "./MatrixClientPeg"; import SecurityCustomisations from "./customisations/Security"; @@ -635,7 +635,7 @@ export async function restoreFromLocalStorage(opts?: { ignoreGuest?: boolean }): }, false, ); - checkServerVersions(); + await checkServerVersions(); return true; } else { logger.log("No previous session found."); @@ -644,29 +644,34 @@ export async function restoreFromLocalStorage(opts?: { ignoreGuest?: boolean }): } async function checkServerVersions(): Promise { - MatrixClientPeg.get() - ?.getVersions() - .then((response) => { - if (!response.versions.includes(MINIMUM_MATRIX_VERSION)) { - const toastKey = "LEGACY_SERVER"; - ToastStore.sharedInstance().addOrReplaceToast({ - key: toastKey, - title: _t("unsupported_server_title"), - props: { - description: _t("unsupported_server_description", { - version: MINIMUM_MATRIX_VERSION, - brand: SdkConfig.get().brand, - }), - acceptLabel: _t("action|ok"), - onAccept: () => { - ToastStore.sharedInstance().dismissToast(toastKey); - }, - }, - component: GenericToast, - priority: 98, - }); - } - }); + const client = MatrixClientPeg.get(); + if (!client) return; + for (const version of SUPPORTED_MATRIX_VERSIONS) { + // Check if the server supports this spec version. (`isVersionSupported` caches the response, so this loop will + // only make a single HTTP request). + if (await client.isVersionSupported(version)) { + // we found a compatible spec version + return; + } + } + + const toastKey = "LEGACY_SERVER"; + ToastStore.sharedInstance().addOrReplaceToast({ + key: toastKey, + title: _t("unsupported_server_title"), + props: { + description: _t("unsupported_server_description", { + version: MINIMUM_MATRIX_VERSION, + brand: SdkConfig.get().brand, + }), + acceptLabel: _t("action|ok"), + onAccept: () => { + ToastStore.sharedInstance().dismissToast(toastKey); + }, + }, + component: GenericToast, + priority: 98, + }); } async function handleLoadSessionFailure(e: unknown): Promise { diff --git a/test/Lifecycle-test.ts b/test/Lifecycle-test.ts index b984549e9d..90dd0c5335 100644 --- a/test/Lifecycle-test.ts +++ b/test/Lifecycle-test.ts @@ -453,10 +453,7 @@ describe("Lifecycle", () => { it("should show a toast if the matrix server version is unsupported", async () => { const toastSpy = jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast"); - mockClient.getVersions.mockResolvedValue({ - versions: ["r0.6.0"], - unstable_features: {}, - }); + mockClient.isVersionSupported.mockImplementation(async (version) => version == "r0.6.0"); initLocalStorageMock({ ...localStorageSession }); expect(await restoreFromLocalStorage()).toEqual(true);