Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split getCapabilities into server / user #1017

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function MatrixClient(opts) {

this._serverSupportsLazyLoading = null;

this._cachedCapabilities = null; // { capabilities: {}, lastUpdated: timestamp }
this._cachedCapabilities = {}; // { user/server: { capabilities: {}, lastUpdated: timestamp } }

// The SDK doesn't really provide a clean way for events to recalculate the push
// actions for themselves, so we have to kinda help them out when they are encrypted.
Expand Down Expand Up @@ -490,28 +490,63 @@ MatrixClient.prototype.setNotifTimelineSet = function(notifTimelineSet) {
};

/**
* Gets the capabilities of the homeserver. Always returns an object of
* capability keys and their options, which may be empty.
* Gets the user-specific capabilities of the homeserver
* Always returns an object of capability keys and their options,
* which may be empty.
* @param {boolean} fresh True to ignore any cached values.
* @return {module:client.Promise} Resolves to the capabilities of the homeserver
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.getCapabilities = function(fresh=false) {
MatrixClient.prototype.getUserCapabilities = function(fresh=false) {
return this._getCapabilitiesOfKind('user', fresh);
};

/**
* Gets the non user-specific capabilities of the homeserver
* Always returns an object of capability keys and their options,
* which may be empty.
* @param {boolean} fresh True to ignore any cached values.
* @return {module:client.Promise} Resolves to the capabilities of the homeserver
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.getServerCapabilities = function(fresh=false) {
return this._getCapabilitiesOfKind('server', fresh);
};

MatrixClient.prototype._getCapabilitiesOfKind = function(kind, fresh=false) {
const now = new Date().getTime();

if (this._cachedCapabilities && !fresh) {
if (now < this._cachedCapabilities.expiration) {
if (this._cachedCapabilities[kind] && !fresh) {
if (now < this._cachedCapabilities[kind].expiration) {
logger.log("Returning cached capabilities");
return Promise.resolve(this._cachedCapabilities.capabilities);
return Promise.resolve(this._cachedCapabilities[kind].capabilities);
}
}

let prom;
if (kind === 'user') {
prom = this._http.authedRequest(
undefined, "GET", "/capabilities/user",
);
} else if (kind == 'server') {
prom = this._http.request(
undefined, "GET", "/capabilities/server",
);
} else {
throw new Error("Unknown capabilities kind: " + kind);
}

// We swallow errors because we need a default object anyhow
return this._http.authedRequest(
undefined, "GET", "/capabilities",
).catch((e) => {
logger.error(e);
return null; // otherwise consume the error
return prom.catch((e) => {
if (kind === 'user' && (e.httpStatus == 400 || e.httpStatus == 404)) {
// try old endpoint
return this._http.authedRequest(
undefined, "GET", "/capabilities",
);
} else {
logger.error(e);
return null; // otherwise consume the error
}
}).then((r) => {
if (!r) r = {};
const capabilities = r["capabilities"] || {};
Expand All @@ -522,16 +557,27 @@ MatrixClient.prototype.getCapabilities = function(fresh=false) {
? CAPABILITIES_CACHE_MS
: 60000 + (Math.random() * 5000);

this._cachedCapabilities = {
this._cachedCapabilities[kind] = {
capabilities: capabilities,
expiration: now + cacheMs,
};

logger.log("Caching capabilities: ", capabilities);
logger.log("Caching " + kind + " capabilities: ", capabilities);
return capabilities;
});
};

/**
* DEPRECATED
* Backwards compatibility alias for getUserCapabilities()
* @param {boolean} fresh True to ignore any cached values.
* @return {module:client.Promise} Resolves to the capabilities of the homeserver
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.getCapabilities = function(fresh=false) {
return this.getUserCapabilities();
};

// Crypto bits
// ===========

Expand Down