From 73cbcfa4eebdd18e8166154b5d370f2d23937d7d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 9 Jun 2025 09:32:42 +0100 Subject: [PATCH] Ensure we send spec-compliant filter strings by stripping out null values (#4865) * Ensure we send spec-compliant filter strings by stripping out null values Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Add test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/unit/filter-component.spec.ts | 7 +++++++ src/filter-component.ts | 24 +++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/spec/unit/filter-component.spec.ts b/spec/unit/filter-component.spec.ts index ad7cecde2..d74bac8c8 100644 --- a/spec/unit/filter-component.spec.ts +++ b/spec/unit/filter-component.spec.ts @@ -170,4 +170,11 @@ describe("Filter Component", function () { expect(filter.check(noMatchEvent)).toBe(false); }); }); + + describe("toJSON", () => { + it("should omit empty values", () => { + const filter = new FilterComponent({ types: ["m.room.message"], senders: ["@alice:example.com"] }); + expect(filter.toJSON()).toEqual({ types: ["m.room.message"], senders: ["@alice:example.com"] }); + }); + }); }); diff --git a/src/filter-component.ts b/src/filter-component.ts index 557fa80df..c27cc0b29 100644 --- a/src/filter-component.ts +++ b/src/filter-component.ts @@ -99,17 +99,19 @@ export class FilterComponent { * Converts the filter component into the form expected over the wire */ public toJSON(): object { - return { - types: this.filterJson.types || null, - not_types: this.filterJson.not_types || [], - rooms: this.filterJson.rooms || null, - not_rooms: this.filterJson.not_rooms || [], - senders: this.filterJson.senders || null, - not_senders: this.filterJson.not_senders || [], - contains_url: this.filterJson.contains_url || null, - [FILTER_RELATED_BY_SENDERS.name]: this.filterJson[FILTER_RELATED_BY_SENDERS.name] || [], - [FILTER_RELATED_BY_REL_TYPES.name]: this.filterJson[FILTER_RELATED_BY_REL_TYPES.name] || [], - }; + return Object.fromEntries( + Object.entries({ + types: this.filterJson.types, + not_types: this.filterJson.not_types, + rooms: this.filterJson.rooms, + not_rooms: this.filterJson.not_rooms, + senders: this.filterJson.senders, + not_senders: this.filterJson.not_senders, + contains_url: this.filterJson.contains_url, + [FILTER_RELATED_BY_SENDERS.name]: this.filterJson[FILTER_RELATED_BY_SENDERS.name], + [FILTER_RELATED_BY_REL_TYPES.name]: this.filterJson[FILTER_RELATED_BY_REL_TYPES.name], + }).filter(([_key, value]) => value), + ); } /**