2018-04-12 22:25:08 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-01 09:45:50 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2018-04-12 22:25:08 +03:00
|
|
|
*/
|
|
|
|
|
2022-04-01 09:45:50 +03:00
|
|
|
import { TweakName, PushRuleActionName, TweakHighlight, TweakSound } from "matrix-js-sdk/src/matrix";
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import { ContentRules, PushRuleVectorState } from "../../../src/notifications";
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const NORMAL_RULE = {
|
2022-12-12 14:24:14 +03:00
|
|
|
actions: [PushRuleActionName.Notify, { set_tweak: TweakName.Highlight, value: false } as TweakHighlight],
|
2022-04-01 09:45:50 +03:00
|
|
|
default: false,
|
2018-04-12 22:25:08 +03:00
|
|
|
enabled: true,
|
|
|
|
pattern: "vdh2",
|
|
|
|
rule_id: "vdh2",
|
|
|
|
};
|
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const LOUD_RULE = {
|
2018-04-12 22:25:08 +03:00
|
|
|
actions: [
|
2022-04-01 09:45:50 +03:00
|
|
|
PushRuleActionName.Notify,
|
|
|
|
{ set_tweak: TweakName.Highlight } as TweakHighlight,
|
|
|
|
{ set_tweak: TweakName.Sound, value: "default" } as TweakSound,
|
2018-04-12 22:25:08 +03:00
|
|
|
],
|
2022-04-01 09:45:50 +03:00
|
|
|
default: false,
|
2018-04-12 22:25:08 +03:00
|
|
|
enabled: true,
|
|
|
|
pattern: "vdh2",
|
|
|
|
rule_id: "vdh2",
|
|
|
|
};
|
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const USERNAME_RULE = {
|
2018-04-12 22:25:08 +03:00
|
|
|
actions: [
|
2022-04-01 09:45:50 +03:00
|
|
|
PushRuleActionName.Notify,
|
|
|
|
{ set_tweak: TweakName.Sound, value: "default" } as TweakSound,
|
|
|
|
{ set_tweak: TweakName.Highlight } as TweakHighlight,
|
2018-04-12 22:25:08 +03:00
|
|
|
],
|
|
|
|
default: true,
|
|
|
|
enabled: true,
|
|
|
|
pattern: "richvdh",
|
|
|
|
rule_id: ".m.rule.contains_user_name",
|
|
|
|
};
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("ContentRules", function () {
|
|
|
|
describe("parseContentRules", function () {
|
|
|
|
it("should handle there being no keyword rules", function () {
|
|
|
|
const rules = { global: { content: [USERNAME_RULE] } };
|
2020-10-30 21:33:33 +03:00
|
|
|
const parsed = ContentRules.parseContentRules(rules);
|
2018-04-12 22:25:08 +03:00
|
|
|
expect(parsed.rules).toEqual([]);
|
|
|
|
expect(parsed.vectorState).toEqual(PushRuleVectorState.ON);
|
|
|
|
expect(parsed.externalRules).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should parse regular keyword notifications", function () {
|
|
|
|
const rules = { global: { content: [NORMAL_RULE, USERNAME_RULE] } };
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const parsed = ContentRules.parseContentRules(rules);
|
2018-04-12 22:25:08 +03:00
|
|
|
expect(parsed.rules.length).toEqual(1);
|
|
|
|
expect(parsed.rules[0]).toEqual(NORMAL_RULE);
|
|
|
|
expect(parsed.vectorState).toEqual(PushRuleVectorState.ON);
|
|
|
|
expect(parsed.externalRules).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should parse loud keyword notifications", function () {
|
|
|
|
const rules = { global: { content: [LOUD_RULE, USERNAME_RULE] } };
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const parsed = ContentRules.parseContentRules(rules);
|
2018-04-12 22:25:08 +03:00
|
|
|
expect(parsed.rules.length).toEqual(1);
|
|
|
|
expect(parsed.rules[0]).toEqual(LOUD_RULE);
|
|
|
|
expect(parsed.vectorState).toEqual(PushRuleVectorState.LOUD);
|
|
|
|
expect(parsed.externalRules).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should parse mixed keyword notifications", function () {
|
|
|
|
const rules = { global: { content: [LOUD_RULE, NORMAL_RULE, USERNAME_RULE] } };
|
2018-04-12 22:25:08 +03:00
|
|
|
|
2020-10-30 21:33:33 +03:00
|
|
|
const parsed = ContentRules.parseContentRules(rules);
|
2018-04-12 22:25:08 +03:00
|
|
|
expect(parsed.rules.length).toEqual(1);
|
|
|
|
expect(parsed.rules[0]).toEqual(LOUD_RULE);
|
|
|
|
expect(parsed.vectorState).toEqual(PushRuleVectorState.LOUD);
|
|
|
|
expect(parsed.externalRules.length).toEqual(1);
|
|
|
|
expect(parsed.externalRules[0]).toEqual(NORMAL_RULE);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|