2019-08-29 14:20:14 +00:00
|
|
|
/*
|
2021-06-22 16:22:38 +00:00
|
|
|
Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
|
2019-08-29 14:20:14 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { SERVICE_TYPES } from "matrix-js-sdk/src/service-types";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-08-09 15:10:54 +00:00
|
|
|
import { HTTPError, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2021-06-22 16:22:38 +00:00
|
|
|
|
2022-12-12 11:24: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
|
|
|
}
|