2019-08-29 14:20:14 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-08-29 14:20:14 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-08-29 14:20:14 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-10 08:01:14 +00:00
|
|
|
import { SERVICE_TYPES, HTTPError, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-06-22 16:22:38 +00:00
|
|
|
|
2019-08-29 14:20:14 +00:00
|
|
|
import SdkConfig from "../SdkConfig";
|
2023-02-16 17:21:44 +00:00
|
|
|
import { Policies } from "../Terms";
|
2019-08-29 14:20:14 +00:00
|
|
|
|
2023-04-03 08:26:55 +00:00
|
|
|
export function getDefaultIdentityServerUrl(): string | undefined {
|
|
|
|
return SdkConfig.get("validated_server_config")?.isUrl;
|
2019-08-29 14:20:14 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:24:12 +00:00
|
|
|
export function setToDefaultIdentityServer(matrixClient: MatrixClient): void {
|
2019-08-29 14:20:14 +00:00
|
|
|
const url = getDefaultIdentityServerUrl();
|
|
|
|
// Account data change will update localstorage, client, etc through dispatcher
|
2023-05-23 15:24:12 +00:00
|
|
|
matrixClient.setAccountData("m.identity_server", {
|
2019-08-29 14:20:14 +00:00
|
|
|
base_url: url,
|
|
|
|
});
|
|
|
|
}
|
2019-10-31 11:58:31 +00:00
|
|
|
|
2023-05-23 15:24:12 +00: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 15:24:12 +00:00
|
|
|
terms = await matrixClient.getTerms(SERVICE_TYPES.IS, fullUrl);
|
2019-10-31 11:58:31 +00:00
|
|
|
} catch (e) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error(e);
|
2023-07-07 13:46:12 +00: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 15:24:12 +00: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
|
|
|
}
|