2019-08-15 13:16:59 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-27 09:31:34 +02:00
|
|
|
import { IThreepid, ThreepidMedium } from "matrix-js-sdk/src/@types/threepids";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2019-08-15 13:16:59 +02:00
|
|
|
import IdentityAuthClient from './IdentityAuthClient';
|
|
|
|
|
2021-09-27 09:31:34 +02:00
|
|
|
export async function getThreepidsWithBindStatus(
|
|
|
|
client: MatrixClient, filterMedium?: ThreepidMedium,
|
|
|
|
): 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 });
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
const threepid = threepids.find(e => e.medium === medium && e.address === address);
|
|
|
|
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
|
2022-05-03 23:04:37 +02:00
|
|
|
if (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;
|
|
|
|
}
|