1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-06 12:02:40 +03:00

Polls push rules (#3181)

* add poll push rule ids

* add getPushRuleAndKindById method to pushprocessor
This commit is contained in:
Kerry
2023-03-02 09:30:40 +13:00
committed by GitHub
parent c8a4d9b88a
commit 933a0c9909
3 changed files with 62 additions and 17 deletions

View File

@@ -11,6 +11,23 @@ describe("NotificationService", function () {
let pushProcessor: PushProcessor; 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. // These would be better if individual rules were configured in the tests themselves.
const matrixClient = { const matrixClient = {
getRoom: function () { getRoom: function () {
@@ -163,22 +180,7 @@ describe("NotificationService", function () {
enabled: true, enabled: true,
rule_id: ".m.rule.room_one_to_one", rule_id: ".m.rule.room_one_to_one",
}, },
{ 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" }],
},
], ],
room: [], room: [],
sender: [], sender: [],
@@ -549,6 +551,29 @@ describe("NotificationService", function () {
), ),
).toBe(expected); ).toBe(expected);
}); });
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,
});
});
});
}); });
describe("Test PushProcessor.partsForDottedKey", function () { describe("Test PushProcessor.partsForDottedKey", function () {

View File

@@ -133,6 +133,14 @@ export enum RuleId {
IncomingCall = ".m.rule.call", IncomingCall = ".m.rule.call",
SuppressNotices = ".m.rule.suppress_notices", SuppressNotices = ".m.rule.suppress_notices",
Tombstone = ".m.rule.tombstone", 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 = { export type PushRuleSet = {

View File

@@ -634,6 +634,18 @@ export class PushProcessor {
* @returns The push rule, or null if no such rule was found * @returns The push rule, or null if no such rule was found
*/ */
public getPushRuleById(ruleId: string): IPushRule | null { 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) { for (const scope of ["global"] as const) {
if (this.client.pushRules?.[scope] === undefined) continue; if (this.client.pushRules?.[scope] === undefined) continue;
@@ -641,7 +653,7 @@ export class PushProcessor {
if (this.client.pushRules[scope][kind] === undefined) continue; if (this.client.pushRules[scope][kind] === undefined) continue;
for (const rule of this.client.pushRules[scope][kind]!) { for (const rule of this.client.pushRules[scope][kind]!) {
if (rule.rule_id === ruleId) return rule; if (rule.rule_id === ruleId) return { rule, kind };
} }
} }
} }