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
*/
2023-08-09 16:10:54 +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" ;
2023-08-22 16:32:05 +01:00
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 {
2023-08-22 16:32:05 +01:00
description : TranslationKey ;
2021-07-12 21:48:20 -06:00
vectorStateToActions : StateToActionsMap ;
2023-03-02 18:33:19 +13:00
/**
* Rules that should be updated to be kept in sync
* when this rule changes
*/
syncedRuleIds ? : ( RuleId | string ) [ ] ;
2021-07-03 12:29:13 +01:00
}
2018-04-11 23:58:37 +01:00
class VectorPushRuleDefinition {
2023-08-22 16:32:05 +01:00
public readonly description : TranslationKey ;
2021-07-12 21:48:20 -06:00
public readonly vectorStateToActions : StateToActionsMap ;
2023-03-02 18:33:19 +13:00
public readonly syncedRuleIds ? : ( RuleId | string ) [ ] ;
2021-07-03 12:29:13 +01:00
2022-12-16 12:29:59 +00:00
public constructor ( opts : IVectorPushRuleDefinition ) {
2018-04-11 23:58:37 +01:00
this . description = opts . description ;
this . vectorStateToActions = opts . vectorStateToActions ;
2023-03-02 18:33:19 +13:00
this . syncedRuleIds = opts . syncedRuleIds ;
2018-04-11 23:58:37 +01:00
}
// Translate the rule actions and its enabled value into vector state
2023-02-15 13:36:22 +00:00
public ruleToVectorState ( rule : IAnnotatedPushRule ) : VectorState | undefined {
2018-10-11 21:59:17 -05:00
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 ) ) {
2018-10-11 21:59:17 -05:00
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 &&
2019-12-19 17:45:24 -07:00
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 ;
}
2018-10-11 21:59:17 -05:00
}
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 ( {
2023-09-04 19:41:39 +01:00
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-10-11 21:59:17 -05:00
} ,
2018-04-11 23:58:37 +01:00
} ) ,
// Messages containing user's username (localpart/MXID)
".m.rule.contains_user_name" : new VectorPushRuleDefinition ( {
2023-09-04 19:41:39 +01:00
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 ,
2018-10-11 21:59:17 -05:00
} ,
2023-09-27 13:03:38 +13:00
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 ( {
2023-09-04 19:41:39 +01:00
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
} ,
2023-09-27 13:03:38 +13: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 ( {
2023-09-04 19:41:39 +01:00
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 ,
2018-10-11 21:59:17 -05:00
} ,
2023-03-02 18:33:19 +13:00
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 ( {
2023-09-04 19:41:39 +01:00
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
2022-05-09 16:52:05 -06:00
// 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 ( {
2023-09-04 19:41:39 +01:00
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 ,
2018-10-11 21:59:17 -05:00
} ,
2023-03-02 18:33:19 +13:00
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
2022-05-09 16:52:05 -06:00
// 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 ( {
2023-09-04 19:41:39 +01:00
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 ( {
2023-09-04 19:41:39 +01:00
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-10-11 21:59:17 -05:00
} ,
2018-04-11 23:58:37 +01:00
} ) ,
// Incoming call
".m.rule.call" : new VectorPushRuleDefinition ( {
2023-09-04 19:41:39 +01:00
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-10-11 21:59:17 -05:00
} ,
2018-04-11 23:58:37 +01:00
} ) ,
// Notifications from bots
".m.rule.suppress_notices" : new VectorPushRuleDefinition ( {
2023-09-04 19:41:39 +01:00
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-10-11 21:59:17 -05:00
} ,
2018-04-11 23:58:37 +01:00
} ) ,
2019-03-15 14:13:15 -06:00
// Room upgrades (tombstones)
".m.rule.tombstone" : new VectorPushRuleDefinition ( {
2023-09-04 19:41:39 +01:00
description : _td ( "settings|notifications|rule_tombstone" ) , // passed through _t() translation in src/components/views/settings/Notifications.js
2019-03-15 14:13:15 -06: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 ,
2019-03-15 14:13:15 -06:00
} ,
} ) ,
2018-04-11 23:58:37 +01:00
} ;