From 72695286d1563e6bfe27f11492a577f0a9d3654d Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 1 Mar 2023 10:49:21 +1300 Subject: [PATCH 1/2] add poll push rule ids --- src/@types/PushRules.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/@types/PushRules.ts b/src/@types/PushRules.ts index 56a93df9fb3..ab581f3680c 100644 --- a/src/@types/PushRules.ts +++ b/src/@types/PushRules.ts @@ -133,6 +133,14 @@ export enum RuleId { IncomingCall = ".m.rule.call", SuppressNotices = ".m.rule.suppress_notices", Tombstone = ".m.rule.tombstone", + PollStart = ".m.rule.poll_start", + PollStartUnstable = ".org.matrix.msc3930.rule.poll_start", + PollEnd = ".m.rule.poll_end", + PollEndUnstable = ".org.matrix.msc3930.rule.poll_end", + PollStartOneToOne = ".m.rule.poll_start_one_to_one", + PollStartOneToOneUnstable = ".org.matrix.msc3930.rule.poll_start_one_to_one", + PollEndOneToOne = ".m.rule.poll_end_one_to_one", + PollEndOneToOneUnstable = ".org.matrix.msc3930.rule.poll_end_one_to_one", } export type PushRuleSet = { From c3a78a9a38601a75ff1ca313c60a9bf3ba45ea90 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 1 Mar 2023 10:49:41 +1300 Subject: [PATCH 2/2] add getPushRuleAndKindById method to pushprocessor --- spec/unit/pushprocessor.spec.ts | 57 ++++++++++++++++++++++++--------- src/pushprocessor.ts | 14 +++++++- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/spec/unit/pushprocessor.spec.ts b/spec/unit/pushprocessor.spec.ts index dd84f03820a..5699afdc0af 100644 --- a/spec/unit/pushprocessor.spec.ts +++ b/spec/unit/pushprocessor.spec.ts @@ -11,6 +11,23 @@ describe("NotificationService", function () { let pushProcessor: PushProcessor; + const msc3914RoomCallRule = { + rule_id: ".org.matrix.msc3914.rule.room.call", + default: true, + enabled: true, + conditions: [ + { + kind: "event_match", + key: "type", + pattern: "org.matrix.msc3401.call", + }, + { + kind: "call_started", + }, + ], + actions: ["notify", { set_tweak: "sound", value: "default" }], + }; + // These would be better if individual rules were configured in the tests themselves. const matrixClient = { getRoom: function () { @@ -163,22 +180,7 @@ describe("NotificationService", function () { enabled: true, rule_id: ".m.rule.room_one_to_one", }, - { - rule_id: ".org.matrix.msc3914.rule.room.call", - default: true, - enabled: true, - conditions: [ - { - kind: "event_match", - key: "type", - pattern: "org.matrix.msc3401.call", - }, - { - kind: "call_started", - }, - ], - actions: ["notify", { set_tweak: "sound", value: "default" }], - }, + msc3914RoomCallRule, ], room: [], sender: [], @@ -493,4 +495,27 @@ describe("NotificationService", function () { }); }); }); + + describe("getPushRuleById()", () => { + it("returns null when rule id is not in rule set", () => { + expect(pushProcessor.getPushRuleById("non-existant-rule")).toBeNull(); + }); + + it("returns push rule when it is found in rule set", () => { + expect(pushProcessor.getPushRuleById(".org.matrix.msc3914.rule.room.call")).toEqual(msc3914RoomCallRule); + }); + }); + + describe("getPushRuleAndKindById()", () => { + it("returns null when rule id is not in rule set", () => { + expect(pushProcessor.getPushRuleAndKindById("non-existant-rule")).toBeNull(); + }); + + it("returns push rule when it is found in rule set", () => { + expect(pushProcessor.getPushRuleAndKindById(".org.matrix.msc3914.rule.room.call")).toEqual({ + kind: "override", + rule: msc3914RoomCallRule, + }); + }); + }); }); diff --git a/src/pushprocessor.ts b/src/pushprocessor.ts index 0d7338f8fb1..fa023466a9a 100644 --- a/src/pushprocessor.ts +++ b/src/pushprocessor.ts @@ -507,6 +507,18 @@ export class PushProcessor { * @returns The push rule, or null if no such rule was found */ public getPushRuleById(ruleId: string): IPushRule | null { + const result = this.getPushRuleAndKindById(ruleId); + return result?.rule ?? null; + } + + /** + * Get one of the users push rules by its ID + * + * @param ruleId - The ID of the rule to search for + * @returns rule The push rule, or null if no such rule was found + * @returns kind - The PushRuleKind of the rule to search for + */ + public getPushRuleAndKindById(ruleId: string): { rule: IPushRule; kind: PushRuleKind } | null { for (const scope of ["global"] as const) { if (this.client.pushRules?.[scope] === undefined) continue; @@ -514,7 +526,7 @@ export class PushProcessor { if (this.client.pushRules[scope][kind] === undefined) continue; for (const rule of this.client.pushRules[scope][kind]!) { - if (rule.rule_id === ruleId) return rule; + if (rule.rule_id === ruleId) return { rule, kind }; } } }