From 743f6d043a8ed73412a5b7dcd1dd87da2e47f332 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Wed, 13 Oct 2021 11:54:27 +0100 Subject: [PATCH] Fix filter when paginating history in getMessagesByUser. Related to https://github.com/matrix-org/mjolnir/pull/132. The old code would call `/sync` with this filter. If a token was provided in the response of `/sync` for earlier messages, it would then use this same filter to call `/rooms/messages`. However, this filter does not do anything on that endpoint when we know the id of the sender, as it requires a RoomEventFilter and there is no warning or error from synapse about the structure of the filter being wrong. This was not noticed until after the related PR because `/sync` with the filter would usually be able to provide a user's entire history in one room. This is because in most cases a user is banned/redacted shortly after joining a room. In the case that `/rooms/messages` was called for more events, the method would always paginate the timeline up until the limit or the end of the room history, which is only the expected behavior when matching the sender with a "glob". --- src/utils.ts | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 8db75b3..9713332 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -76,42 +76,12 @@ export async function redactUserMessagesIn(client: MatrixClient, userIdOrGlob: s * @returns {Promise} Resolves when complete. */ export async function getMessagesByUserIn(client: MatrixClient, sender: string, roomId: string, limit: number, cb: (events: any[]) => void): Promise { - const filter = { - room: { - rooms: [roomId], - state: { - // types: ["m.room.member"], // We'll redact all types of events - rooms: [roomId], - }, - timeline: { - rooms: [roomId], - // types: ["m.room.message"], // We'll redact all types of events - }, - ephemeral: { - limit: 0, - types: [], - }, - account_data: { - limit: 0, - types: [], - }, - }, - presence: { - limit: 0, - types: [], - }, - account_data: { - limit: 0, - types: [], - }, + const isGlob = sender.includes("*"); + const roomEventFilter = { + rooms: [roomId], + ... isGlob ? {} : {senders: [sender]} }; - let isGlob = true; - if (!sender.includes("*")) { - isGlob = false; - filter.room.timeline['senders'] = [sender]; - } - const matcher = new MatrixGlob(sender); function testUser(userId: string): boolean { @@ -134,7 +104,7 @@ export async function getMessagesByUserIn(client: MatrixClient, sender: string, function backfill(from: string) { const qs = { - filter: JSON.stringify(filter), + filter: JSON.stringify(roomEventFilter), from: from, dir: "b", };