@@ -83,12 +83,12 @@ module.exports = React.createClass({
8383 var newPushRuleVectorState = event . target . className . split ( "-" ) [ 1 ] ;
8484
8585 if ( "keywords" === vectorRuleId ) {
86- this . _changeKeywordsPushRuleVectorState ( newPushRuleVectorState )
86+ this . _setKeywordsPushRuleVectorState ( newPushRuleVectorState )
8787 }
8888 else {
8989 var rule = this . getRule ( vectorRuleId ) ;
9090 if ( rule ) {
91- this . _changePushRuleVectorState ( rule , newPushRuleVectorState ) ;
91+ this . _setPushRuleVectorState ( rule , newPushRuleVectorState ) ;
9292 }
9393 }
9494 } ,
@@ -134,7 +134,7 @@ module.exports = React.createClass({
134134 return array ;
135135 } , [ ] ) ;
136136
137- self . _updateKeywords ( newKeywords ) ;
137+ self . _setKeywords ( newKeywords ) ;
138138 }
139139 }
140140 } ) ;
@@ -186,7 +186,7 @@ module.exports = React.createClass({
186186 return stateKind ;
187187 } ,
188188
189- _changePushRuleVectorState : function ( rule , newPushRuleVectorState ) {
189+ _setPushRuleVectorState : function ( rule , newPushRuleVectorState ) {
190190 // For now, we support only enabled/disabled for hs default rules
191191 // Translate ON, LOUD, OFF to one of the 2.
192192 if ( rule && rule . vectorState !== newPushRuleVectorState ) {
@@ -202,7 +202,7 @@ module.exports = React.createClass({
202202 }
203203 } ,
204204
205- _changeKeywordsPushRuleVectorState : function ( newPushRuleVectorState ) {
205+ _setKeywordsPushRuleVectorState : function ( newPushRuleVectorState ) {
206206 // Is there really a change?
207207 if ( this . state . vectorContentRules . vectorState === newPushRuleVectorState
208208 || this . state . vectorContentRules . rules . length === 0 ) {
@@ -221,8 +221,7 @@ module.exports = React.createClass({
221221 for ( var i in this . state . vectorContentRules . rules ) {
222222 var rule = this . state . vectorContentRules . rules [ i ] ;
223223
224- var enabled ;
225- var actions ;
224+ var enabled , actions ;
226225 switch ( newPushRuleVectorState ) {
227226 case PushRuleVectorState . ON :
228227 if ( rule . actions . length !== 1 ) {
@@ -271,7 +270,7 @@ module.exports = React.createClass({
271270 } ) ;
272271 } ,
273272
274- _updateKeywords : function ( newKeywords ) {
273+ _setKeywords : function ( newKeywords ) {
275274 this . setState ( {
276275 phase : this . phases . LOADING
277276 } ) ;
@@ -314,30 +313,40 @@ module.exports = React.createClass({
314313 // Then, add the new ones
315314 q . all ( removeDeferreds ) . done ( function ( resps ) {
316315 var deferreds = [ ] ;
316+
317+ var pushRuleVectorStateKind = self . state . vectorContentRules . vectorState ;
318+ if ( pushRuleVectorStateKind === PushRuleVectorState . OFF ) {
319+ // When the current global keywords rule is OFF, we need to look at
320+ // the flavor of rules in 'vectorContentRules' to apply the same actions
321+ // when creating the new rule.
322+ // Thus, this new rule will join the 'vectorContentRules' set.
323+ if ( self . state . vectorContentRules . rules . length ) {
324+ pushRuleVectorStateKind = self . _pushRuleVectorStateKind ( self . state . vectorContentRules . rules [ 0 ] ) ;
325+ }
326+ else {
327+ // ON is default
328+ pushRuleVectorStateKind = PushRuleVectorState . ON ;
329+ }
330+ }
331+
317332 for ( var i in newKeywords ) {
318333 var keyword = newKeywords [ i ] ;
319334
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 ] ) ;
335+ if ( vectorContentRulesPatterns . indexOf ( keyword ) < 0 ) {
336+ if ( self . state . vectorContentRules . vectorState !== PushRuleVectorState . OFF ) {
337+ deferreds . push ( cli . addPushRule
338+ ( 'global' , 'content' , keyword , {
339+ actions : self . _actionsFor ( pushRuleVectorStateKind ) ,
340+ pattern : keyword
341+ } ) ) ;
328342 }
329343 else {
330- // ON is default
331- pushRuleVectorStateKind = PushRuleVectorState . ON ;
344+ deferreds . push ( self . _addDisabledPushRule ( 'global' , 'content' , keyword , {
345+ actions : self . _actionsFor ( pushRuleVectorStateKind ) ,
346+ pattern : keyword
347+ } ) ) ;
332348 }
333349 }
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- }
341350 }
342351
343352 q . all ( deferreds ) . done ( function ( resps ) {
@@ -346,6 +355,24 @@ module.exports = React.createClass({
346355 } , onError ) ;
347356 } ,
348357
358+ // Create a push rule but disabled
359+ _addDisabledPushRule : function ( scope , kind , ruleId , body ) {
360+ var cli = MatrixClientPeg . get ( ) ;
361+ var deferred = q . defer ( ) ;
362+
363+ cli . addPushRule ( scope , kind , ruleId , body ) . done ( function ( ) {
364+ cli . setPushRuleEnabled ( scope , kind , ruleId , false ) . done ( function ( ) {
365+ deferred . resolve ( ) ;
366+ } , function ( err ) {
367+ deferred . reject ( err ) ;
368+ } ) ;
369+ } , function ( err ) {
370+ deferred . reject ( err ) ;
371+ } ) ;
372+
373+ return deferred . promise ;
374+ } ,
375+
349376 _refreshFromServer : function ( ) {
350377 var self = this ;
351378 MatrixClientPeg . get ( ) . getPushRules ( ) . done ( function ( rulesets ) {
0 commit comments