1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2024-03-28 12:55:44 +00:00
parent 1ed082f3d4
commit 32ddf2813d
2 changed files with 17 additions and 18 deletions

View File

@ -198,11 +198,12 @@ describe("NotificationService", function () {
}); });
it("should add default rules in the correct order", () => { it("should add default rules in the correct order", () => {
matrixClient.pushRules = PushProcessor.rewriteDefaultRules({ const pushRules = PushProcessor.rewriteDefaultRules({
device: {}, device: {},
global: { global: {
content: [], content: [],
override: [ 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", rule_id: ".m.rule.master",
default: true, 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. // 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. // 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, (rule) => rule.rule_id === RuleId.ContainsDisplayName,
); );
expect(containsDisplayNameRuleIdx).toBeGreaterThan(-1); expect(containsDisplayNameRuleIdx).toBeGreaterThan(-1);
const isRoomMentionRuleIdx = matrixClient.pushRules?.global.override?.findIndex( const isRoomMentionRuleIdx = pushRules.global.override?.findIndex(
(rule) => rule.rule_id === RuleId.IsRoomMention, (rule) => rule.rule_id === RuleId.IsRoomMention,
); );
expect(isRoomMentionRuleIdx).toBeGreaterThan(-1); expect(isRoomMentionRuleIdx).toBeGreaterThan(-1);
const mReactionRuleIdx = matrixClient.pushRules?.global.override?.findIndex( const mReactionRuleIdx = pushRules.global.override?.findIndex((rule) => rule.rule_id === ".m.rule.reaction");
(rule) => rule.rule_id === ".m.rule.reaction",
);
expect(mReactionRuleIdx).toBeGreaterThan(-1); expect(mReactionRuleIdx).toBeGreaterThan(-1);
expect(containsDisplayNameRuleIdx).toBeLessThan(isRoomMentionRuleIdx!); expect(containsDisplayNameRuleIdx).toBeLessThan(isRoomMentionRuleIdx!);
expect(isRoomMentionRuleIdx).toBeLessThan(mReactionRuleIdx!); 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", ".m.rule.master",
"coffee", "coffee",
".m.rule.contains_display_name", ".m.rule.contains_display_name",
@ -327,7 +325,7 @@ describe("NotificationService", function () {
".m.rule.reaction", ".m.rule.reaction",
".org.matrix.msc3786.rule.room.server_acl", ".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", "user-defined",
".org.matrix.msc3914.rule.room.call", ".org.matrix.msc3914.rule.room.call",
// Assert that unknown default rules are maintained // Assert that unknown default rules are maintained

View File

@ -49,10 +49,6 @@ const RULEKINDS_IN_ORDER = [
PushRuleKind.Underride, PushRuleKind.Underride,
]; ];
const UserDefinedRules = Symbol("UserDefinedRules");
type OrderedRules = Array<string | typeof UserDefinedRules>;
// The default override rules to apply to the push rules that arrive from the server. // The default override rules to apply to the push rules that arrive from the server.
// We do this for two reasons: // We do this for two reasons:
// 1. Synapse is unlikely to send us the push rule in an incremental sync - see // 1. Synapse is unlikely to send us the push rule in an incremental sync - see
@ -119,6 +115,11 @@ const DEFAULT_OVERRIDE_RULES: Record<string, IPushRule> = {
}, },
}; };
// A special rule id for `EXPECTED_DEFAULT_OVERRIDE_RULE_IDS` and friends.
const UserDefinedRules = Symbol("UserDefinedRules");
type OrderedRules = Array<string | typeof UserDefinedRules>;
const EXPECTED_DEFAULT_OVERRIDE_RULE_IDS: OrderedRules = [ const EXPECTED_DEFAULT_OVERRIDE_RULE_IDS: OrderedRules = [
RuleId.Master, RuleId.Master,
UserDefinedRules, UserDefinedRules,
@ -171,7 +172,7 @@ const EXPECTED_DEFAULT_UNDERRIDE_RULE_IDS: OrderedRules = [
* @param kind - the kind of push rule set being merged. * @param kind - the kind of push rule set being merged.
* @param incomingRules - the existing set of known push rules for the user. * @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 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. * @returns A copy of `incomingRules`, with any missing default rules inserted in the right place.
*/ */
@ -182,10 +183,10 @@ function mergeRulesWithDefaults(
defaultRuleIds: OrderedRules, defaultRuleIds: OrderedRules,
): IPushRule[] { ): IPushRule[] {
// Find the indices of the edges of the user-defined rules in the incoming rules // 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] = [ const userDefinedRulesRange: [number, number] = [
incomingRulesEnabled.indexOf(false), mappedIncomingRules.indexOf(false),
incomingRulesEnabled.lastIndexOf(false), mappedIncomingRules.lastIndexOf(false),
]; ];
function insertDefaultPushRule(ruleId: OrderedRules[number]): void { function insertDefaultPushRule(ruleId: OrderedRules[number]): void {
@ -202,7 +203,7 @@ function mergeRulesWithDefaults(
let nextExpectedRuleIdIndex = 0; let nextExpectedRuleIdIndex = 0;
const newRules: IPushRule[] = []; 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 [ for (const rule of [
...incomingRules.slice(0, userDefinedRulesRange[0]), ...incomingRules.slice(0, userDefinedRulesRange[0]),
...incomingRules.slice(userDefinedRulesRange[1]), ...incomingRules.slice(userDefinedRulesRange[1]),