|
| 1 | +/* |
| 2 | +Copyright 2015, 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 | +var React = require('react'); |
| 19 | +var sdk = require('matrix-react-sdk'); |
| 20 | +var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); |
| 21 | +var UserSettingsStore = require('matrix-react-sdk/lib/UserSettingsStore'); |
| 22 | + |
| 23 | +/** |
| 24 | + * Enum for state of a push rule as defined by the Vector UI. |
| 25 | + * @readonly |
| 26 | + * @enum {string} |
| 27 | + */ |
| 28 | +var PushRuleState = { |
| 29 | + /** The user will receive push notification for this rule */ |
| 30 | + ON: "on", |
| 31 | + /** The user will receive push notification for this rule with sound and |
| 32 | + highlight if this is legitimate */ |
| 33 | + STRONG: "strong", |
| 34 | + /** The push rule is disabled */ |
| 35 | + OFF: "off" |
| 36 | +}; |
| 37 | + |
| 38 | +module.exports = React.createClass({ |
| 39 | + displayName: 'Notififications', |
| 40 | + |
| 41 | + phases: { |
| 42 | + LOADING: "LOADING", // The component is loading or sending data to the hs |
| 43 | + DISPLAY: "DISPLAY", // The component is ready and display data |
| 44 | + ERROR: "ERROR" // There was an error |
| 45 | + }, |
| 46 | + |
| 47 | + getInitialState: function() { |
| 48 | + return { |
| 49 | + phase: this.phases.LOADING, |
| 50 | + vectorPushRules: [] |
| 51 | + }; |
| 52 | + }, |
| 53 | + |
| 54 | + componentWillMount: function() { |
| 55 | + this._refreshFromServer(); |
| 56 | + }, |
| 57 | + |
| 58 | + onEnableNotificationsChange: function(event) { |
| 59 | + UserSettingsStore.setEnableNotifications(event.target.checked); |
| 60 | + }, |
| 61 | + |
| 62 | + onNotifStateButtonClicked: function(event) { |
| 63 | + var vectorRuleId = event.target.className.split("-")[0]; |
| 64 | + var newPushRuleState = event.target.className.split("-")[1]; |
| 65 | + |
| 66 | + var rule = this.getRule(vectorRuleId); |
| 67 | + |
| 68 | + // For now, we support only enabled/disabled. |
| 69 | + // Translate ON, STRONG, OFF to one of the 2. |
| 70 | + if (rule && rule.state !== newPushRuleState) { |
| 71 | + |
| 72 | + this.setState({ |
| 73 | + phase: this.phases.LOADING |
| 74 | + }); |
| 75 | + |
| 76 | + var self = this; |
| 77 | + MatrixClientPeg.get().setPushRuleEnabled('global', rule.rule.kind, rule.rule.rule_id, (newPushRuleState !== PushRuleState.OFF)).done(function() { |
| 78 | + |
| 79 | + self._refreshFromServer(); |
| 80 | + self.forceUpdate(); |
| 81 | + }); |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + getRule: function(vectorRuleId) { |
| 86 | + for (var i in this.state.vectorPushRules) { |
| 87 | + var rule = this.state.vectorPushRules[i]; |
| 88 | + if (rule.vectorRuleId === vectorRuleId) { |
| 89 | + return rule; |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + |
| 94 | + _refreshFromServer: function() { |
| 95 | + var self = this; |
| 96 | + MatrixClientPeg.get().getPushRules().done(function(rulesets) { |
| 97 | + MatrixClientPeg.get().pushRules = rulesets; |
| 98 | + |
| 99 | + // Get homeserver default rules expected by Vector |
| 100 | + var rule_categories = { |
| 101 | + '.m.rule.master': 'master', |
| 102 | + |
| 103 | + '.m.rule.contains_display_name': 'vector', |
| 104 | + '.m.rule.room_one_to_one': 'vector', |
| 105 | + '.m.rule.invite_for_me': 'vector', |
| 106 | + '.m.rule.member_event': 'vector', |
| 107 | + '.m.rule.call': 'vector', |
| 108 | + }; |
| 109 | + |
| 110 | + var defaultRules = {master: [], vector: {}, additional: [], fallthrough: [], suppression: []}; |
| 111 | + for (var kind in rulesets.global) { |
| 112 | + for (var i = 0; i < Object.keys(rulesets.global[kind]).length; ++i) { |
| 113 | + var r = rulesets.global[kind][i]; |
| 114 | + var cat = rule_categories[r.rule_id]; |
| 115 | + r.kind = kind; |
| 116 | + if (r.rule_id[0] === '.') { |
| 117 | + if (cat) { |
| 118 | + if (cat === 'vector') |
| 119 | + { |
| 120 | + defaultRules.vector[r.rule_id] = r; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + defaultRules[cat].push(r); |
| 125 | + } |
| 126 | + } else { |
| 127 | + defaultRules.additional.push(r); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Build the rules displayed by Vector UI |
| 134 | + self.state.vectorPushRules = []; |
| 135 | + var rule, state; |
| 136 | + |
| 137 | + rule = defaultRules.vector['.m.rule.contains_display_name']; |
| 138 | + state = (rule && rule.enabled) ? PushRuleState.STRONG : PushRuleState.OFF; |
| 139 | + self.state.vectorPushRules.push({ |
| 140 | + "vectorRuleId": "contains_display_name", |
| 141 | + "description" : "Messages containing my name", |
| 142 | + "rule": rule, |
| 143 | + "state": state, |
| 144 | + "disabled": PushRuleState.ON |
| 145 | + }); |
| 146 | + |
| 147 | + // TODO: Merge contains_user_name |
| 148 | + |
| 149 | + // TODO: Add "Messages containing keywords" |
| 150 | + |
| 151 | + rule = defaultRules.vector['.m.rule.room_one_to_one']; |
| 152 | + state = (rule && rule.enabled) ? PushRuleState.STRONG : PushRuleState.OFF; |
| 153 | + self.state.vectorPushRules.push({ |
| 154 | + "vectorRuleId": "room_one_to_one", |
| 155 | + "description" : "Messages just sent to me", |
| 156 | + "rule": rule, |
| 157 | + "state": state, |
| 158 | + "disabled": PushRuleState.ON |
| 159 | + }); |
| 160 | + |
| 161 | + rule = defaultRules.vector['.m.rule.invite_for_me']; |
| 162 | + state = (rule && rule.enabled) ? PushRuleState.STRONG : PushRuleState.OFF; |
| 163 | + self.state.vectorPushRules.push({ |
| 164 | + "vectorRuleId": "invite_for_me", |
| 165 | + "description" : "When I'm invited to a room", |
| 166 | + "rule": rule, |
| 167 | + "state": state, |
| 168 | + "disabled": PushRuleState.ON |
| 169 | + }); |
| 170 | + |
| 171 | + rule = defaultRules.vector['.m.rule.member_event']; |
| 172 | + state = (rule && rule.enabled) ? PushRuleState.ON : PushRuleState.OFF; |
| 173 | + self.state.vectorPushRules.push({ |
| 174 | + "vectorRuleId": "member_event", |
| 175 | + "description" : "When people join or leave a room", |
| 176 | + "rule": rule, |
| 177 | + "state": state, |
| 178 | + "disabled": PushRuleState.STRONG |
| 179 | + }); |
| 180 | + |
| 181 | + rule = defaultRules.vector['.m.rule.call']; |
| 182 | + state = (rule && rule.enabled) ? PushRuleState.STRONG : PushRuleState.OFF; |
| 183 | + self.state.vectorPushRules.push({ |
| 184 | + "vectorRuleId": "call", |
| 185 | + "description" : "Call invitation", |
| 186 | + "rule": rule, |
| 187 | + "state": state, |
| 188 | + "disabled": PushRuleState.ON |
| 189 | + }); |
| 190 | + |
| 191 | + self.setState({ |
| 192 | + phase: self.phases.DISPLAY |
| 193 | + }); |
| 194 | + |
| 195 | + self.forceUpdate(); |
| 196 | + }); |
| 197 | + }, |
| 198 | + |
| 199 | + renderNotifRulesTableRow: function(title, className, pushRuleState, disabled) { |
| 200 | + return ( |
| 201 | + <tr key = {className}> |
| 202 | + <th class="">{title}</th> |
| 203 | + |
| 204 | + <th> |
| 205 | + <input className= {className + "-" + PushRuleState.ON} |
| 206 | + type="radio" |
| 207 | + checked={ pushRuleState === PushRuleState.ON } |
| 208 | + disabled= { (disabled === PushRuleState.ON) ? "disabled" : false } |
| 209 | + onChange={ this.onNotifStateButtonClicked } /> |
| 210 | + </th> |
| 211 | + |
| 212 | + <th> |
| 213 | + <input className= {className + "-" + PushRuleState.STRONG} |
| 214 | + type="radio" |
| 215 | + checked={ pushRuleState === PushRuleState.STRONG } |
| 216 | + disabled= { (disabled === PushRuleState.STRONG) ? "disabled" : false } |
| 217 | + onChange={ this.onNotifStateButtonClicked } /> |
| 218 | + </th> |
| 219 | + |
| 220 | + <th> |
| 221 | + <input className= {className + "-" + PushRuleState.OFF} |
| 222 | + type="radio" |
| 223 | + checked={ pushRuleState === PushRuleState.OFF } |
| 224 | + disabled= { (disabled === PushRuleState.OFF) ? "disabled" : false } |
| 225 | + onChange={ this.onNotifStateButtonClicked } /> |
| 226 | + </th> |
| 227 | + </tr> |
| 228 | + ); |
| 229 | + }, |
| 230 | + |
| 231 | + renderNotifRulesTableRows: function() { |
| 232 | + var rows = []; |
| 233 | + for (var i in this.state.vectorPushRules) { |
| 234 | + var rule = this.state.vectorPushRules[i]; |
| 235 | + rows.push(this.renderNotifRulesTableRow(rule.description, rule.vectorRuleId, rule.state, rule.disabled)); |
| 236 | + } |
| 237 | + return rows; |
| 238 | + }, |
| 239 | + |
| 240 | + render: function() { |
| 241 | + if (this.state.phase === this.phases.LOADING) { |
| 242 | + var Loader = sdk.getComponent("elements.Spinner"); |
| 243 | + return ( |
| 244 | + <div className="mx_UserSettings_notifTable"> |
| 245 | + <Loader /> |
| 246 | + </div> |
| 247 | + ); |
| 248 | + } |
| 249 | + |
| 250 | + return ( |
| 251 | + <div className="mx_UserSettings_notifTable"> |
| 252 | + |
| 253 | + <div className="mx_UserNotifSettings_tableRow"> |
| 254 | + <div className="mx_UserNotifSettings_inputCell"> |
| 255 | + <input id="enableNotifications" |
| 256 | + ref="enableNotifications" |
| 257 | + type="checkbox" |
| 258 | + checked={ UserSettingsStore.getEnableNotifications() } |
| 259 | + onChange={ this.onEnableNotificationsChange } /> |
| 260 | + </div> |
| 261 | + <div className="mx_UserNotifSettings_labelCell"> |
| 262 | + <label htmlFor="enableNotifications"> |
| 263 | + Enable desktop notifications |
| 264 | + </label> |
| 265 | + </div> |
| 266 | + </div> |
| 267 | + |
| 268 | + <h3>General use </h3> |
| 269 | + |
| 270 | + <div className="mx_UserNotifSettings_pushRulesTableWrapper"> |
| 271 | + <table className="mx_UserNotifSettings_pushRulesTable"> |
| 272 | + <thead> |
| 273 | + <tr> |
| 274 | + <th width="55%"></th> |
| 275 | + <th width="15%">Normal</th> |
| 276 | + <th width="15%">Strong</th> |
| 277 | + <th width="15%">Off</th> |
| 278 | + </tr> |
| 279 | + </thead> |
| 280 | + <tbody> |
| 281 | + |
| 282 | + { this.renderNotifRulesTableRows() } |
| 283 | + |
| 284 | + </tbody> |
| 285 | + </table> |
| 286 | + </div> |
| 287 | + |
| 288 | + </div> |
| 289 | + ); |
| 290 | + } |
| 291 | +}); |
0 commit comments