Skip to content

Commit

Permalink
Add getEventNearestToTimestamp (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr authored Nov 24, 2023
2 parents ea8c166 + 703e625 commit 669408d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,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/v1/rooms/" + encodeURIComponent(roomId) + "/timestamp_to_event", { ts, dir });
}

/**
* Gets the profile for a given user
* @param {string} userId the user ID to lookup
Expand Down
12 changes: 12 additions & 0 deletions src/SynapseAdminApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,16 @@ export class SynapseAdminApis {
public async makeRoomAdmin(roomId: string, userId?: string): Promise<void> {
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 });
}
}
28 changes: 28 additions & 0 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,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/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);

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).toEqual(originServerTs);
});
});

describe('getUserProfile', () => {
it('should call the right endpoint', async () => {
const { client, http, hsUrl } = createTestClient();
Expand Down
28 changes: 28 additions & 0 deletions test/SynapseAdminApisTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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).toEqual(originServerTs);
});
});
});
});

0 comments on commit 669408d

Please sign in to comment.