2019-08-15 13:16:59 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-08-15 13:16:59 +02:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02: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-15 13:16:59 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-23 11:04:25 +02:00
|
|
|
import { IThreepid, ThreepidMedium, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import IdentityAuthClient from "./IdentityAuthClient";
|
2019-08-15 13:16:59 +02:00
|
|
|
|
2021-09-27 09:31:34 +02:00
|
|
|
export async function getThreepidsWithBindStatus(
|
2022-12-12 12:24:14 +01:00
|
|
|
client: MatrixClient,
|
|
|
|
filterMedium?: ThreepidMedium,
|
2021-09-27 09:31:34 +02:00
|
|
|
): Promise<IThreepid[]> {
|
2019-08-15 13:16:59 +02:00
|
|
|
const userId = client.getUserId();
|
|
|
|
|
|
|
|
let { threepids } = await client.getThreePids();
|
|
|
|
if (filterMedium) {
|
|
|
|
threepids = threepids.filter((a) => a.medium === filterMedium);
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:33:58 +02:00
|
|
|
// Check bind status assuming we have an IS and terms are agreed
|
2019-09-11 17:55:45 +02:00
|
|
|
if (threepids.length > 0 && !!client.getIdentityServerUrl()) {
|
2019-09-11 14:36:10 +02:00
|
|
|
try {
|
|
|
|
const authClient = new IdentityAuthClient();
|
|
|
|
const identityAccessToken = await authClient.getAccessToken({ check: false });
|
2023-03-10 15:55:06 +01:00
|
|
|
if (!identityAccessToken) {
|
|
|
|
throw new Error("No identity access token found");
|
|
|
|
}
|
2019-09-11 14:36:10 +02:00
|
|
|
|
|
|
|
// Restructure for lookup query
|
2021-09-27 09:31:34 +02:00
|
|
|
const query = threepids.map(({ medium, address }): [string, string] => [medium, address]);
|
2019-09-11 14:36:10 +02:00
|
|
|
const lookupResults = await client.bulkLookupThreePids(query, identityAccessToken);
|
|
|
|
|
|
|
|
// Record which are already bound
|
|
|
|
for (const [medium, address, mxid] of lookupResults.threepids) {
|
|
|
|
if (mxid !== userId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (filterMedium && medium !== filterMedium) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
const threepid = threepids.find((e) => e.medium === medium && e.address === address);
|
2019-09-11 14:36:10 +02:00
|
|
|
if (!threepid) continue;
|
|
|
|
threepid.bound = true;
|
2019-08-15 13:16:59 +02:00
|
|
|
}
|
2019-09-11 14:36:10 +02:00
|
|
|
} catch (e) {
|
|
|
|
// Ignore terms errors here and assume other flows handle this
|
2023-05-16 15:25:43 +02:00
|
|
|
if (!(e instanceof MatrixError) || e.errcode !== "M_TERMS_NOT_SIGNED") {
|
2019-09-11 14:36:10 +02:00
|
|
|
throw e;
|
2019-08-15 13:16:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return threepids;
|
|
|
|
}
|