diff --git a/spec/test-utils.js b/spec/test-utils.js index e5d5c490b..111c032a3 100644 --- a/spec/test-utils.js +++ b/spec/test-utils.js @@ -85,6 +85,7 @@ export function mkEvent(opts) { room_id: opts.room, sender: opts.sender || opts.user, // opts.user for backwards-compat content: opts.content, + unsigned: opts.unsigned, event_id: "$" + Math.random() + "-" + Math.random(), }; if (opts.skey !== undefined) { diff --git a/spec/unit/filter-component.spec.js b/spec/unit/filter-component.spec.js index 49f1d5614..d5d9fa287 100644 --- a/spec/unit/filter-component.spec.js +++ b/spec/unit/filter-component.spec.js @@ -1,3 +1,4 @@ +import { RelationType, UNSTABLE_FILTER_RELATION_SENDERS, UNSTABLE_FILTER_RELATION_TYPES } from "../../src"; import { FilterComponent } from "../../src/filter-component"; import { mkEvent } from '../test-utils'; @@ -30,5 +31,98 @@ describe("Filter Component", function() { expect(checkResult).toBe(true); }); + + it("should filter out events by relation participation", function() { + const currentUserId = '@me:server.org'; + const filter = new FilterComponent({ + [UNSTABLE_FILTER_RELATION_SENDERS.name]: currentUserId, + }, currentUserId); + + const threadRootNotParticipated = mkEvent({ + type: 'm.room.message', + content: {}, + room: 'roomId', + user: '@someone-else:server.org', + event: true, + unsigned: { + "m.relations": { + [RelationType.Thread]: { + count: 2, + current_user_participated: false, + }, + }, + }, + }); + + expect(filter.check(threadRootNotParticipated)).toBe(false); + }); + + it("should keep events by relation participation", function() { + const currentUserId = '@me:server.org'; + const filter = new FilterComponent({ + [UNSTABLE_FILTER_RELATION_SENDERS.name]: currentUserId, + }, currentUserId); + + const threadRootParticipated = mkEvent({ + type: 'm.room.message', + content: {}, + unsigned: { + "m.relations": { + [RelationType.Thread]: { + count: 2, + current_user_participated: true, + }, + }, + }, + user: '@someone-else:server.org', + room: 'roomId', + event: true, + }); + + expect(filter.check(threadRootParticipated)).toBe(true); + }); + + it("should filter out events by relation type", function() { + const filter = new FilterComponent({ + [UNSTABLE_FILTER_RELATION_TYPES.name]: RelationType.Thread, + }); + + const referenceRelationEvent = mkEvent({ + type: 'm.room.message', + content: {}, + room: 'roomId', + event: true, + unsigned: { + "m.relations": { + [RelationType.Reference]: {}, + }, + }, + }); + + expect(filter.check(referenceRelationEvent)).toBe(false); + }); + + it("should keep events by relation type", function() { + const filter = new FilterComponent({ + [UNSTABLE_FILTER_RELATION_TYPES.name]: RelationType.Thread, + }); + + const threadRootEvent = mkEvent({ + type: 'm.room.message', + content: {}, + unsigned: { + "m.relations": { + [RelationType.Thread]: { + count: 2, + current_user_participated: true, + }, + }, + }, + room: 'roomId', + event: true, + }); + + expect(filter.check(threadRootEvent)).toBe(true); + }); }); }); diff --git a/src/filter-component.ts b/src/filter-component.ts index 9ef535558..a69b9f454 100644 --- a/src/filter-component.ts +++ b/src/filter-component.ts @@ -80,9 +80,10 @@ export class FilterComponent { // of performance // This should be improved when bundled relationships solve that problem const relationSenders = []; - if (this.userId && relations?.[RelationType.Thread]?.current_user_participated) { + if (this.userId && bundledRelationships?.[RelationType.Thread]?.current_user_participated) { relationSenders.push(this.userId); } + return this.checkFields( event.getRoomId(), event.getSender(),