Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix relation sender filter #2196

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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