element-web-Github/src/boundThreepids.ts
David Langley 491f0cd08a
Change license (#13)
* Copyright headers 1

* Licence headers 2

* Copyright Headers 3

* Copyright Headers 4

* Copyright Headers 5

* Copyright Headers 6

* Copyright headers 7

* Add copyright headers for html and config file

* Replace license files and update package.json

* Update with CLA

* lint
2024-09-09 13:57:16 +00:00

59 lines
2.1 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { IThreepid, ThreepidMedium, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
import IdentityAuthClient from "./IdentityAuthClient";
export async function getThreepidsWithBindStatus(
client: MatrixClient,
filterMedium?: ThreepidMedium,
): Promise<IThreepid[]> {
const userId = client.getUserId();
let { threepids } = await client.getThreePids();
if (filterMedium) {
threepids = threepids.filter((a) => a.medium === filterMedium);
}
// Check bind status assuming we have an IS and terms are agreed
if (threepids.length > 0 && !!client.getIdentityServerUrl()) {
try {
const authClient = new IdentityAuthClient();
const identityAccessToken = await authClient.getAccessToken({ check: false });
if (!identityAccessToken) {
throw new Error("No identity access token found");
}
// Restructure for lookup query
const query = threepids.map(({ medium, address }): [string, string] => [medium, address]);
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;
}
const threepid = threepids.find((e) => e.medium === medium && e.address === address);
if (!threepid) continue;
threepid.bound = true;
}
} catch (e) {
// Ignore terms errors here and assume other flows handle this
if (!(e instanceof MatrixError) || e.errcode !== "M_TERMS_NOT_SIGNED") {
throw e;
}
}
}
return threepids;
}