From 8f016726f017e110c397f4551904f1a747aafe00 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Wed, 7 Aug 2019 17:45:13 +0100 Subject: [PATCH] Add API for bulk lookup on the Identity Server This adds support for querying `/bulk_lookup` on the IS to check several 3PIDs at the same time. Part of https://github.com/vector-im/riot-web/issues/10159 --- src/base-apis.js | 38 ++++++++++++++++++++++++++++++++++++++ src/http-api.js | 11 +++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/base-apis.js b/src/base-apis.js index c55e5b93b20..c4230c74ff9 100644 --- a/src/base-apis.js +++ b/src/base-apis.js @@ -1889,6 +1889,44 @@ MatrixBaseApis.prototype.lookupThreePid = async function( } }; +/** + * Looks up the public Matrix ID mappings for multiple 3PIDs. + * + * @param {array} query Array of arrays containing [medium, address] + * @param {string} identityAccessToken The `access_token` field of the Identity + * Server `/account/register` response (see {@link registerWithIdentityServer}). + * + * @return {module:client.Promise} Resolves: Lookup results from IS. + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixBaseApis.prototype.bulkLookupThreePids = async function( + query, + identityAccessToken, +) { + const params = { + threepids: query, + }; + + try { + return await this._http.idServerRequest( + undefined, "POST", "/bulk_lookup", JSON.stringify(params), + httpApi.PREFIX_IDENTITY_V2, identityAccessToken, + ); + } catch (err) { + if (err.cors === "rejected" || err.httpStatus === 404) { + // Fall back to deprecated v1 API for now + // TODO: Remove this path once v2 is only supported version + // See https://github.com/vector-im/riot-web/issues/10443 + logger.warn("IS doesn't support v2, falling back to deprecated v1"); + return await this._http.idServerRequest( + undefined, "POST", "/bulk_lookup", JSON.stringify(params), + httpApi.PREFIX_IDENTITY_V1, identityAccessToken, + ); + } + throw err; + } +}; + /** * Get account info from the Identity Server. This is useful as a neutral check * to verify that other APIs are likely to approve access by testing that the diff --git a/src/http-api.js b/src/http-api.js index 9271b9bae9a..0ee411cf79a 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -396,16 +396,19 @@ module.exports.MatrixHttpApi.prototype = { withCredentials: false, json: false, _matrix_opts: this.opts, + headers: {}, }; if (method == 'GET') { opts.qs = params; - } else { + } else if (typeof params === "object") { opts.form = params; + } else if (typeof params === "string") { + // Assume the caller has serialised the body to JSON + opts.body = params; + opts.headers['Content-Type'] = "application/json"; } if (accessToken) { - opts.headers = { - Authorization: `Bearer ${accessToken}`, - }; + opts.headers['Authorization'] = `Bearer ${accessToken}`; } const defer = Promise.defer();