|
| 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 | +}; |
0 commit comments