Skip to content

Commit 3224a4e

Browse files
committed
Add helper functions for decoding and encoding lists of push actions
1 parent 8b1444c commit 3224a4e

1 file changed

Lines changed: 73 additions & 78 deletions

File tree

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

Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,81 @@ var PushRuleVectorState = {
3737
OFF: "off"
3838
};
3939

40-
41-
var ACTION_NOTIFY = [
42-
"notify",
43-
{
44-
"set_tweak": "highlight",
45-
"value": false
40+
// Encodes a dictionary of {
41+
// "notify": true/false,
42+
// "sound": string or undefined,
43+
// "highlight: true/false,
44+
// }
45+
// to a list of push actions.
46+
function encodeActions(action) {
47+
var notify = action.notify;
48+
var sound = action.sound;
49+
var highlight = action.highlight;
50+
if (notify) {
51+
var actions = ["notify"];
52+
if (sound) {
53+
actions.push({"set_tweak": "sound", "value": sound});
54+
}
55+
if (highlight) {
56+
actions.push({"set_tweak": "highlight"});
57+
} else {
58+
actions.push({"set_tweak": "highlight", "value": false});
59+
}
60+
return actions;
61+
} else {
62+
return ["dont_notify"];
4663
}
47-
];
64+
}
4865

49-
var ACTION_NOTIFY_DEFAULT_SOUND = [
50-
"notify",
51-
{
52-
"set_tweak": "sound",
53-
"value": "default"
54-
},
55-
{
56-
"set_tweak": "highlight",
57-
"value": false
58-
}
59-
];
66+
// Decode a list of actions to a dictionary of {
67+
// "notify": true/false,
68+
// "sound": string or undefined,
69+
// "highlight: true/false,
70+
// }
71+
// If the actions couldn't be decoded then returns null.
72+
function decodeActions(actions) {
73+
var notify = false;
74+
var sound = null;
75+
var highlight = false;
6076

61-
var ACTION_NOTIFY_RING_SOUND = [
62-
"notify",
63-
{
64-
"set_tweak": "sound",
65-
"value": "ring"
66-
},
67-
{
68-
"set_tweak": "highlight",
69-
"value": false
77+
for (var i = 0; i < actions.length; ++i) {
78+
var action = actions[i];
79+
if (action === "notify") {
80+
notify = true;
81+
} else if (action === "dont_notify") {
82+
notify = false;
83+
} else if (typeof action === 'object') {
84+
if (action.set_tweak === "sound") {
85+
sound = action.value
86+
} else if (action.set_tweak === "highlight") {
87+
highlight = action.value;
88+
} else {
89+
// We don't understand this kind of tweak, so give up.
90+
return null;
91+
}
92+
} else {
93+
// We don't understand this kind of action, so give up.
94+
return null;
95+
}
7096
}
71-
];
7297

73-
var ACTION_HIGHLIGHT_DEFAULT_SOUND = [
74-
"notify",
75-
{
76-
"set_tweak": "sound",
77-
"value": "default"
78-
},
79-
{
80-
"set_tweak":"highlight"
98+
if (highlight === undefined) {
99+
// If a highlight tweak is missing a value then it defaults to true.
100+
highlight = true;
81101
}
82-
];
83102

84-
var ACTION_DONT_NOTIFY = [ "dont_notify" ];
103+
var result = {notify: notify, highlight: highlight};
104+
if (sound !== null) {
105+
result.sound = sound;
106+
}
107+
return result;
108+
}
85109

110+
var ACTION_NOTIFY = encodeActions({notify: true});
111+
var ACTION_NOTIFY_DEFAULT_SOUND = encodeActions({notify: true, sound: "default"});
112+
var ACTION_NOTIFY_RING_SOUND = encodeActions({notify: true, sound: "ring"});
113+
var ACTION_HIGHLIGHT_DEFAULT_SOUND = encodeActions({notify: true, sound: "default", highlight: true});
114+
var ACTION_DONT_NOTIFY = encodeActions({notify: false});
86115
var ACTION_DISABLED = null;
87116

88117

@@ -177,47 +206,13 @@ var LEGACY_RULES = {
177206
};
178207

179208
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;
209+
var decoded = decodeActions(actions);
210+
if (decoded !== null) {
211+
return encodeActions(decoded);
219212
} else {
220-
return ACTION_DONT_NOTIFY;
213+
// We don't recognise one of the actions here, so we don't try to
214+
// canonicalise them.
215+
return actions;
221216
}
222217
}
223218

0 commit comments

Comments
 (0)