Skip to content

Commit 79f3e2b

Browse files
committed
Correct the dir parameter of MSC3715
Signed-off-by: Dominik Henneke <[email protected]>
1 parent fe2c350 commit 79f3e2b

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import HttpBackend from "matrix-mock-request";
18+
19+
import { Direction, MatrixClient, MatrixScheduler } from "../../src/matrix";
20+
import { TestClient } from "../TestClient";
21+
22+
describe("MatrixClient relations", () => {
23+
const userId = "@alice:localhost";
24+
const accessToken = "aseukfgwef";
25+
const roomId = "!room:here";
26+
let client: MatrixClient | undefined;
27+
let httpBackend: HttpBackend | undefined;
28+
29+
const setupTests = (): [MatrixClient, HttpBackend] => {
30+
const scheduler = new MatrixScheduler();
31+
const testClient = new TestClient(
32+
userId,
33+
"DEVICE",
34+
accessToken,
35+
undefined,
36+
{ scheduler },
37+
);
38+
const httpBackend = testClient.httpBackend;
39+
const client = testClient.client;
40+
41+
return [client, httpBackend];
42+
};
43+
44+
beforeEach(() => {
45+
[client, httpBackend] = setupTests();
46+
});
47+
48+
afterEach(() => {
49+
httpBackend!.verifyNoOutstandingExpectation();
50+
return httpBackend!.stop();
51+
});
52+
53+
it("should read related events with the default options", async () => {
54+
const response = client!.relations(roomId, '$event-0', null, null);
55+
56+
httpBackend!
57+
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b")
58+
.respond(200, { chunk: [], next_batch: 'NEXT' });
59+
60+
await httpBackend!.flushAllExpected();
61+
62+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
63+
});
64+
65+
it("should read related events with relation type", async () => {
66+
const response = client!.relations(roomId, '$event-0', 'm.reference', null);
67+
68+
httpBackend!
69+
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b")
70+
.respond(200, { chunk: [], next_batch: 'NEXT' });
71+
72+
await httpBackend!.flushAllExpected();
73+
74+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
75+
});
76+
77+
it("should read related events with relation type and event type", async () => {
78+
const response = client!.relations(roomId, '$event-0', 'm.reference', 'm.room.message');
79+
80+
httpBackend!
81+
.when(
82+
"GET",
83+
"/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b",
84+
)
85+
.respond(200, { chunk: [], next_batch: 'NEXT' });
86+
87+
await httpBackend!.flushAllExpected();
88+
89+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
90+
});
91+
92+
it("should read related events with custom options", async () => {
93+
const response = client!.relations(roomId, '$event-0', null, null, {
94+
dir: Direction.Forward,
95+
from: 'FROM',
96+
limit: 10,
97+
to: 'TO',
98+
});
99+
100+
httpBackend!
101+
.when(
102+
"GET",
103+
"/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO",
104+
)
105+
.respond(200, { chunk: [], next_batch: 'NEXT' });
106+
107+
await httpBackend!.flushAllExpected();
108+
109+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
110+
});
111+
});

src/@types/requests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export interface IRelationsRequestOpts {
160160
from?: string;
161161
to?: string;
162162
limit?: number;
163-
direction?: Direction;
163+
dir?: Direction;
164164
}
165165

166166
export interface IRelationsResponse {

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5399,7 +5399,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
53995399
if (Thread.hasServerSideSupport && timelineSet.thread) {
54005400
const thread = timelineSet.thread;
54015401
const opts: IRelationsRequestOpts = {
5402-
direction: Direction.Backward,
5402+
dir: Direction.Backward,
54035403
limit: 50,
54045404
};
54055405

@@ -6920,7 +6920,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
69206920
eventId: string,
69216921
relationType?: RelationType | string | null,
69226922
eventType?: EventType | string | null,
6923-
opts: IRelationsRequestOpts = { direction: Direction.Backward },
6923+
opts: IRelationsRequestOpts = { dir: Direction.Backward },
69246924
): Promise<{
69256925
originalEvent: MatrixEvent;
69266926
events: MatrixEvent[];
@@ -7498,7 +7498,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
74987498
eventId: string,
74997499
relationType?: RelationType | string | null,
75007500
eventType?: EventType | string | null,
7501-
opts: IRelationsRequestOpts = { direction: Direction.Backward },
7501+
opts: IRelationsRequestOpts = { dir: Direction.Backward },
75027502
): Promise<IRelationsResponse> {
75037503
const queryString = utils.encodeParams(opts as Record<string, string | number>);
75047504

src/models/thread.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
406406
return this.timelineSet.getLiveTimeline();
407407
}
408408

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

441-
const prependEvents = (opts.direction ?? Direction.Backward) === Direction.Backward;
441+
const prependEvents = (opts.dir ?? Direction.Backward) === Direction.Backward;
442442

443443
this.timelineSet.addEventsToTimeline(
444444
events,

0 commit comments

Comments
 (0)