Files
element-web/src/notifications/VectorPushRulesDefinitions.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

214 lines
9.7 KiB
TypeScript
Raw Normal View History

2018-04-11 23:58:37 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2016-2022 The Matrix.org Foundation C.I.C.
2018-04-11 23:58:37 +01:00
2024-09-09 14:57:16 +01: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-11 23:58:37 +01:00
*/
import { IAnnotatedPushRule, PushRuleAction, RuleId } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
import { _td, TranslationKey } from "../languageHandler";
2021-06-29 13:11:58 +01:00
import { StandardActions } from "./StandardActions";
2021-07-12 21:48:20 -06:00
import { PushRuleVectorState, VectorState } from "./PushRuleVectorState";
2021-06-29 13:11:58 +01:00
import { NotificationUtils } from "./NotificationUtils";
2021-10-15 16:30:53 +02:00
2021-07-12 21:48:20 -06:00
type StateToActionsMap = {
[state in VectorState]?: PushRuleAction[];
};
2018-04-11 23:58:37 +01:00
2022-03-09 12:05:16 +00:00
interface IVectorPushRuleDefinition {
description: TranslationKey;
2021-07-12 21:48:20 -06:00
vectorStateToActions: StateToActionsMap;
/**
* Rules that should be updated to be kept in sync
* when this rule changes
*/
syncedRuleIds?: (RuleId | string)[];
}
2018-04-11 23:58:37 +01:00
class VectorPushRuleDefinition {
public readonly description: TranslationKey;
2021-07-12 21:48:20 -06:00
public readonly vectorStateToActions: StateToActionsMap;
public readonly syncedRuleIds?: (RuleId | string)[];
public constructor(opts: IVectorPushRuleDefinition) {
2018-04-11 23:58:37 +01:00
this.description = opts.description;
this.vectorStateToActions = opts.vectorStateToActions;
this.syncedRuleIds = opts.syncedRuleIds;
2018-04-11 23:58:37 +01:00
}
// Translate the rule actions and its enabled value into vector state
public ruleToVectorState(rule: IAnnotatedPushRule): VectorState | undefined {
let enabled = false;
2018-04-11 23:58:37 +01:00
if (rule) {
enabled = rule.enabled;
}
2023-02-13 11:39:16 +00:00
for (const state of Object.values(PushRuleVectorState.states)) {
const vectorStateToActions = this.vectorStateToActions[state];
2018-04-11 23:58:37 +01:00
if (!vectorStateToActions) {
// No defined actions means that this vector state expects a disabled (or absent) rule
if (!enabled) {
return state;
}
} else {
2018-12-11 19:01:27 +00:00
// The actions must match to the ones expected by vector state.
// Use `decodeActions` on both sides to canonicalize things like
// value: true vs. unspecified for highlight (which defaults to
2018-12-12 01:46:48 +00:00
// true, making them equivalent).
2018-12-11 19:01:27 +00:00
if (
enabled &&
JSON.stringify(NotificationUtils.decodeActions(rule.actions)) ===
JSON.stringify(NotificationUtils.decodeActions(vectorStateToActions))
) {
2018-04-11 23:58:37 +01:00
return state;
}
}
}
2021-10-15 16:30:53 +02:00
logger.error(
`Cannot translate rule actions into Vector rule state. ` +
2018-12-11 19:01:27 +00:00
`Rule: ${JSON.stringify(rule)}, ` +
`Expected: ${JSON.stringify(this.vectorStateToActions)}`,
);
2018-04-11 23:58:37 +01:00
return undefined;
}
}
2022-03-09 12:05:16 +00:00
export type { VectorPushRuleDefinition };
2018-04-11 23:58:37 +01:00
/**
* The descriptions of rules managed by the Vector UI.
*/
2023-02-13 11:39:16 +00:00
export const VectorPushRulesDefinitions: Record<string, VectorPushRuleDefinition> = {
2018-04-11 23:58:37 +01:00
// Messages containing user's display name
".m.rule.contains_display_name": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_contains_display_name"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
// The actions for each vector state, or null to disable the rule.
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
2018-04-11 23:58:37 +01:00
}),
// Messages containing user's username (localpart/MXID)
".m.rule.contains_user_name": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_contains_user_name"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
// The actions for each vector state, or null to disable the rule.
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
syncedRuleIds: [RuleId.IsUserMention],
2018-04-11 23:58:37 +01:00
}),
2018-12-11 19:01:27 +00:00
// Messages containing @room
".m.rule.roomnotif": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_roomnotif"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-12-11 19:01:27 +00:00
vectorStateToActions: {
// The actions for each vector state, or null to disable the rule.
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
2018-12-11 19:01:27 +00:00
},
syncedRuleIds: [RuleId.IsRoomMention],
2018-12-11 19:01:27 +00:00
}),
2018-04-11 23:58:37 +01:00
// Messages just sent to the user in a 1:1 room
".m.rule.room_one_to_one": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_room_one_to_one"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DONT_NOTIFY,
},
syncedRuleIds: [
RuleId.PollStartOneToOne,
RuleId.PollStartOneToOneUnstable,
RuleId.PollEndOneToOne,
RuleId.PollEndOneToOneUnstable,
],
2018-04-11 23:58:37 +01:00
}),
2018-12-11 19:01:27 +00:00
// Encrypted messages just sent to the user in a 1:1 room
".m.rule.encrypted_room_one_to_one": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_encrypted_room_one_to_one"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-12-11 19:01:27 +00:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DONT_NOTIFY,
2018-12-11 19:01:27 +00:00
},
}),
2018-04-11 23:58:37 +01:00
// Messages just sent to a group chat room
// 1:1 room messages are caught by the .m.rule.room_one_to_one rule if any defined
2018-04-11 23:58:37 +01:00
// By opposition, all other room messages are from group chat rooms.
".m.rule.message": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_message"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DONT_NOTIFY,
},
syncedRuleIds: [RuleId.PollStart, RuleId.PollStartUnstable, RuleId.PollEnd, RuleId.PollEndUnstable],
2018-04-11 23:58:37 +01:00
}),
2018-12-11 19:01:27 +00:00
// Encrypted messages just sent to a group chat room
// Encrypted 1:1 room messages are caught by the .m.rule.encrypted_room_one_to_one rule if any defined
2018-12-11 19:01:27 +00:00
// By opposition, all other room messages are from group chat rooms.
".m.rule.encrypted": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_encrypted"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-12-11 19:01:27 +00:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DONT_NOTIFY,
2018-12-11 19:01:27 +00:00
},
}),
2018-04-11 23:58:37 +01:00
// Invitation for the user
".m.rule.invite_for_me": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_invite_for_me"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
2018-04-11 23:58:37 +01:00
}),
// Incoming call
".m.rule.call": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_call"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_RING_SOUND,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
2018-04-11 23:58:37 +01:00
}),
// Notifications from bots
".m.rule.suppress_notices": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_suppress_notices"), // passed through _t() translation in src/components/views/settings/Notifications.js
2018-04-11 23:58:37 +01:00
vectorStateToActions: {
// .m.rule.suppress_notices is a "negative" rule, we have to invert its enabled value for vector UI
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_DISABLED,
[VectorState.Loud]: StandardActions.ACTION_NOTIFY_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DONT_NOTIFY,
},
2018-04-11 23:58:37 +01:00
}),
// Room upgrades (tombstones)
".m.rule.tombstone": new VectorPushRuleDefinition({
description: _td("settings|notifications|rule_tombstone"), // passed through _t() translation in src/components/views/settings/Notifications.js
vectorStateToActions: {
// The actions for each vector state, or null to disable the rule.
2021-07-12 21:48:20 -06:00
[VectorState.On]: StandardActions.ACTION_NOTIFY,
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
}),
2018-04-11 23:58:37 +01:00
};