From dd23a1a40152949f7411f66f4da12517835715c4 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 15 Dec 2021 05:06:26 -0600 Subject: [PATCH] Add support for MSC3030 `/timestamp_to_event` (#2072) - `/jumptodate` slash command is being worked on in https://github.com/matrix-org/matrix-react-sdk/pull/7372 - Jump to date headers are being worked on in https://github.com/matrix-org/matrix-react-sdk/pull/7339 Related to https://github.com/vector-im/element-web/issues/7677 Part of MSC3030: https://github.com/matrix-org/matrix-doc/pull/3030 Experimental Synapse implementation added in https://github.com/matrix-org/synapse/pull/9445 --- src/client.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/client.ts b/src/client.ts index 4824b9063..a54161d6d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -728,6 +728,11 @@ interface IRoomHierarchy { rooms: IHierarchyRoom[]; next_batch?: string; } + +interface ITimestampToEventResponse { + event_id: string; + origin_server_ts: string; +} /* eslint-enable camelcase */ // We're using this constant for methods overloading and inspect whether a variable @@ -8937,6 +8942,36 @@ export class MatrixClient extends EventEmitter { public async whoami(): Promise<{ user_id: string }> { // eslint-disable-line camelcase return this.http.authedRequest(undefined, Method.Get, "/account/whoami"); } + + /** + * Find the event_id closest to the given timestamp in the given direction. + * @return {Promise} A promise of an object containing the event_id and + * origin_server_ts of the closest event to the timestamp in the given + * direction + */ + public async timestampToEvent( + roomId: string, + timestamp: number, + dir: Direction, + ): Promise { + const path = utils.encodeUri("/rooms/$roomId/timestamp_to_event", { + $roomId: roomId, + }); + + return await this.http.authedRequest( + undefined, + Method.Get, + path, + { + ts: timestamp.toString(), + dir: dir, + }, + undefined, + { + prefix: "/_matrix/client/unstable/org.matrix.msc3030", + }, + ); + } } /**