1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-23 17:02:25 +03:00

fix relation sender filter (#2196)

* fix relation sender filter

Signed-off-by: Kerry Archibald <kerrya@element.io>

* lint

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry
2022-02-24 09:44:18 +01:00
committed by GitHub
parent 2128f67dc3
commit 2ec5acb55d
3 changed files with 97 additions and 1 deletions

View File

@@ -85,6 +85,7 @@ export function mkEvent(opts) {
room_id: opts.room, room_id: opts.room,
sender: opts.sender || opts.user, // opts.user for backwards-compat sender: opts.sender || opts.user, // opts.user for backwards-compat
content: opts.content, content: opts.content,
unsigned: opts.unsigned,
event_id: "$" + Math.random() + "-" + Math.random(), event_id: "$" + Math.random() + "-" + Math.random(),
}; };
if (opts.skey !== undefined) { if (opts.skey !== undefined) {

View File

@@ -1,3 +1,4 @@
import { RelationType, UNSTABLE_FILTER_RELATION_SENDERS, UNSTABLE_FILTER_RELATION_TYPES } from "../../src";
import { FilterComponent } from "../../src/filter-component"; import { FilterComponent } from "../../src/filter-component";
import { mkEvent } from '../test-utils'; import { mkEvent } from '../test-utils';
@@ -30,5 +31,98 @@ describe("Filter Component", function() {
expect(checkResult).toBe(true); 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);
});
}); });
}); });

View File

@@ -80,9 +80,10 @@ export class FilterComponent {
// of performance // of performance
// This should be improved when bundled relationships solve that problem // This should be improved when bundled relationships solve that problem
const relationSenders = []; const relationSenders = [];
if (this.userId && relations?.[RelationType.Thread]?.current_user_participated) { if (this.userId && bundledRelationships?.[RelationType.Thread]?.current_user_participated) {
relationSenders.push(this.userId); relationSenders.push(this.userId);
} }
return this.checkFields( return this.checkFields(
event.getRoomId(), event.getRoomId(),
event.getSender(), event.getSender(),