Skip to content

Commit 6e641a2

Browse files
jgarplindJoel
andauthored
Add ability to choose how many timeline events to sync when peeking (#4300)
* Add ability to choose how many timeline events to sync when peeking. * Add a test that covers the new method parameter. * Formatting. --------- Co-authored-by: Joel <joel.garplind+github@gmail.com>
1 parent b36682c commit 6e641a2

File tree

3 files changed

+63
-58
lines changed

3 files changed

+63
-58
lines changed

spec/integ/matrix-client-syncing.spec.ts

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,67 +2281,70 @@ describe("MatrixClient syncing", () => {
22812281
httpBackend!.expectedRequests = [];
22822282
});
22832283

2284-
it("should return a room based on the room initialSync API", async () => {
2285-
httpBackend!.when("GET", `/rooms/${encodeURIComponent(roomOne)}/initialSync`).respond(200, {
2286-
room_id: roomOne,
2287-
membership: KnownMembership.Leave,
2288-
messages: {
2289-
start: "start",
2290-
end: "end",
2291-
chunk: [
2284+
it.each([undefined, 100])(
2285+
"should return a room based on the room initialSync API with limit %s",
2286+
async (limit) => {
2287+
httpBackend!.when("GET", `/rooms/${encodeURIComponent(roomOne)}/initialSync`).respond(200, {
2288+
room_id: roomOne,
2289+
membership: KnownMembership.Leave,
2290+
messages: {
2291+
start: "start",
2292+
end: "end",
2293+
chunk: [
2294+
{
2295+
content: { body: "Message 1" },
2296+
type: "m.room.message",
2297+
event_id: "$eventId1",
2298+
sender: userA,
2299+
origin_server_ts: 12313525,
2300+
room_id: roomOne,
2301+
},
2302+
{
2303+
content: { body: "Message 2" },
2304+
type: "m.room.message",
2305+
event_id: "$eventId2",
2306+
sender: userB,
2307+
origin_server_ts: 12315625,
2308+
room_id: roomOne,
2309+
},
2310+
],
2311+
},
2312+
state: [
22922313
{
2293-
content: { body: "Message 1" },
2294-
type: "m.room.message",
2295-
event_id: "$eventId1",
2314+
content: { name: "Room Name" },
2315+
type: "m.room.name",
2316+
event_id: "$eventId",
22962317
sender: userA,
2297-
origin_server_ts: 12313525,
2318+
origin_server_ts: 12314525,
2319+
state_key: "",
22982320
room_id: roomOne,
22992321
},
2322+
],
2323+
presence: [
23002324
{
2301-
content: { body: "Message 2" },
2302-
type: "m.room.message",
2303-
event_id: "$eventId2",
2304-
sender: userB,
2305-
origin_server_ts: 12315625,
2306-
room_id: roomOne,
2325+
content: {},
2326+
type: "m.presence",
2327+
sender: userA,
23072328
},
23082329
],
2309-
},
2310-
state: [
2311-
{
2312-
content: { name: "Room Name" },
2313-
type: "m.room.name",
2314-
event_id: "$eventId",
2315-
sender: userA,
2316-
origin_server_ts: 12314525,
2317-
state_key: "",
2318-
room_id: roomOne,
2319-
},
2320-
],
2321-
presence: [
2322-
{
2323-
content: {},
2324-
type: "m.presence",
2325-
sender: userA,
2326-
},
2327-
],
2328-
});
2329-
httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
2330-
2331-
const prom = client!.peekInRoom(roomOne);
2332-
await httpBackend!.flushAllExpected();
2333-
const room = await prom;
2334-
2335-
expect(room.roomId).toBe(roomOne);
2336-
expect(room.getMyMembership()).toBe(KnownMembership.Leave);
2337-
expect(room.name).toBe("Room Name");
2338-
expect(room.currentState.getStateEvents("m.room.name", "")?.getId()).toBe("$eventId");
2339-
expect(room.timeline[0].getContent().body).toBe("Message 1");
2340-
expect(room.timeline[1].getContent().body).toBe("Message 2");
2341-
client?.stopPeeking();
2342-
httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
2343-
await httpBackend!.flushAllExpected();
2344-
});
2330+
});
2331+
httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
2332+
2333+
const prom = client!.peekInRoom(roomOne, limit);
2334+
await httpBackend!.flushAllExpected();
2335+
const room = await prom;
2336+
2337+
expect(room.roomId).toBe(roomOne);
2338+
expect(room.getMyMembership()).toBe(KnownMembership.Leave);
2339+
expect(room.name).toBe("Room Name");
2340+
expect(room.currentState.getStateEvents("m.room.name", "")?.getId()).toBe("$eventId");
2341+
expect(room.timeline[0].getContent().body).toBe("Message 1");
2342+
expect(room.timeline[1].getContent().body).toBe("Message 2");
2343+
client?.stopPeeking();
2344+
httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
2345+
await httpBackend!.flushAllExpected();
2346+
},
2347+
);
23452348
});
23462349

23472350
describe("user account data", () => {

src/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6603,13 +6603,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
66036603
* Peek into a room and receive updates about the room. This only works if the
66046604
* history visibility for the room is world_readable.
66056605
* @param roomId - The room to attempt to peek into.
6606+
* @param limit - The number of timeline events to initially retrieve.
66066607
* @returns Promise which resolves: Room object
66076608
* @returns Rejects: with an error response.
66086609
*/
6609-
public peekInRoom(roomId: string): Promise<Room> {
6610+
public peekInRoom(roomId: string, limit: number = 20): Promise<Room> {
66106611
this.peekSync?.stopPeeking();
66116612
this.peekSync = new SyncApi(this, this.clientOpts, this.buildSyncApiOptions());
6612-
return this.peekSync.peek(roomId);
6613+
return this.peekSync.peek(roomId, limit);
66136614
}
66146615

66156616
/**

src/sync.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,17 +401,18 @@ export class SyncApi {
401401
* Peek into a room. This will result in the room in question being synced so it
402402
* is accessible via getRooms(). Live updates for the room will be provided.
403403
* @param roomId - The room ID to peek into.
404+
* @param limit - The number of timeline events to initially retrieve.
404405
* @returns A promise which resolves once the room has been added to the
405406
* store.
406407
*/
407-
public peek(roomId: string): Promise<Room> {
408+
public peek(roomId: string, limit: number = 20): Promise<Room> {
408409
if (this._peekRoom?.roomId === roomId) {
409410
return Promise.resolve(this._peekRoom);
410411
}
411412

412413
const client = this.client;
413414
this._peekRoom = this.createRoom(roomId);
414-
return this.client.roomInitialSync(roomId, 20).then((response) => {
415+
return this.client.roomInitialSync(roomId, limit).then((response) => {
415416
if (this._peekRoom?.roomId !== roomId) {
416417
throw new Error("Peeking aborted");
417418
}

0 commit comments

Comments
 (0)