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".
This commit is contained in:
gnuxie 2021-10-13 11:54:27 +01:00
parent 4e8315be44
commit 743f6d043a

View File

@ -76,42 +76,12 @@ export async function redactUserMessagesIn(client: MatrixClient, userIdOrGlob: s
* @returns {Promise<any>} Resolves when complete.
*/
export async function getMessagesByUserIn(client: MatrixClient, sender: string, roomId: string, limit: number, cb: (events: any[]) => void): Promise<any> {
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",
};