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

Fix & make GET /relations stable #264

Merged
merged 2 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/MatrixClient.ts
Expand Up @@ -1955,6 +1955,26 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* Get relations for a given event.
* @param {string} roomId The room ID to for the given event.
* @param {string} eventId The event ID to list reacations for.
* @param {string?} relationType The type of reaction (e.g. `m.room.member`) to filter for. Optional.
* @param {string?} eventType The type of event to look for (e.g. `m.room.member`). Optional.
* @returns {Promise<{chunk: any[]}>} Resolves to an object containing the original event, and a chunk of relations
*/
@timedMatrixClientFunctionCall()
public async getRelationsForEvent(roomId: string, eventId: string, relationType?: string, eventType?: string): Promise<{ chunk: any[] }> {
let url = `/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(eventId)}`;
if (relationType) {
url += `/${relationType}`;
}
if (eventType) {
url += `/${eventType}`;
}
return this.doRequest("GET", url);
}

/**
* Performs a web request to the homeserver, applying appropriate authorization headers for
* this client.
Expand Down
5 changes: 3 additions & 2 deletions src/UnstableApis.ts
Expand Up @@ -43,9 +43,10 @@ export class UnstableApis {
* @param {string} eventId The event ID to list reacations for.
* @param {string?} relationType The type of reaction (e.g. `m.room.member`) to filter for. Optional.
* @param {string?} eventType The type of event to look for (e.g. `m.room.member`). Optional.
* @returns {Promise<{original_event: any, chunk: any[]}>} Resolves to an object containing the original event, and a chunk of relations
* @returns {Promise<{chunk: any[]}>} Resolves to an object containing the original event, and a chunk of relations
* @deprecated Please use the function of the same name in MatrixClient. This will be removed in a future release.
*/
public async getRelationsForEvent(roomId: string, eventId: string, relationType?: string, eventType?: string): Promise<{ original_event: any, chunk: any[] }> {
public async getRelationsForEvent(roomId: string, eventId: string, relationType?: string, eventType?: string): Promise<{ chunk: any[] }> {
let url = `/_matrix/client/unstable/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(eventId)}`;
if (relationType) {
url += `/${relationType}`;
Expand Down
32 changes: 32 additions & 0 deletions test/MatrixClientTest.ts
Expand Up @@ -6939,6 +6939,38 @@ describe('MatrixClient', () => {
});
});

describe('getRelationsForEvent', () => {
test.each([
[null, null],
['org.example.relation', null],
['org.example.relation', 'org.example.event_type'],
])("should call the right endpoint for rel=%p and type=%p", async (relType, eventType) => {
const { client, http, hsUrl } = createTestClient();

const roomId = "!room:example.org";
const eventId = "$event";
const response = {
chunk: [
{ eventContents: true },
{ eventContents: true },
{ eventContents: true },
],
};

http.when("GET", "/_matrix/client/v1/rooms").respond(200, (path, content) => {
const relTypeComponent = relType ? `/${encodeURIComponent(relType)}` : '';
const eventTypeComponent = eventType ? `/${encodeURIComponent(eventType)}` : '';
// eslint-disable-next-line max-len
const idx = path.indexOf(`${hsUrl}/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(eventId)}${relTypeComponent}${eventTypeComponent}`);
expect(idx).toBe(0);
return response;
});

const [result] = await Promise.all([client.getRelationsForEvent(roomId, eventId, relType, eventType), http.flushAllExpected()]);
expect(result).toEqual(response);
});
});

describe('redactObjectForLogging', () => {
it('should redact multilevel objects', () => {
const input = {
Expand Down
1 change: 0 additions & 1 deletion test/UnstableApisTest.ts
Expand Up @@ -78,7 +78,6 @@ describe('UnstableApis', () => {
const roomId = "!room:example.org";
const eventId = "$event";
const response = {
original_event: { eventContents: true },
chunk: [
{ eventContents: true },
{ eventContents: true },
Expand Down