Skip to content

Commit 73cbcfa

Browse files
authored
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>
1 parent 99972ce commit 73cbcfa

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

spec/unit/filter-component.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ describe("Filter Component", function () {
170170
expect(filter.check(noMatchEvent)).toBe(false);
171171
});
172172
});
173+
174+
describe("toJSON", () => {
175+
it("should omit empty values", () => {
176+
const filter = new FilterComponent({ types: ["m.room.message"], senders: ["@alice:example.com"] });
177+
expect(filter.toJSON()).toEqual({ types: ["m.room.message"], senders: ["@alice:example.com"] });
178+
});
179+
});
173180
});

src/filter-component.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ export class FilterComponent {
9999
* Converts the filter component into the form expected over the wire
100100
*/
101101
public toJSON(): object {
102-
return {
103-
types: this.filterJson.types || null,
104-
not_types: this.filterJson.not_types || [],
105-
rooms: this.filterJson.rooms || null,
106-
not_rooms: this.filterJson.not_rooms || [],
107-
senders: this.filterJson.senders || null,
108-
not_senders: this.filterJson.not_senders || [],
109-
contains_url: this.filterJson.contains_url || null,
110-
[FILTER_RELATED_BY_SENDERS.name]: this.filterJson[FILTER_RELATED_BY_SENDERS.name] || [],
111-
[FILTER_RELATED_BY_REL_TYPES.name]: this.filterJson[FILTER_RELATED_BY_REL_TYPES.name] || [],
112-
};
102+
return Object.fromEntries(
103+
Object.entries({
104+
types: this.filterJson.types,
105+
not_types: this.filterJson.not_types,
106+
rooms: this.filterJson.rooms,
107+
not_rooms: this.filterJson.not_rooms,
108+
senders: this.filterJson.senders,
109+
not_senders: this.filterJson.not_senders,
110+
contains_url: this.filterJson.contains_url,
111+
[FILTER_RELATED_BY_SENDERS.name]: this.filterJson[FILTER_RELATED_BY_SENDERS.name],
112+
[FILTER_RELATED_BY_REL_TYPES.name]: this.filterJson[FILTER_RELATED_BY_REL_TYPES.name],
113+
}).filter(([_key, value]) => value),
114+
);
113115
}
114116

115117
/**

0 commit comments

Comments
 (0)