1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

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>
This commit is contained in:
Joel
2024-07-04 14:14:14 +02:00
committed by GitHub
parent b36682cb99
commit 6e641a28c0
3 changed files with 63 additions and 58 deletions

View File

@@ -2281,67 +2281,70 @@ describe("MatrixClient syncing", () => {
httpBackend!.expectedRequests = []; httpBackend!.expectedRequests = [];
}); });
it("should return a room based on the room initialSync API", async () => { it.each([undefined, 100])(
httpBackend!.when("GET", `/rooms/${encodeURIComponent(roomOne)}/initialSync`).respond(200, { "should return a room based on the room initialSync API with limit %s",
room_id: roomOne, async (limit) => {
membership: KnownMembership.Leave, httpBackend!.when("GET", `/rooms/${encodeURIComponent(roomOne)}/initialSync`).respond(200, {
messages: { room_id: roomOne,
start: "start", membership: KnownMembership.Leave,
end: "end", messages: {
chunk: [ start: "start",
end: "end",
chunk: [
{
content: { body: "Message 1" },
type: "m.room.message",
event_id: "$eventId1",
sender: userA,
origin_server_ts: 12313525,
room_id: roomOne,
},
{
content: { body: "Message 2" },
type: "m.room.message",
event_id: "$eventId2",
sender: userB,
origin_server_ts: 12315625,
room_id: roomOne,
},
],
},
state: [
{ {
content: { body: "Message 1" }, content: { name: "Room Name" },
type: "m.room.message", type: "m.room.name",
event_id: "$eventId1", event_id: "$eventId",
sender: userA, sender: userA,
origin_server_ts: 12313525, origin_server_ts: 12314525,
room_id: roomOne, state_key: "",
},
{
content: { body: "Message 2" },
type: "m.room.message",
event_id: "$eventId2",
sender: userB,
origin_server_ts: 12315625,
room_id: roomOne, room_id: roomOne,
}, },
], ],
}, presence: [
state: [ {
{ content: {},
content: { name: "Room Name" }, type: "m.presence",
type: "m.room.name", sender: userA,
event_id: "$eventId", },
sender: userA, ],
origin_server_ts: 12314525, });
state_key: "", httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
room_id: roomOne,
},
],
presence: [
{
content: {},
type: "m.presence",
sender: userA,
},
],
});
httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
const prom = client!.peekInRoom(roomOne); const prom = client!.peekInRoom(roomOne, limit);
await httpBackend!.flushAllExpected(); await httpBackend!.flushAllExpected();
const room = await prom; const room = await prom;
expect(room.roomId).toBe(roomOne); expect(room.roomId).toBe(roomOne);
expect(room.getMyMembership()).toBe(KnownMembership.Leave); expect(room.getMyMembership()).toBe(KnownMembership.Leave);
expect(room.name).toBe("Room Name"); expect(room.name).toBe("Room Name");
expect(room.currentState.getStateEvents("m.room.name", "")?.getId()).toBe("$eventId"); expect(room.currentState.getStateEvents("m.room.name", "")?.getId()).toBe("$eventId");
expect(room.timeline[0].getContent().body).toBe("Message 1"); expect(room.timeline[0].getContent().body).toBe("Message 1");
expect(room.timeline[1].getContent().body).toBe("Message 2"); expect(room.timeline[1].getContent().body).toBe("Message 2");
client?.stopPeeking(); client?.stopPeeking();
httpBackend!.when("GET", "/events").respond(200, { chunk: [] }); httpBackend!.when("GET", "/events").respond(200, { chunk: [] });
await httpBackend!.flushAllExpected(); await httpBackend!.flushAllExpected();
}); },
);
}); });
describe("user account data", () => { describe("user account data", () => {

View File

@@ -6603,13 +6603,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* Peek into a room and receive updates about the room. This only works if the * Peek into a room and receive updates about the room. This only works if the
* history visibility for the room is world_readable. * history visibility for the room is world_readable.
* @param roomId - The room to attempt to peek into. * @param roomId - The room to attempt to peek into.
* @param limit - The number of timeline events to initially retrieve.
* @returns Promise which resolves: Room object * @returns Promise which resolves: Room object
* @returns Rejects: with an error response. * @returns Rejects: with an error response.
*/ */
public peekInRoom(roomId: string): Promise<Room> { public peekInRoom(roomId: string, limit: number = 20): Promise<Room> {
this.peekSync?.stopPeeking(); this.peekSync?.stopPeeking();
this.peekSync = new SyncApi(this, this.clientOpts, this.buildSyncApiOptions()); this.peekSync = new SyncApi(this, this.clientOpts, this.buildSyncApiOptions());
return this.peekSync.peek(roomId); return this.peekSync.peek(roomId, limit);
} }
/** /**

View File

@@ -401,17 +401,18 @@ export class SyncApi {
* Peek into a room. This will result in the room in question being synced so it * Peek into a room. This will result in the room in question being synced so it
* is accessible via getRooms(). Live updates for the room will be provided. * is accessible via getRooms(). Live updates for the room will be provided.
* @param roomId - The room ID to peek into. * @param roomId - The room ID to peek into.
* @param limit - The number of timeline events to initially retrieve.
* @returns A promise which resolves once the room has been added to the * @returns A promise which resolves once the room has been added to the
* store. * store.
*/ */
public peek(roomId: string): Promise<Room> { public peek(roomId: string, limit: number = 20): Promise<Room> {
if (this._peekRoom?.roomId === roomId) { if (this._peekRoom?.roomId === roomId) {
return Promise.resolve(this._peekRoom); return Promise.resolve(this._peekRoom);
} }
const client = this.client; const client = this.client;
this._peekRoom = this.createRoom(roomId); this._peekRoom = this.createRoom(roomId);
return this.client.roomInitialSync(roomId, 20).then((response) => { return this.client.roomInitialSync(roomId, limit).then((response) => {
if (this._peekRoom?.roomId !== roomId) { if (this._peekRoom?.roomId !== roomId) {
throw new Error("Peeking aborted"); throw new Error("Peeking aborted");
} }