From ee752e3885200681845b9d4711190675e0312b2e Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 16 Aug 2019 15:02:49 +0100 Subject: [PATCH 1/2] Add getIdServer() & doesServerRequireIdServerParam() Remove individual cache for lazy loading and just cache the whole versions response, then we can cache both of these flags --- src/client.js | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/client.js b/src/client.js index 2cd0f54b093..e81a14d1a25 100644 --- a/src/client.js +++ b/src/client.js @@ -238,7 +238,7 @@ function MatrixClient(opts) { // The pushprocessor caches useful things, so keep one and re-use it this._pushProcessor = new PushProcessor(this); - this._serverSupportsLazyLoading = null; + this._serverVersionsCache = null; this._cachedCapabilities = null; // { capabilities: {}, lastUpdated: timestamp } @@ -4045,12 +4045,13 @@ MatrixClient.prototype.stopClient = function() { }; /* - * Query the server to see if it support members lazy loading - * @return {Promise} true if server supports lazy loading + * Get the API versions supported by the server, along with any + * unstable APIs it supports + * @return {Promise} The server /versions response */ -MatrixClient.prototype.doesServerSupportLazyLoading = async function() { - if (this._serverSupportsLazyLoading === null) { - const response = await this._http.request( +MatrixClient.prototype.getVersions = async function() { + if (this._serverVersionsCache === null) { + this._serverVersionsCache = await this._http.request( undefined, // callback "GET", "/_matrix/client/versions", undefined, // queryParams @@ -4059,15 +4060,38 @@ MatrixClient.prototype.doesServerSupportLazyLoading = async function() { prefix: '', }, ); + } + return this._serverVersionsCache; +}; + +/* + * Query the server to see if it support members lazy loading + * @return {Promise} true if server supports lazy loading + */ +MatrixClient.prototype.doesServerSupportLazyLoading = async function() { + const response = await this.getVersions(); - const versions = response["versions"]; - const unstableFeatures = response["unstable_features"]; + const versions = response["versions"]; + const unstableFeatures = response["unstable_features"]; - this._serverSupportsLazyLoading = - (versions && versions.includes("r0.5.0")) - || (unstableFeatures && unstableFeatures["m.lazy_load_members"]); + return (versions && versions.includes("r0.5.0")) + || (unstableFeatures && unstableFeatures["m.lazy_load_members"]); +}; + +/* + * Query the server to see if the `id_server` parameter is required + * when registering with an 3pid, adding a 3pid or resetting password. + * @return {Promise} true if id_server parameter is required + */ +MatrixClient.prototype.doesServerRequireIdServerParam = async function() { + const response = await this.getVersions(); + + const unstableFeatures = response["unstable_features"]; + if (unstableFeatures["m.require_identity_server"] === undefined) { + return true; + } else { + return unstableFeatures["m.require_identity_server"]; } - return this._serverSupportsLazyLoading; }; /* From 3c69b8511d5fe33d584494b5eeb7390b80cb2c01 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 19 Aug 2019 11:21:32 +0100 Subject: [PATCH 2/2] cache should expire TODO --- src/client.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client.js b/src/client.js index e81a14d1a25..646b7fa7c77 100644 --- a/src/client.js +++ b/src/client.js @@ -238,6 +238,8 @@ function MatrixClient(opts) { // The pushprocessor caches useful things, so keep one and re-use it this._pushProcessor = new PushProcessor(this); + // Cache of the server's /versions response + // TODO: This should expire: https://github.com/matrix-org/matrix-js-sdk/issues/1020 this._serverVersionsCache = null; this._cachedCapabilities = null; // { capabilities: {}, lastUpdated: timestamp }