2019-08-29 15:20:14 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-08-29 15:20:14 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-08-29 15:20:14 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-08-10 09:01:14 +01:00
|
|
|
import { SERVICE_TYPES, HTTPError, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-06-22 17:22:38 +01:00
|
|
|
|
2019-08-29 15:20:14 +01:00
|
|
|
import SdkConfig from "../SdkConfig";
|
2023-02-16 17:21:44 +00:00
|
|
|
import { Policies } from "../Terms";
|
2019-08-29 15:20:14 +01:00
|
|
|
|
2023-04-03 20:26:55 +12:00
|
|
|
export function getDefaultIdentityServerUrl(): string | undefined {
|
|
|
|
|
return SdkConfig.get("validated_server_config")?.isUrl;
|
2019-08-29 15:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:24:12 +01:00
|
|
|
export function setToDefaultIdentityServer(matrixClient: MatrixClient): void {
|
2019-08-29 15:20:14 +01:00
|
|
|
const url = getDefaultIdentityServerUrl();
|
|
|
|
|
// Account data change will update localstorage, client, etc through dispatcher
|
2023-05-23 16:24:12 +01:00
|
|
|
matrixClient.setAccountData("m.identity_server", {
|
2024-12-19 22:53:51 +00:00
|
|
|
base_url: url ?? null,
|
2019-08-29 15:20:14 +01:00
|
|
|
});
|
|
|
|
|
}
|
2019-10-31 11:58:31 +00:00
|
|
|
|
2023-05-23 16:24:12 +01:00
|
|
|
export async function doesIdentityServerHaveTerms(matrixClient: MatrixClient, fullUrl: string): Promise<boolean> {
|
2023-02-16 17:21:44 +00:00
|
|
|
let terms: { policies?: Policies } | null;
|
2019-10-31 11:58:31 +00:00
|
|
|
try {
|
2023-05-23 16:24:12 +01:00
|
|
|
terms = await matrixClient.getTerms(SERVICE_TYPES.IS, fullUrl);
|
2019-10-31 11:58:31 +00:00
|
|
|
} catch (e) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error(e);
|
2023-07-07 14:46:12 +01:00
|
|
|
if (e instanceof HTTPError && e.httpStatus === 404) {
|
2019-10-31 11:58:31 +00:00
|
|
|
terms = null;
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
return !!terms?.["policies"] && Object.keys(terms["policies"]).length > 0;
|
2019-10-31 11:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:24:12 +01:00
|
|
|
export function doesAccountDataHaveIdentityServer(matrixClient: MatrixClient): boolean {
|
|
|
|
|
const event = matrixClient.getAccountData("m.identity_server");
|
|
|
|
|
return event?.getContent()["base_url"];
|
2019-10-31 11:58:31 +00:00
|
|
|
}
|