@@ -17,71 +17,111 @@ limitations under the License.
1717'use strict' ;
1818
1919var StandardActions = require ( './StandardActions' ) ;
20+ var PushRuleVectorState = require ( './PushRuleVectorState' ) ;
21+
22+ class VectorPushRuleDefinition {
23+ constructor ( opts ) {
24+ this . kind = opts . kind ;
25+ this . description = opts . description ;
26+ this . vectorStateToActions = opts . vectorStateToActions ;
27+ }
28+
29+ // Translate the rule actions and its enabled value into vector state
30+ ruleToVectorState ( rule ) {
31+ var enabled = false ;
32+ var actions = null ;
33+ if ( rule ) {
34+ enabled = rule . enabled ;
35+ actions = rule . actions ;
36+ }
37+
38+ for ( var stateKey in PushRuleVectorState . states ) {
39+ var state = PushRuleVectorState . states [ stateKey ] ;
40+ var vectorStateToActions = this . vectorStateToActions [ state ] ;
41+
42+ if ( ! vectorStateToActions ) {
43+ // No defined actions means that this vector state expects a disabled (or absent) rule
44+ if ( ! enabled ) {
45+ return state ;
46+ }
47+ } else {
48+ // The actions must match to the ones expected by vector state
49+ if ( enabled && JSON . stringify ( rule . actions ) === JSON . stringify ( vectorStateToActions ) ) {
50+ return state ;
51+ }
52+ }
53+ }
54+
55+ console . error ( "Cannot translate rule actions into Vector rule state. Rule: " +
56+ JSON . stringify ( rule ) ) ;
57+ return undefined ;
58+ }
59+ } ;
2060
2161/**
2262 * The descriptions of rules managed by the Vector UI.
2363 */
2464module . exports = {
2565 // Messages containing user's display name
2666 // (skip contains_user_name which is too geeky)
27- ".m.rule.contains_display_name" : {
67+ ".m.rule.contains_display_name" : new VectorPushRuleDefinition ( {
2868 kind : "underride" ,
2969 description : "Messages containing my name" ,
3070 vectorStateToActions : { // The actions for each vector state, or null to disable the rule.
3171 on : StandardActions . ACTION_NOTIFY ,
3272 loud : StandardActions . ACTION_HIGHLIGHT_DEFAULT_SOUND ,
3373 off : StandardActions . ACTION_DISABLED
3474 }
35- } ,
75+ } ) ,
3676
3777 // Messages just sent to the user in a 1:1 room
38- ".m.rule.room_one_to_one" : {
78+ ".m.rule.room_one_to_one" : new VectorPushRuleDefinition ( {
3979 kind : "underride" ,
4080 description : "Messages in one-to-one chats" ,
4181 vectorStateToActions : {
4282 on : StandardActions . ACTION_NOTIFY ,
4383 loud : StandardActions . ACTION_NOTIFY_DEFAULT_SOUND ,
4484 off : StandardActions . ACTION_DONT_NOTIFY
4585 }
46- } ,
86+ } ) ,
4787
4888 // Messages just sent to a group chat room
4989 // 1:1 room messages are catched by the .m.rule.room_one_to_one rule if any defined
5090 // By opposition, all other room messages are from group chat rooms.
51- ".m.rule.message" : {
91+ ".m.rule.message" : new VectorPushRuleDefinition ( {
5292 kind : "underride" ,
5393 description : "Messages in group chats" ,
5494 vectorStateToActions : {
5595 on : StandardActions . ACTION_NOTIFY ,
5696 loud : StandardActions . ACTION_NOTIFY_DEFAULT_SOUND ,
5797 off : StandardActions . ACTION_DONT_NOTIFY
5898 }
59- } ,
99+ } ) ,
60100
61101 // Invitation for the user
62- ".m.rule.invite_for_me" : {
102+ ".m.rule.invite_for_me" : new VectorPushRuleDefinition ( {
63103 kind : "underride" ,
64104 description : "When I'm invited to a room" ,
65105 vectorStateToActions : {
66106 on : StandardActions . ACTION_NOTIFY ,
67107 loud : StandardActions . ACTION_NOTIFY_DEFAULT_SOUND ,
68108 off : StandardActions . ACTION_DISABLED
69109 }
70- } ,
110+ } ) ,
71111
72112 // Incoming call
73- ".m.rule.call" : {
113+ ".m.rule.call" : new VectorPushRuleDefinition ( {
74114 kind : "underride" ,
75115 description : "Call invitation" ,
76116 vectorStateToActions : {
77117 on : StandardActions . ACTION_NOTIFY ,
78118 loud : StandardActions . ACTION_NOTIFY_RING_SOUND ,
79119 off : StandardActions . ACTION_DISABLED
80120 }
81- } ,
121+ } ) ,
82122
83123 // Notifications from bots
84- ".m.rule.suppress_notices" : {
124+ ".m.rule.suppress_notices" : new VectorPushRuleDefinition ( {
85125 kind : "override" ,
86126 description : "Messages sent by bot" ,
87127 vectorStateToActions : {
@@ -90,5 +130,5 @@ module.exports = {
90130 loud : StandardActions . ACTION_NOTIFY_DEFAULT_SOUND ,
91131 off : StandardActions . ACTION_DONT_NOTIFY ,
92132 }
93- }
133+ } ) ,
94134} ;
0 commit comments