diff --git a/spec/unit/pushprocessor.spec.ts b/spec/unit/pushprocessor.spec.ts index 136ecc56c..122084d92 100644 --- a/spec/unit/pushprocessor.spec.ts +++ b/spec/unit/pushprocessor.spec.ts @@ -198,11 +198,12 @@ describe("NotificationService", function () { }); it("should add default rules in the correct order", () => { - matrixClient.pushRules = PushProcessor.rewriteDefaultRules({ + const pushRules = PushProcessor.rewriteDefaultRules({ device: {}, global: { content: [], override: [ + // Include user-defined push rules inbetween .m.rule.master and other default rules to assert they are maintained in-order. { rule_id: ".m.rule.master", default: true, @@ -298,27 +299,24 @@ describe("NotificationService", function () { ], }, }); - pushProcessor = new PushProcessor(matrixClient); // By the time we get here, we expect the PushProcessor to have merged the new .m.rule.is_room_mention rule into the existing list of rules. // Check that has happened, and that it is in the right place. - const containsDisplayNameRuleIdx = matrixClient.pushRules?.global.override?.findIndex( + const containsDisplayNameRuleIdx = pushRules.global.override?.findIndex( (rule) => rule.rule_id === RuleId.ContainsDisplayName, ); expect(containsDisplayNameRuleIdx).toBeGreaterThan(-1); - const isRoomMentionRuleIdx = matrixClient.pushRules?.global.override?.findIndex( + const isRoomMentionRuleIdx = pushRules.global.override?.findIndex( (rule) => rule.rule_id === RuleId.IsRoomMention, ); expect(isRoomMentionRuleIdx).toBeGreaterThan(-1); - const mReactionRuleIdx = matrixClient.pushRules?.global.override?.findIndex( - (rule) => rule.rule_id === ".m.rule.reaction", - ); + const mReactionRuleIdx = pushRules.global.override?.findIndex((rule) => rule.rule_id === ".m.rule.reaction"); expect(mReactionRuleIdx).toBeGreaterThan(-1); expect(containsDisplayNameRuleIdx).toBeLessThan(isRoomMentionRuleIdx!); expect(isRoomMentionRuleIdx).toBeLessThan(mReactionRuleIdx!); - expect(matrixClient.pushRules?.global.override?.map((r) => r.rule_id)).toEqual([ + expect(pushRules.global.override?.map((r) => r.rule_id)).toEqual([ ".m.rule.master", "coffee", ".m.rule.contains_display_name", @@ -327,7 +325,7 @@ describe("NotificationService", function () { ".m.rule.reaction", ".org.matrix.msc3786.rule.room.server_acl", ]); - expect(matrixClient.pushRules?.global.underride?.map((r) => r.rule_id)).toEqual([ + expect(pushRules.global.underride?.map((r) => r.rule_id)).toEqual([ "user-defined", ".org.matrix.msc3914.rule.room.call", // Assert that unknown default rules are maintained diff --git a/src/pushprocessor.ts b/src/pushprocessor.ts index b13cfc0ca..c88ca40af 100644 --- a/src/pushprocessor.ts +++ b/src/pushprocessor.ts @@ -49,10 +49,6 @@ const RULEKINDS_IN_ORDER = [ PushRuleKind.Underride, ]; -const UserDefinedRules = Symbol("UserDefinedRules"); - -type OrderedRules = Array; - // The default override rules to apply to the push rules that arrive from the server. // We do this for two reasons: // 1. Synapse is unlikely to send us the push rule in an incremental sync - see @@ -119,6 +115,11 @@ const DEFAULT_OVERRIDE_RULES: Record = { }, }; +// A special rule id for `EXPECTED_DEFAULT_OVERRIDE_RULE_IDS` and friends. +const UserDefinedRules = Symbol("UserDefinedRules"); + +type OrderedRules = Array; + const EXPECTED_DEFAULT_OVERRIDE_RULE_IDS: OrderedRules = [ RuleId.Master, UserDefinedRules, @@ -171,7 +172,7 @@ const EXPECTED_DEFAULT_UNDERRIDE_RULE_IDS: OrderedRules = [ * @param kind - the kind of push rule set being merged. * @param incomingRules - the existing set of known push rules for the user. * @param defaultRules - a lookup table for the default definitions of push rules. - * @param defaultRuleIds - the IDs of the expected default push rules, in order. + * @param defaultRuleIds - the IDs of the expected push rules, in order. * * @returns A copy of `incomingRules`, with any missing default rules inserted in the right place. */ @@ -182,10 +183,10 @@ function mergeRulesWithDefaults( defaultRuleIds: OrderedRules, ): IPushRule[] { // Find the indices of the edges of the user-defined rules in the incoming rules - const incomingRulesEnabled = incomingRules.map((rule) => rule.enabled); + const mappedIncomingRules = incomingRules.map((rule) => rule.default); const userDefinedRulesRange: [number, number] = [ - incomingRulesEnabled.indexOf(false), - incomingRulesEnabled.lastIndexOf(false), + mappedIncomingRules.indexOf(false), + mappedIncomingRules.lastIndexOf(false), ]; function insertDefaultPushRule(ruleId: OrderedRules[number]): void { @@ -202,7 +203,7 @@ function mergeRulesWithDefaults( let nextExpectedRuleIdIndex = 0; const newRules: IPushRule[] = []; - // Process the default rules by merging them with defaults + // Merge our expected rules (including the incoming custom rules) into the incoming default rules. for (const rule of [ ...incomingRules.slice(0, userDefinedRulesRange[0]), ...incomingRules.slice(userDefinedRulesRange[1]),