Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getEventNearestToTimestamp #351

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/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 @@ -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/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);
});
});
});
});