Skip to content

Commit c6b5018

Browse files
committed
Move more stuff out of Notifications.js
1 parent 0996a0b commit c6b5018

3 files changed

Lines changed: 135 additions & 70 deletions

File tree

‎src/components/views/settings/Notifications.js‎

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var notifications = require('../../../notifications');
3030
var NotificationUtils = notifications.NotificationUtils;
3131
var VectorPushRulesDefinitions = notifications.VectorPushRulesDefinitions;
3232
var PushRuleVectorState = notifications.PushRuleVectorState;
33+
var ContentRules = notifications.ContentRules;
3334

3435
/**
3536
* Rules that Vector used to set in order to override the actions of default rules.
@@ -429,8 +430,6 @@ module.exports = React.createClass({
429430

430431
// HS default rules
431432
var defaultRules = {master: [], vector: {}, others: []};
432-
// Content/keyword rules
433-
var contentRules = {on: [], on_but_disabled:[], loud: [], loud_but_disabled: [], other: []};
434433

435434
for (var kind in rulesets.global) {
436435
for (var i = 0; i < Object.keys(rulesets.global[kind]).length; ++i) {
@@ -449,82 +448,22 @@ module.exports = React.createClass({
449448
defaultRules['others'].push(r);
450449
}
451450
}
452-
else if (kind === 'content') {
453-
switch (PushRuleVectorState.contentRuleVectorStateKind(r)) {
454-
case PushRuleVectorState.ON:
455-
if (r.enabled) {
456-
contentRules.on.push(r);
457-
}
458-
else {
459-
contentRules.on_but_disabled.push(r);
460-
}
461-
break;
462-
case PushRuleVectorState.LOUD:
463-
if (r.enabled) {
464-
contentRules.loud.push(r);
465-
}
466-
else {
467-
contentRules.loud_but_disabled.push(r);
468-
}
469-
break;
470-
default:
471-
contentRules.other.push(r);
472-
break;
473-
}
474-
}
475-
}
476-
}
477-
478-
// Decide which content rules to display in Vector UI.
479-
// Vector displays a single global rule for a list of keywords
480-
// whereas Matrix has a push rule per keyword.
481-
// Vector can set the unique rule in ON, LOUD or OFF state.
482-
// Matrix has enabled/disabled plus a combination of (highlight, sound) tweaks.
483-
484-
// The code below determines which set of user's content push rules can be
485-
// displayed by the vector UI.
486-
// Push rules that does not fit, ie defined by another Matrix client, ends
487-
// in self.state.externalContentRules.
488-
// There is priority in the determination of which set will be the displayed one.
489-
// The set with rules that have LOUD tweaks is the first choice. Then, the ones
490-
// with ON tweaks (no tweaks).
491-
if (contentRules.loud.length) {
492-
self.state.vectorContentRules = {
493-
vectorState: PushRuleVectorState.LOUD,
494-
rules: contentRules.loud
495451
}
496-
self.state.externalContentRules = [].concat(contentRules.loud_but_disabled, contentRules.on, contentRules.on_but_disabled, contentRules.other);
497-
}
498-
else if (contentRules.loud_but_disabled.length) {
499-
self.state.vectorContentRules = {
500-
vectorState: PushRuleVectorState.OFF,
501-
rules: contentRules.loud_but_disabled
502-
}
503-
self.state.externalContentRules = [].concat(contentRules.on, contentRules.on_but_disabled, contentRules.other);
504-
}
505-
else if (contentRules.on.length) {
506-
self.state.vectorContentRules = {
507-
vectorState: PushRuleVectorState.ON,
508-
rules: contentRules.on
509-
}
510-
self.state.externalContentRules = [].concat(contentRules.on_but_disabled, contentRules.other);
511-
}
512-
else if (contentRules.on_but_disabled.length) {
513-
self.state.vectorContentRules = {
514-
vectorState: PushRuleVectorState.OFF,
515-
rules: contentRules.on_but_disabled
516-
}
517-
self.state.externalContentRules = contentRules.other;
518-
}
519-
else {
520-
self.state.externalContentRules = contentRules.other;
521452
}
522453

523454
// Get the master rule if any defined by the hs
524455
if (defaultRules.master.length > 0) {
525456
self.state.masterPushRule = defaultRules.master[0];
526457
}
527458

459+
// parse the keyword rules into our state
460+
var contentRules = ContentRules.parseContentRules(rulesets);
461+
self.state.vectorContentRules = {
462+
vectorState: contentRules.vectorState,
463+
rules: contentRules.rules,
464+
};
465+
self.state.externalContentRules = contentRules.externalRules;
466+
528467
// Build the rules displayed in the Vector UI matrix table
529468
self.state.vectorPushRules = [];
530469

‎src/notifications/ContentRules.js‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Copyright 2016 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var PushRuleVectorState = require('./PushRuleVectorState');
20+
21+
module.exports = {
22+
/**
23+
* Extract the keyword rules from a list of rules, and parse them
24+
* into a form which is useful for Vector's UI.
25+
*
26+
* Returns an object containing:
27+
* rules: the primary list of keyword rules
28+
* vectorState: a PushRuleVectorState indicating whether those rules are
29+
* OFF/ON/LOUD
30+
* externalRules: a list of other keyword rules, with states other than
31+
* vectorState
32+
*/
33+
parseContentRules: function(rulesets) {
34+
// first categorise the keyword rules in terms of their actions
35+
var contentRules = this._categoriseContentRules(rulesets);
36+
37+
// Decide which content rules to display in Vector UI.
38+
// Vector displays a single global rule for a list of keywords
39+
// whereas Matrix has a push rule per keyword.
40+
// Vector can set the unique rule in ON, LOUD or OFF state.
41+
// Matrix has enabled/disabled plus a combination of (highlight, sound) tweaks.
42+
43+
// The code below determines which set of user's content push rules can be
44+
// displayed by the vector UI.
45+
// Push rules that does not fit, ie defined by another Matrix client, ends
46+
// in externalRules.
47+
// There is priority in the determination of which set will be the displayed one.
48+
// The set with rules that have LOUD tweaks is the first choice. Then, the ones
49+
// with ON tweaks (no tweaks).
50+
51+
if (contentRules.loud.length) {
52+
return {
53+
vectorState: PushRuleVectorState.LOUD,
54+
rules: contentRules.loud,
55+
externalRules: [].concat(contentRules.loud_but_disabled, contentRules.on, contentRules.on_but_disabled, contentRules.other),
56+
};
57+
}
58+
else if (contentRules.loud_but_disabled.length) {
59+
return {
60+
vectorState: PushRuleVectorState.OFF,
61+
rules: contentRules.loud_but_disabled,
62+
externalRules: [].concat(contentRules.on, contentRules.on_but_disabled, contentRules.other),
63+
};
64+
}
65+
else if (contentRules.on.length) {
66+
return {
67+
vectorState: PushRuleVectorState.ON,
68+
rules: contentRules.on,
69+
externalRules: [].concat(contentRules.on_but_disabled, contentRules.other),
70+
};
71+
}
72+
else if (contentRules.on_but_disabled.length) {
73+
return {
74+
vectorState: PushRuleVectorState.OFF,
75+
rules: contentRules.on_but_disabled,
76+
externalRules: contentRules.other,
77+
}
78+
} else {
79+
return {
80+
vectorState: PushRuleVectorState.ON,
81+
rules: [],
82+
externalRules: contentRules.other,
83+
}
84+
}
85+
},
86+
87+
_categoriseContentRules: function(rulesets) {
88+
var contentRules = {on: [], on_but_disabled:[], loud: [], loud_but_disabled: [], other: []};
89+
for (var kind in rulesets.global) {
90+
for (var i = 0; i < Object.keys(rulesets.global[kind]).length; ++i) {
91+
var r = rulesets.global[kind][i];
92+
93+
// check it's not a default rule
94+
if (r.rule_id[0] === '.' || kind !== 'content') {
95+
continue;
96+
}
97+
98+
r.kind = kind; // is this needed? not sure
99+
100+
switch (PushRuleVectorState.contentRuleVectorStateKind(r)) {
101+
case PushRuleVectorState.ON:
102+
if (r.enabled) {
103+
contentRules.on.push(r);
104+
}
105+
else {
106+
contentRules.on_but_disabled.push(r);
107+
}
108+
break;
109+
case PushRuleVectorState.LOUD:
110+
if (r.enabled) {
111+
contentRules.loud.push(r);
112+
}
113+
else {
114+
contentRules.loud_but_disabled.push(r);
115+
}
116+
break;
117+
default:
118+
contentRules.other.push(r);
119+
break;
120+
}
121+
}
122+
}
123+
return contentRules;
124+
},
125+
};

‎src/notifications/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ module.exports = {
2020
NotificationUtils: require('./NotificationUtils'),
2121
PushRuleVectorState: require('./PushRuleVectorState'),
2222
VectorPushRulesDefinitions: require('./VectorPushRulesDefinitions'),
23+
ContentRules: require('./ContentRules'),
2324
};

0 commit comments

Comments
 (0)