Skip to content

Commit

Permalink
fix relation sender filter (#2196)
Browse files Browse the repository at this point in the history
* fix relation sender filter

Signed-off-by: Kerry Archibald <[email protected]>

* lint

Signed-off-by: Kerry Archibald <[email protected]>
  • Loading branch information
Kerry authored Feb 24, 2022
1 parent 2128f67 commit 2ec5acb
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions spec/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
94 changes: 94 additions & 0 deletions spec/unit/filter-component.spec.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
});
3 changes: 2 additions & 1 deletion src/filter-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 2ec5acb

Please sign in to comment.