Skip to content

Commit 669408d

Browse files
authored
Add getEventNearestToTimestamp (#38)
2 parents ea8c166 + 703e625 commit 669408d

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

src/MatrixClient.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,18 @@ export class MatrixClient extends EventEmitter {
998998
};
999999
}
10001000

1001+
/**
1002+
* Get the nearest event to a given timestamp, either forwards or backwards.
1003+
* @param roomId The room ID to get the context in.
1004+
* @param ts The event ID to get the context of.
1005+
* @param dir The maximum number of events to return on either side of the event.
1006+
* @returns The ID and origin server timestamp of the event.
1007+
*/
1008+
@timedMatrixClientFunctionCall()
1009+
public async getEventNearestToTimestamp(roomId: string, ts: number, dir: "f"|"b"): Promise<{event_id: string, origin_server_ts: number}> {
1010+
return await this.doRequest("GET", "/_matrix/client/v1/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir });
1011+
}
1012+
10011013
/**
10021014
* Gets the profile for a given user
10031015
* @param {string} userId the user ID to lookup

src/SynapseAdminApis.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,16 @@ export class SynapseAdminApis {
479479
public async makeRoomAdmin(roomId: string, userId?: string): Promise<void> {
480480
return this.client.doRequest("POST", `/_synapse/admin/v1/rooms/${encodeURIComponent(roomId)}/make_room_admin`, {}, { user_id: userId });
481481
}
482+
483+
/**
484+
* Get the nearest event to a given timestamp, either forwards or backwards. You do not
485+
* need to be joined to the room to retrieve this information.
486+
* @param roomId The room ID to get the context in.
487+
* @param ts The event ID to get the context of.
488+
* @param dir The maximum number of events to return on either side of the event.
489+
* @returns The ID and origin server timestamp of the event.
490+
*/
491+
public async getEventNearestToTimestamp(roomId: string, ts: number, dir: "f"|"b"): Promise<{event_id: string, origin_server_ts: number}> {
492+
return await this.client.doRequest("GET", "/_synapse/admin/v1/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir });
493+
}
482494
}

test/MatrixClientTest.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,34 @@ describe('MatrixClient', () => {
27572757
});
27582758
});
27592759

2760+
describe('getEventNearestToTimestamp', () => {
2761+
it('should use the right endpoint', async () => {
2762+
const { client, http, hsUrl } = createTestClient();
2763+
const roomId = "!abc123:example.org";
2764+
const dir = "f";
2765+
const timestamp = 1234;
2766+
2767+
const eventId = "$def456:example.org";
2768+
const originServerTs = 4567;
2769+
2770+
http.when("GET", "/_matrix/client/v1/rooms").respond(200, (path, _content, req) => {
2771+
expect(path).toEqual(`${hsUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`);
2772+
expect(req.queryParams['dir']).toEqual(dir);
2773+
expect(req.queryParams['ts']).toEqual(timestamp);
2774+
2775+
return {
2776+
event_id: eventId,
2777+
origin_server_ts: originServerTs,
2778+
};
2779+
});
2780+
2781+
const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]);
2782+
expect(result).toBeDefined();
2783+
expect(result.event_id).toEqual(eventId);
2784+
expect(result.origin_server_ts).toEqual(originServerTs);
2785+
});
2786+
});
2787+
27602788
describe('getUserProfile', () => {
27612789
it('should call the right endpoint', async () => {
27622790
const { client, http, hsUrl } = createTestClient();

test/SynapseAdminApisTest.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,33 @@ describe('SynapseAdminApis', () => {
531531
await Promise.all([client.makeRoomAdmin(roomId, userId), http.flushAllExpected()]);
532532
});
533533
});
534+
535+
describe('getEventNearestToTimestamp', () => {
536+
it('should use the right endpoint', async () => {
537+
const { client, http, hsUrl } = createTestSynapseAdminClient();
538+
const roomId = "!abc123:example.org";
539+
const dir = "f";
540+
const timestamp = 1234;
541+
542+
const eventId = "$def456:example.org";
543+
const originServerTs = 4567;
544+
545+
http.when("GET", "/_synapse/admin/v1/rooms").respond(200, (path, _content, req) => {
546+
expect(path).toEqual(`${hsUrl}/_synapse/admin/v1/rooms/${encodeURIComponent(roomId)}/timestamp_to_event`);
547+
expect(req.queryParams['dir']).toEqual(dir);
548+
expect(req.queryParams['ts']).toEqual(timestamp);
549+
550+
return {
551+
event_id: eventId,
552+
origin_server_ts: originServerTs,
553+
};
554+
});
555+
556+
const [result] = await Promise.all([client.getEventNearestToTimestamp(roomId, timestamp, dir), http.flushAllExpected()]);
557+
expect(result).toBeDefined();
558+
expect(result.event_id).toEqual(eventId);
559+
expect(result.origin_server_ts).toEqual(originServerTs);
560+
});
561+
});
534562
});
535563
});

0 commit comments

Comments
 (0)