Skip to content

Commit

Permalink
Correct the dir parameter of MSC3715
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Henneke <dominik.henneke@nordeck.net>
  • Loading branch information
dhenneke committed Oct 10, 2022
1 parent fe2c350 commit a75e5b7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
112 changes: 112 additions & 0 deletions spec/integ/matrix-client-relations.spec.ts
@@ -0,0 +1,112 @@
/*
Copyright 2022 Dominik Henneke
Copyright 2022 Nordeck IT + Consulting GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import HttpBackend from "matrix-mock-request";

import { Direction, MatrixClient, MatrixScheduler } from "../../src/matrix";
import { TestClient } from "../TestClient";

describe("MatrixClient relations", () => {
const userId = "@alice:localhost";
const accessToken = "aseukfgwef";
const roomId = "!room:here";
let client: MatrixClient | undefined;
let httpBackend: HttpBackend | undefined;

const setupTests = (): [MatrixClient, HttpBackend] => {
const scheduler = new MatrixScheduler();
const testClient = new TestClient(
userId,
"DEVICE",
accessToken,
undefined,
{ scheduler },
);
const httpBackend = testClient.httpBackend;
const client = testClient.client;

return [client, httpBackend];
};

beforeEach(() => {
[client, httpBackend] = setupTests();
});

afterEach(() => {
httpBackend!.verifyNoOutstandingExpectation();
return httpBackend!.stop();
});

it("should read related events with the default options", async () => {
const response = client!.relations(roomId, '$event-0', null, null);

httpBackend!
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b")
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();

expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
});

it("should read related events with relation type", async () => {
const response = client!.relations(roomId, '$event-0', 'm.reference', null);

httpBackend!
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b")
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();

expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
});

it("should read related events with relation type and event type", async () => {
const response = client!.relations(roomId, '$event-0', 'm.reference', 'm.room.message');

httpBackend!
.when(
"GET",
"/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b",
)
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();

expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
});

it("should read related events with custom options", async () => {
const response = client!.relations(roomId, '$event-0', null, null, {
dir: Direction.Forward,
from: 'FROM',
limit: 10,
to: 'TO',
});

httpBackend!
.when(
"GET",
"/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO",
)
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();

expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
});
});
2 changes: 1 addition & 1 deletion src/@types/requests.ts
Expand Up @@ -160,7 +160,7 @@ export interface IRelationsRequestOpts {
from?: string;
to?: string;
limit?: number;
direction?: Direction;
dir?: Direction;
}

export interface IRelationsResponse {
Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Expand Up @@ -5399,7 +5399,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (Thread.hasServerSideSupport && timelineSet.thread) {
const thread = timelineSet.thread;
const opts: IRelationsRequestOpts = {
direction: Direction.Backward,
dir: Direction.Backward,
limit: 50,
};

Expand Down Expand Up @@ -6920,7 +6920,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
eventId: string,
relationType?: RelationType | string | null,
eventType?: EventType | string | null,
opts: IRelationsRequestOpts = { direction: Direction.Backward },
opts: IRelationsRequestOpts = { dir: Direction.Backward },
): Promise<{
originalEvent: MatrixEvent;
events: MatrixEvent[];
Expand Down Expand Up @@ -7498,7 +7498,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
eventId: string,
relationType?: RelationType | string | null,
eventType?: EventType | string | null,
opts: IRelationsRequestOpts = { direction: Direction.Backward },
opts: IRelationsRequestOpts = { dir: Direction.Backward },
): Promise<IRelationsResponse> {
const queryString = utils.encodeParams(opts as Record<string, string | number>);

Expand Down
4 changes: 2 additions & 2 deletions src/models/thread.ts
Expand Up @@ -406,7 +406,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
return this.timelineSet.getLiveTimeline();
}

public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, direction: Direction.Backward }): Promise<{
public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, dir: Direction.Backward }): Promise<{
originalEvent: MatrixEvent;
events: MatrixEvent[];
nextBatch?: string | null;
Expand Down Expand Up @@ -438,7 +438,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
return this.client.decryptEventIfNeeded(event);
}));

const prependEvents = (opts.direction ?? Direction.Backward) === Direction.Backward;
const prependEvents = (opts.dir ?? Direction.Backward) === Direction.Backward;

this.timelineSet.addEventsToTimeline(
events,
Expand Down

0 comments on commit a75e5b7

Please sign in to comment.