From 686a499e118eda4ab560777fbe89e35753265e84 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 22 Nov 2023 14:06:15 +0000 Subject: [PATCH 1/5] Add getEventNearestToTimestamp --- src/MatrixClient.ts | 12 ++++++++++++ src/SynapseAdminApis.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/MatrixClient.ts b/src/MatrixClient.ts index f9ca9e44..86f740a3 100644 --- a/src/MatrixClient.ts +++ b/src/MatrixClient.ts @@ -996,6 +996,18 @@ export class MatrixClient extends EventEmitter { }; } + /** + * Get the nearest event to a given timestamp, either forwards or backwards. + * @param roomId The room ID to get the context in. + * @param ts The event ID to get the context of. + * @param dir The maximum number of events to return on either side of the event. + * @returns The ID and origin server timestamp of the event. + */ + @timedMatrixClientFunctionCall() + public async getEventNearestToTimestamp(roomId: string, ts: number, dir: "f"|"b"): Promise<{event_id: string, origin_server_ts: number}> { + return await this.doRequest("GET", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir }); + } + /** * Gets the profile for a given user * @param {string} userId the user ID to lookup diff --git a/src/SynapseAdminApis.ts b/src/SynapseAdminApis.ts index 84bcdecd..fc4b8ebd 100644 --- a/src/SynapseAdminApis.ts +++ b/src/SynapseAdminApis.ts @@ -479,4 +479,16 @@ export class SynapseAdminApis { public async makeRoomAdmin(roomId: string, userId?: string): Promise { return this.client.doRequest("POST", `/_synapse/admin/v1/rooms/${encodeURIComponent(roomId)}/make_room_admin`, {}, { user_id: userId }); } + + /** + * Get the nearest event to a given timestamp, either forwards or backwards. You do not + * need to be joined to the room to retrieve this information. + * @param roomId The room ID to get the context in. + * @param ts The event ID to get the context of. + * @param dir The maximum number of events to return on either side of the event. + * @returns The ID and origin server timestamp of the event. + */ + public async getEventNearestToTimestamp(roomId: string, ts: number, dir: "f"|"b"): Promise<{event_id: string, origin_server_ts: number}> { + return await this.client.doRequest("GET", "/_synapse/admin/v1/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir }); + } } From 9c6835e03a6d7fdb5508bea853b8d3e7fc25bfd5 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 22 Nov 2023 14:06:18 +0000 Subject: [PATCH 2/5] Add tests for getEventNearestToTimestamp --- test/MatrixClientTest.ts | 28 ++++++++++++++++++++++++++++ test/SynapseAdminApisTest.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/test/MatrixClientTest.ts b/test/MatrixClientTest.ts index 6abdf93e..a8c4a373 100644 --- a/test/MatrixClientTest.ts +++ b/test/MatrixClientTest.ts @@ -2759,6 +2759,34 @@ describe('MatrixClient', () => { }); }); + describe('getEventNearestToTimestamp', () => { + it('should use the right endpoint', async () => { + const { client, http, hsUrl } = createTestClient(); + const roomId = "!abc123:example.org"; + const dir = "f"; + const timestamp = 1234; + + const eventId = "$def456:example.org"; + const originServerTs = 4567; + + http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, _content, req) => { + expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); + expect(req.queryParams['dir']).toEqual(dir); + expect(req.queryParams['ts']).toEqual(timestamp.toString()); + + return { + event_id: eventId, + origin_server_ts: originServerTs, + }; + }); + + const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]); + expect(result).toBeDefined(); + expect(result.event_id).toEqual(eventId); + expect(result.origin_server_ts).toMatchObject(originServerTs); + }); + }); + describe('getUserProfile', () => { it('should call the right endpoint', async () => { const { client, http, hsUrl } = createTestClient(); diff --git a/test/SynapseAdminApisTest.ts b/test/SynapseAdminApisTest.ts index b9d9f5f6..b0e62cf1 100644 --- a/test/SynapseAdminApisTest.ts +++ b/test/SynapseAdminApisTest.ts @@ -531,5 +531,33 @@ describe('SynapseAdminApis', () => { await Promise.all([client.makeRoomAdmin(roomId, userId), http.flushAllExpected()]); }); }); + + describe('getEventNearestToTimestamp', () => { + it('should use the right endpoint', async () => { + const { client, http, hsUrl } = createTestSynapseAdminClient(); + const roomId = "!abc123:example.org"; + const dir = "f"; + const timestamp = 1234; + + const eventId = "$def456:example.org"; + const originServerTs = 4567; + + http.when("GET", "/_synapse/admin/v1/rooms").respond(200, (path, _content, req) => { + expect(path).toEqual(`${hsUrl}/_synapse/admin/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); + expect(req.queryParams['dir']).toEqual(dir); + expect(req.queryParams['ts']).toEqual(timestamp.toString()); + + return { + event_id: eventId, + origin_server_ts: originServerTs, + }; + }); + + const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]); + expect(result).toBeDefined(); + expect(result.event_id).toEqual(eventId); + expect(result.origin_server_ts).toMatchObject(originServerTs); + }); + }); }); }); From 06e66df6d3a6b9011de6df3ae5be10299683ffc6 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 22 Nov 2023 14:19:32 +0000 Subject: [PATCH 3/5] v1 --- src/MatrixClient.ts | 2 +- test/MatrixClientTest.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MatrixClient.ts b/src/MatrixClient.ts index 86f740a3..808377bd 100644 --- a/src/MatrixClient.ts +++ b/src/MatrixClient.ts @@ -1005,7 +1005,7 @@ export class MatrixClient extends EventEmitter { */ @timedMatrixClientFunctionCall() public async getEventNearestToTimestamp(roomId: string, ts: number, dir: "f"|"b"): Promise<{event_id: string, origin_server_ts: number}> { - return await this.doRequest("GET", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir }); + return await this.doRequest("GET", "/_matrix/client/v1/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir }); } /** diff --git a/test/MatrixClientTest.ts b/test/MatrixClientTest.ts index a8c4a373..53b419b6 100644 --- a/test/MatrixClientTest.ts +++ b/test/MatrixClientTest.ts @@ -2770,7 +2770,7 @@ describe('MatrixClient', () => { const originServerTs = 4567; http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, _content, req) => { - expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); + expect(path).toEqual(`${hsUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); expect(req.queryParams['dir']).toEqual(dir); expect(req.queryParams['ts']).toEqual(timestamp.toString()); From cf050597c17438761d5e0c9b30e78cb8d1b250b6 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 23 Nov 2023 12:49:01 +0000 Subject: [PATCH 4/5] Fix test --- test/MatrixClientTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MatrixClientTest.ts b/test/MatrixClientTest.ts index 53b419b6..cadf7fba 100644 --- a/test/MatrixClientTest.ts +++ b/test/MatrixClientTest.ts @@ -2772,7 +2772,7 @@ describe('MatrixClient', () => { http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, _content, req) => { expect(path).toEqual(`${hsUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); expect(req.queryParams['dir']).toEqual(dir); - expect(req.queryParams['ts']).toEqual(timestamp.toString()); + expect(req.queryParams['ts']).toEqual(timestamp); return { event_id: eventId, From 703e625dc783cb2d738c08dd0ff0690b7f82c546 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 23 Nov 2023 16:42:09 +0000 Subject: [PATCH 5/5] Fix tests --- test/MatrixClientTest.ts | 4 ++-- test/SynapseAdminApisTest.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/MatrixClientTest.ts b/test/MatrixClientTest.ts index cadf7fba..c71e7d20 100644 --- a/test/MatrixClientTest.ts +++ b/test/MatrixClientTest.ts @@ -2769,7 +2769,7 @@ describe('MatrixClient', () => { const eventId = "$def456:example.org"; const originServerTs = 4567; - http.when("GET", "/_matrix/client/v3/rooms").respond(200, (path, _content, req) => { + http.when("GET", "/_matrix/client/v1/rooms").respond(200, (path, _content, req) => { expect(path).toEqual(`${hsUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); expect(req.queryParams['dir']).toEqual(dir); expect(req.queryParams['ts']).toEqual(timestamp); @@ -2783,7 +2783,7 @@ describe('MatrixClient', () => { const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]); expect(result).toBeDefined(); expect(result.event_id).toEqual(eventId); - expect(result.origin_server_ts).toMatchObject(originServerTs); + expect(result.origin_server_ts).toEqual(originServerTs); }); }); diff --git a/test/SynapseAdminApisTest.ts b/test/SynapseAdminApisTest.ts index b0e62cf1..1dcd8541 100644 --- a/test/SynapseAdminApisTest.ts +++ b/test/SynapseAdminApisTest.ts @@ -545,7 +545,7 @@ describe('SynapseAdminApis', () => { http.when("GET", "/_synapse/admin/v1/rooms").respond(200, (path, _content, req) => { expect(path).toEqual(`${hsUrl}/_synapse/admin/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`); expect(req.queryParams['dir']).toEqual(dir); - expect(req.queryParams['ts']).toEqual(timestamp.toString()); + expect(req.queryParams['ts']).toEqual(timestamp); return { event_id: eventId, @@ -556,7 +556,7 @@ describe('SynapseAdminApis', () => { const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]); expect(result).toBeDefined(); expect(result.event_id).toEqual(eventId); - expect(result.origin_server_ts).toMatchObject(originServerTs); + expect(result.origin_server_ts).toEqual(originServerTs); }); }); });