Skip to content

Commit

Permalink
Add capabilities API (#54)
Browse files Browse the repository at this point in the history
* Add capabilities API

* Fix property

* lint
  • Loading branch information
Half-Shot authored Feb 25, 2025
1 parent 99be87d commit 1fd998c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ import { IKeyBackupInfo, IKeyBackupInfoRetrieved, IKeyBackupInfoUnsigned, IKeyBa
import { MatrixError } from "./models/MatrixError";
import { MXCUrl } from "./models/MXCUrl";
import { MatrixContentScannerClient } from "./MatrixContentScannerClient";
import { MatrixCapabilities } from "./models/Capabilities";

const SYNC_BACKOFF_MIN_MS = 5000;
const SYNC_BACKOFF_MAX_MS = 15000;
const VERSIONS_CACHE_MS = 7200000; // 2 hours
const CAPABILITES_CACHE_MS = 7200000; // 2 hours

/**
* A client that is capable of interacting with a matrix homeserver.
Expand Down Expand Up @@ -106,6 +108,8 @@ export class MatrixClient extends EventEmitter {
private readonly unstableApisInstance = new UnstableApis(this);
private cachedVersions: ServerVersions;
private versionsLastFetched = 0;
private cachedCapabilites: MatrixCapabilities;
private capabilitesLastFetched = 0;

/**
* Set this to true to have the client only persist the sync token after the sync
Expand Down Expand Up @@ -275,6 +279,20 @@ export class MatrixClient extends EventEmitter {
return event;
}

/**
* Get the set of capabilites for the authenticated client.
* @returns {Promise<MatrixCapabilities>} Resolves to the server's supported versions.
*/
@timedMatrixClientFunctionCall()
public async getCapabilities(): Promise<MatrixCapabilities> {
if (!this.cachedCapabilites || (Date.now() - this.capabilitesLastFetched) >= CAPABILITES_CACHE_MS) {
this.cachedCapabilites = (await this.doRequest("GET", "/_matrix/client/v3/capabilities")).capabilities;
this.capabilitesLastFetched = Date.now();
}

return this.cachedCapabilites;
}

/**
* Retrieves the server's supported specification versions and unstable features.
* @returns {Promise<ServerVersions>} Resolves to the server's supported versions.
Expand Down
37 changes: 37 additions & 0 deletions src/models/Capabilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

type BooleanCapability = { enabled: boolean };

export interface MatrixCapabilities {
/**
* Is the user able to add, remove, or change 3PID associations on their account .
*/
"m.3pid_changes"?: BooleanCapability;
/**
* Is the user able to change their own password.
*/
"m.change_password"?: BooleanCapability;
/**
* Is the user able to generate single-use, time-limited tokens via the API.
*/
"m.get_login_token"?: BooleanCapability;
/**
* Is the user able to change their own avatar_url via profile endpoints.
*/
"m.set_avatar_url"?: BooleanCapability;
/**
* Is the user able to change their own display name via profile endpoints.
*/
"m.set_displayname"?: BooleanCapability;
/**
* Describes the default and available room versions a server supports, and at what level of stability.
*
* Any room version not marked as "stable" should be considered "unstable"
*/
"m.room_versions"?: {
"available": {
[version: string]: "stable"|string;
};
"default": string;
};
[key: string]: unknown;
}

0 comments on commit 1fd998c

Please sign in to comment.