Skip to content

Commit 8b1444c

Browse files
committed
Port the legacy im.vector rules to the new format on startup
1 parent 3b2d0a6 commit 8b1444c

1 file changed

Lines changed: 95 additions & 15 deletions

File tree

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

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ var ACTION_DONT_NOTIFY = [ "dont_notify" ];
8585

8686
var ACTION_DISABLED = null;
8787

88+
8889
/**
8990
* The descriptions of rules managed by the Vector UI.
90-
* Each rule is described so that if the server does not have it in its default
91-
* rules or if the user wants to use actions ('PushRuleVectorState') that are
92-
* different from the hs one, the code will create a new rule that will override
93-
* the hs one.
9491
*/
9592
var VectorPushRulesDefinitions = {
9693

@@ -165,6 +162,66 @@ var VectorPushRulesDefinitions = {
165162
}
166163
};
167164

165+
/**
166+
* Rules that Vector used to set in order to override the actions of default rules.
167+
* These are used to port peoples existing overrides to match the current API.
168+
* These can be removed and forgotten once everyone has moved to the new client.
169+
*/
170+
var LEGACY_RULES = {
171+
"im.vector.rule.contains_display_name": ".m.rule.contains_display_name",
172+
"im.vector.rule.room_one_to_one": ".m.rule.room_one_to_one",
173+
"im.vector.rule.room_message": ".m.rule.message",
174+
"im.vector.rule.invite_for_me": ".m.rule.invite_for_me",
175+
"im.vector.rule.call": ".m.rule.call",
176+
"im.vector.rule.notices": ".m.rule.suppress_notices"
177+
};
178+
179+
function portLegacyActions(actions) {
180+
var notify = false;
181+
var sound = null;
182+
var highlight = false;
183+
var unknown_action = false;
184+
185+
for (var i = 0; i < actions.length; ++i) {
186+
var action = actions[i];
187+
if (action === "notify") {
188+
notify = true;
189+
} else if (action === "dont_notify") {
190+
notify = false;
191+
} else if (typeof action === 'object') {
192+
if (action.set_tweak === "sound") {
193+
sound = action.value
194+
} else if (action.set_tweak === "highlight") {
195+
highlight = action.value;
196+
} else {
197+
unknown_action = true;
198+
}
199+
} else {
200+
unknown_action = true;
201+
}
202+
}
203+
204+
// We don't regconise one of the actions here, so we don't try to
205+
// canonicalise them.
206+
if (unknown_action) return actions;
207+
208+
if (notify) {
209+
var new_actions = ["notify"];
210+
if (sound !== null) {
211+
new_actions.push({"set_tweak": "sound", "value": sound});
212+
}
213+
if (highlight) {
214+
new_actions.push({"set_tweak": "highlight"});
215+
} else {
216+
new_actions.push({"set_tweak": "highlight", "value": false});
217+
}
218+
return new_actions;
219+
} else {
220+
return ACTION_DONT_NOTIFY;
221+
}
222+
}
223+
224+
168225
module.exports = React.createClass({
169226
displayName: 'Notififications',
170227

@@ -521,23 +578,45 @@ module.exports = React.createClass({
521578
return deferred.promise;
522579
},
523580

524-
// Add a push rule server side according to the 'VectorPushRulesDefinitions' spec
525-
_addOverridingVectorPushRule: function(vectorRuleId, vectorState) {
581+
// Check if any legacy im.vector rules need to be ported to the new API
582+
// for overriding the actions of default rules.
583+
_portRulesToNewAPI: function(rulesets) {
526584
var self = this;
585+
var needsUpdate = [];
586+
var cli = MatrixClientPeg.get();
527587

528-
// Create the rule as predefined
529-
var ruleDefinition = VectorPushRulesDefinitions[vectorRuleId];
530-
var body = {
531-
conditions: ruleDefinition.conditions,
532-
actions: ruleDefinition.vectorStateToActions[vectorState]
588+
for (var kind in rulesets.global) {
589+
var ruleset = rulesets.global[kind];
590+
for (var i = 0; i < ruleset.length; ++i) {
591+
var rule = ruleset[i];
592+
if (rule.rule_id in LEGACY_RULES) {
593+
console.log("Porting legacy rule", rule);
594+
needsUpdate.push( function(kind, rule) {
595+
return cli.setPushRuleActions(
596+
'global', kind, LEGACY_RULES[rule.rule_id], portLegacyActions(rule.actions)
597+
).then( function() {
598+
return cli.deletePushRule('global', kind, rule.rule_id);
599+
})
600+
}(kind, rule));
601+
}
602+
}
603+
}
604+
605+
if (needsUpdate.length > 0) {
606+
// If somme of the rules need to be ported then wait for the porting
607+
// to happen and then fetch the rules again.
608+
return q.allSettled(needsUpdate).then( function() {
609+
return cli.getPushRules();
610+
});
611+
} else {
612+
// Otherwise return the rules that we already have.
613+
return rulesets;
533614
}
534-
535-
return MatrixClientPeg.get().addPushRule('global', ruleDefinition.kind, vectorRuleId, body);
536615
},
537616

538617
_refreshFromServer: function() {
539618
var self = this;
540-
MatrixClientPeg.get().getPushRules().done(function(rulesets) {
619+
MatrixClientPeg.get().getPushRules().then(self._portRulesToNewAPI).done(function(rulesets) {
541620
MatrixClientPeg.get().pushRules = rulesets;
542621

543622
// Get homeserver default rules and triage them by categories
@@ -568,6 +647,7 @@ module.exports = React.createClass({
568647
var r = rulesets.global[kind][i];
569648
var cat = rule_categories[r.rule_id];
570649
r.kind = kind;
650+
571651
if (r.rule_id[0] === '.') {
572652
if (cat === 'vector') {
573653
defaultRules.vector[r.rule_id] = r;
@@ -842,7 +922,7 @@ module.exports = React.createClass({
842922

843923
// When enabled, the master rule inhibits all existing rules
844924
// So do not show all notification settings
845-
if (this.state.masterPushRule.enabled) {
925+
if (this.state.masterPushRule && this.state.masterPushRule.enabled) {
846926
return (
847927
<div>
848928
{masterPushRuleDiv}

0 commit comments

Comments
 (0)