@@ -79,87 +79,16 @@ module.exports = React.createClass({
7979 } ,
8080
8181 onNotifStateButtonClicked : function ( event ) {
82- var self = this ;
83- var cli = MatrixClientPeg . get ( ) ;
8482 var vectorRuleId = event . target . className . split ( "-" ) [ 0 ] ;
8583 var newPushRuleVectorState = event . target . className . split ( "-" ) [ 1 ] ;
8684
87- if ( "keywords" === vectorRuleId
88- && this . state . vectorContentRules . vectorState !== newPushRuleVectorState
89- && this . state . vectorContentRules . rules . length ) {
90-
91- this . setState ( {
92- phase : this . phases . LOADING
93- } ) ;
94-
95- // Update all rules in self.state.vectorContentRules
96- var deferreds = [ ] ;
97- for ( var i in this . state . vectorContentRules . rules ) {
98- var rule = this . state . vectorContentRules . rules [ i ] ;
99-
100- var enabled ;
101- var actions ;
102- switch ( newPushRuleVectorState ) {
103- case PushRuleVectorState . ON :
104- if ( rule . actions . length !== 1 ) {
105- actions = this . _actionsFor ( PushRuleVectorState . ON ) ;
106- }
107-
108- if ( this . state . vectorContentRules . vectorState === PushRuleVectorState . OFF ) {
109- enabled = true ;
110- }
111- break ;
112-
113- case PushRuleVectorState . LOUD :
114- if ( rule . actions . length !== 3 ) {
115- actions = this . _actionsFor ( PushRuleVectorState . LOUD ) ;
116- }
117-
118- if ( this . state . vectorContentRules . vectorState === PushRuleVectorState . OFF ) {
119- enabled = true ;
120- }
121- break ;
122-
123- case PushRuleVectorState . OFF :
124- enabled = false ;
125- break ;
126- }
127-
128- if ( actions ) {
129- // Note that the workaround in _updatePushRuleActions will automatically
130- // enable the rule
131- deferreds . push ( this . _updatePushRuleActions ( rule , actions , enabled ) ) ;
132- }
133- else if ( enabled != undefined ) {
134- deferreds . push ( cli . setPushRuleEnabled ( 'global' , rule . kind , rule . rule_id , enabled ) ) ;
135- }
136- }
137-
138- q . all ( deferreds ) . done ( function ( resps ) {
139- self . _refreshFromServer ( ) ;
140- } , function ( error ) {
141- var ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
142- Modal . createDialog ( ErrorDialog , {
143- title : "Can't update user notification settings" ,
144- description : error . toString ( ) ,
145- onFinished : self . _refreshFromServer
146- } ) ;
147- } ) ;
85+ if ( "keywords" === vectorRuleId ) {
86+ this . _changeKeywordsPushRuleVectorState ( newPushRuleVectorState )
14887 }
14988 else {
15089 var rule = this . getRule ( vectorRuleId ) ;
151-
152- // For now, we support only enabled/disabled for hs default rules
153- // Translate ON, LOUD, OFF to one of the 2.
154- if ( rule && rule . vectorState !== newPushRuleVectorState ) {
155-
156- this . setState ( {
157- phase : this . phases . LOADING
158- } ) ;
159-
160- cli . setPushRuleEnabled ( 'global' , rule . rule . kind , rule . rule . rule_id , ( newPushRuleVectorState !== PushRuleVectorState . OFF ) ) . done ( function ( ) {
161- self . _refreshFromServer ( ) ;
162- } ) ;
90+ if ( rule ) {
91+ this . _changePushRuleVectorState ( rule , newPushRuleVectorState ) ;
16392 }
16493 }
16594 } ,
@@ -192,9 +121,6 @@ module.exports = React.createClass({
192121 onFinished : function onFinished ( should_leave , newValue ) {
193122
194123 if ( should_leave && newValue !== keywords ) {
195- var cli = MatrixClientPeg . get ( ) ;
196- var removeDeferreds = [ ] ;
197-
198124 var newKeywords = newValue . split ( ',' ) ;
199125 for ( var i in newKeywords ) {
200126 newKeywords [ i ] = newKeywords [ i ] . trim ( ) ;
@@ -207,75 +133,8 @@ module.exports = React.createClass({
207133 }
208134 return array ;
209135 } , [ ] ) ;
210-
211- self . setState ( {
212- phase : self . phases . LOADING
213- } ) ;
214-
215- // Remove per-word push rules of keywords that are no more in the list
216- var vectorContentRulesPatterns = [ ] ;
217- for ( var i in self . state . vectorContentRules . rules ) {
218- var rule = self . state . vectorContentRules . rules [ i ] ;
219-
220- vectorContentRulesPatterns . push ( rule . pattern ) ;
221-
222- if ( newKeywords . indexOf ( rule . pattern ) < 0 ) {
223- removeDeferreds . push ( cli . deletePushRule ( 'global' , rule . kind , rule . rule_id ) ) ;
224- }
225- }
226-
227- // If the keyword is part of `externalContentRules`, remove the rule
228- // before recreating it in the right Vector path
229- for ( var i in self . state . externalContentRules ) {
230- var rule = self . state . externalContentRules [ i ] ;
231-
232- if ( newKeywords . indexOf ( rule . pattern ) >= 0 ) {
233- removeDeferreds . push ( cli . deletePushRule ( 'global' , rule . kind , rule . rule_id ) ) ;
234- }
235- }
236-
237- var onError = function ( error ) {
238- var ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
239- Modal . createDialog ( ErrorDialog , {
240- title : "Can't update keywords" ,
241- description : error . toString ( ) ,
242- onFinished : self . _refreshFromServer
243- } ) ;
244- }
245-
246- // Then, add the new ones
247- q . all ( removeDeferreds ) . done ( function ( resps ) {
248- var deferreds = [ ] ;
249- for ( var i in newKeywords ) {
250- var keyword = newKeywords [ i ] ;
251-
252- var pushRuleVectorStateKind = self . state . vectorContentRules . vectorState ;
253- if ( pushRuleVectorStateKind === PushRuleVectorState . OFF ) {
254- // When the current global keywords rule is OFF, we need to look at
255- // the flavor of rules in 'vectorContentRules' to apply the same actions
256- // when creating the new rule.
257- // Thus, this new rule will join the 'vectorContentRules' set.
258- if ( self . state . vectorContentRules . rules . length ) {
259- pushRuleVectorStateKind = self . _pushRuleVectorStateKind ( self . state . vectorContentRules . rules [ 0 ] ) ;
260- }
261- else {
262- // ON is default
263- pushRuleVectorStateKind = PushRuleVectorState . ON ;
264- }
265- }
266-
267- if ( vectorContentRulesPatterns . indexOf ( keyword ) < 0 ) {
268- deferreds . push ( cli . addPushRule ( 'global' , 'content' , keyword , {
269- actions : self . _actionsFor ( pushRuleVectorStateKind ) ,
270- pattern : keyword
271- } ) ) ;
272- }
273- }
274-
275- q . all ( deferreds ) . done ( function ( resps ) {
276- self . _refreshFromServer ( ) ;
277- } , onError ) ;
278- } , onError ) ;
136+
137+ self . _updateKeywords ( newKeywords ) ;
279138 }
280139 }
281140 } ) ;
@@ -326,6 +185,166 @@ module.exports = React.createClass({
326185 }
327186 return stateKind ;
328187 } ,
188+
189+ _changePushRuleVectorState : function ( rule , newPushRuleVectorState ) {
190+ // For now, we support only enabled/disabled for hs default rules
191+ // Translate ON, LOUD, OFF to one of the 2.
192+ if ( rule && rule . vectorState !== newPushRuleVectorState ) {
193+
194+ this . setState ( {
195+ phase : this . phases . LOADING
196+ } ) ;
197+
198+ var self = this ;
199+ MatrixClientPeg . get ( ) . setPushRuleEnabled ( 'global' , rule . rule . kind , rule . rule . rule_id , ( newPushRuleVectorState !== PushRuleVectorState . OFF ) ) . done ( function ( ) {
200+ self . _refreshFromServer ( ) ;
201+ } ) ;
202+ }
203+ } ,
204+
205+ _changeKeywordsPushRuleVectorState : function ( newPushRuleVectorState ) {
206+ // Is there really a change?
207+ if ( this . state . vectorContentRules . vectorState === newPushRuleVectorState
208+ || this . state . vectorContentRules . rules . length === 0 ) {
209+ return ;
210+ }
211+
212+ var self = this ;
213+ var cli = MatrixClientPeg . get ( ) ;
214+
215+ this . setState ( {
216+ phase : this . phases . LOADING
217+ } ) ;
218+
219+ // Update all rules in self.state.vectorContentRules
220+ var deferreds = [ ] ;
221+ for ( var i in this . state . vectorContentRules . rules ) {
222+ var rule = this . state . vectorContentRules . rules [ i ] ;
223+
224+ var enabled ;
225+ var actions ;
226+ switch ( newPushRuleVectorState ) {
227+ case PushRuleVectorState . ON :
228+ if ( rule . actions . length !== 1 ) {
229+ actions = this . _actionsFor ( PushRuleVectorState . ON ) ;
230+ }
231+
232+ if ( this . state . vectorContentRules . vectorState === PushRuleVectorState . OFF ) {
233+ enabled = true ;
234+ }
235+ break ;
236+
237+ case PushRuleVectorState . LOUD :
238+ if ( rule . actions . length !== 3 ) {
239+ actions = this . _actionsFor ( PushRuleVectorState . LOUD ) ;
240+ }
241+
242+ if ( this . state . vectorContentRules . vectorState === PushRuleVectorState . OFF ) {
243+ enabled = true ;
244+ }
245+ break ;
246+
247+ case PushRuleVectorState . OFF :
248+ enabled = false ;
249+ break ;
250+ }
251+
252+ if ( actions ) {
253+ // Note that the workaround in _updatePushRuleActions will automatically
254+ // enable the rule
255+ deferreds . push ( this . _updatePushRuleActions ( rule , actions , enabled ) ) ;
256+ }
257+ else if ( enabled != undefined ) {
258+ deferreds . push ( cli . setPushRuleEnabled ( 'global' , rule . kind , rule . rule_id , enabled ) ) ;
259+ }
260+ }
261+
262+ q . all ( deferreds ) . done ( function ( resps ) {
263+ self . _refreshFromServer ( ) ;
264+ } , function ( error ) {
265+ var ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
266+ Modal . createDialog ( ErrorDialog , {
267+ title : "Can't update user notification settings" ,
268+ description : error . toString ( ) ,
269+ onFinished : self . _refreshFromServer
270+ } ) ;
271+ } ) ;
272+ } ,
273+
274+ _updateKeywords : function ( newKeywords ) {
275+ this . setState ( {
276+ phase : this . phases . LOADING
277+ } ) ;
278+
279+ var self = this ;
280+ var cli = MatrixClientPeg . get ( ) ;
281+ var removeDeferreds = [ ] ;
282+
283+ // Remove per-word push rules of keywords that are no more in the list
284+ var vectorContentRulesPatterns = [ ] ;
285+ for ( var i in self . state . vectorContentRules . rules ) {
286+ var rule = self . state . vectorContentRules . rules [ i ] ;
287+
288+ vectorContentRulesPatterns . push ( rule . pattern ) ;
289+
290+ if ( newKeywords . indexOf ( rule . pattern ) < 0 ) {
291+ removeDeferreds . push ( cli . deletePushRule ( 'global' , rule . kind , rule . rule_id ) ) ;
292+ }
293+ }
294+
295+ // If the keyword is part of `externalContentRules`, remove the rule
296+ // before recreating it in the right Vector path
297+ for ( var i in self . state . externalContentRules ) {
298+ var rule = self . state . externalContentRules [ i ] ;
299+
300+ if ( newKeywords . indexOf ( rule . pattern ) >= 0 ) {
301+ removeDeferreds . push ( cli . deletePushRule ( 'global' , rule . kind , rule . rule_id ) ) ;
302+ }
303+ }
304+
305+ var onError = function ( error ) {
306+ var ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
307+ Modal . createDialog ( ErrorDialog , {
308+ title : "Can't update keywords" ,
309+ description : error . toString ( ) ,
310+ onFinished : self . _refreshFromServer
311+ } ) ;
312+ }
313+
314+ // Then, add the new ones
315+ q . all ( removeDeferreds ) . done ( function ( resps ) {
316+ var deferreds = [ ] ;
317+ for ( var i in newKeywords ) {
318+ var keyword = newKeywords [ i ] ;
319+
320+ var pushRuleVectorStateKind = self . state . vectorContentRules . vectorState ;
321+ if ( pushRuleVectorStateKind === PushRuleVectorState . OFF ) {
322+ // When the current global keywords rule is OFF, we need to look at
323+ // the flavor of rules in 'vectorContentRules' to apply the same actions
324+ // when creating the new rule.
325+ // Thus, this new rule will join the 'vectorContentRules' set.
326+ if ( self . state . vectorContentRules . rules . length ) {
327+ pushRuleVectorStateKind = self . _pushRuleVectorStateKind ( self . state . vectorContentRules . rules [ 0 ] ) ;
328+ }
329+ else {
330+ // ON is default
331+ pushRuleVectorStateKind = PushRuleVectorState . ON ;
332+ }
333+ }
334+
335+ if ( vectorContentRulesPatterns . indexOf ( keyword ) < 0 ) {
336+ deferreds . push ( cli . addPushRule ( 'global' , 'content' , keyword , {
337+ actions : self . _actionsFor ( pushRuleVectorStateKind ) ,
338+ pattern : keyword
339+ } ) ) ;
340+ }
341+ }
342+
343+ q . all ( deferreds ) . done ( function ( resps ) {
344+ self . _refreshFromServer ( ) ;
345+ } , onError ) ;
346+ } , onError ) ;
347+ } ,
329348
330349 _refreshFromServer : function ( ) {
331350 var self = this ;
0 commit comments