Skip to content

Commit

Permalink
Use stable API prefix for 3PID APIs when supported
Browse files Browse the repository at this point in the history
If the server advertises spec version r0.6.0, it must have the 3PID APIs
available under the stable API prefix.

Fixes element-hq/element-web#11246
  • Loading branch information
jryans committed Nov 6, 2019
1 parent 35adb75 commit 20f5c3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/base-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -1374,12 +1374,12 @@ MatrixBaseApis.prototype.addThreePid = function(creds, bind, callback) {
* @return {module:client.Promise} Resolves: on success
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixBaseApis.prototype.addThreePidOnly = function(data) {
MatrixBaseApis.prototype.addThreePidOnly = async function(data) {
const path = "/account/3pid/add";
const prefix = await this.isVersionSupported("r0.6.0") ?
httpApi.PREFIX_R0 : httpApi.PREFIX_UNSTABLE;
return this._http.authedRequest(
undefined, "POST", path, null, data, {
prefix: httpApi.PREFIX_UNSTABLE,
},
undefined, "POST", path, null, data, { prefix },
);
};

Expand All @@ -1397,12 +1397,12 @@ MatrixBaseApis.prototype.addThreePidOnly = function(data) {
* @return {module:client.Promise} Resolves: on success
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixBaseApis.prototype.bindThreePid = function(data) {
MatrixBaseApis.prototype.bindThreePid = async function(data) {
const path = "/account/3pid/bind";
const prefix = await this.isVersionSupported("r0.6.0") ?
httpApi.PREFIX_R0 : httpApi.PREFIX_UNSTABLE;
return this._http.authedRequest(
undefined, "POST", path, null, data, {
prefix: httpApi.PREFIX_UNSTABLE,
},
undefined, "POST", path, null, data, { prefix },
);
};

Expand All @@ -1417,17 +1417,17 @@ MatrixBaseApis.prototype.bindThreePid = function(data) {
* @return {module:client.Promise} Resolves: on success
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixBaseApis.prototype.unbindThreePid = function(medium, address) {
MatrixBaseApis.prototype.unbindThreePid = async function(medium, address) {
const path = "/account/3pid/unbind";
const data = {
medium,
address,
id_server: this.getIdentityServerUrl(true),
};
const prefix = await this.isVersionSupported("r0.6.0") ?
httpApi.PREFIX_R0 : httpApi.PREFIX_UNSTABLE;
return this._http.authedRequest(
undefined, "POST", path, null, data, {
prefix: httpApi.PREFIX_UNSTABLE,
},
undefined, "POST", path, null, data, { prefix },
);
};

Expand Down
10 changes: 10 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4210,6 +4210,16 @@ MatrixClient.prototype.getVersions = async function() {
return this._serverVersionsCache;
};

/**
* Check if a particular spec version is supported by the server.
* @param {string} version The spec version (such as "r0.5.0") to check for.
* @return {Promise<bool>} Whether it is supported
*/
MatrixClient.prototype.isVersionSupported = async function(version) {
const { versions } = await this.getVersions();
return versions && versions.includes(version);
};

/**
* Query the server to see if it support members lazy loading
* @return {Promise<boolean>} true if server supports lazy loading
Expand Down

0 comments on commit 20f5c3e

Please sign in to comment.