2016-01-19 17:25:36 +03:00
|
|
|
import Ember from 'ember';
|
2017-05-29 21:50:03 +03:00
|
|
|
import RSVP from 'rsvp';
|
2016-06-30 13:21:47 +03:00
|
|
|
import Service from 'ember-service';
|
|
|
|
import computed from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import set from 'ember-metal/set';
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
// ember-cli-shims doesn't export Error
|
|
|
|
const {Error: EmberError} = Ember;
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
export function feature(name, user = false) {
|
|
|
|
let watchedProps = user ? [`accessibility.${name}`] : [`config.${name}`, `labs.${name}`];
|
|
|
|
|
|
|
|
return computed.apply(Ember, watchedProps.concat({
|
2016-01-19 17:25:36 +03:00
|
|
|
get() {
|
2017-03-03 19:14:33 +03:00
|
|
|
if (user) {
|
|
|
|
return this.get(`accessibility.${name}`);
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
if (this.get(`config.${name}`)) {
|
|
|
|
return this.get(`config.${name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.get(`labs.${name}`) || false;
|
2016-01-19 17:25:36 +03:00
|
|
|
},
|
|
|
|
set(key, value) {
|
2017-03-03 19:14:33 +03:00
|
|
|
this.update(key, value, user);
|
2016-05-05 17:03:09 +03:00
|
|
|
return value;
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
2017-03-03 19:14:33 +03:00
|
|
|
}));
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Service.extend({
|
2016-06-30 13:21:47 +03:00
|
|
|
store: injectService(),
|
|
|
|
config: injectService(),
|
2017-03-03 19:14:33 +03:00
|
|
|
session: injectService(),
|
2017-03-17 20:16:21 +03:00
|
|
|
settings: injectService(),
|
2016-06-30 13:21:47 +03:00
|
|
|
notifications: injectService(),
|
2016-01-19 17:25:36 +03:00
|
|
|
|
|
|
|
publicAPI: feature('publicAPI'),
|
2016-04-15 17:45:50 +03:00
|
|
|
subscribers: feature('subscribers'),
|
2017-03-03 19:14:33 +03:00
|
|
|
nightShift: feature('nightShift', true),
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
_user: null,
|
2016-02-23 18:00:17 +03:00
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
labs: computed('settings.labs', function () {
|
|
|
|
let labs = this.get('settings.labs');
|
2016-01-19 17:25:36 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(labs) || {};
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-05-05 17:03:09 +03:00
|
|
|
}),
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
accessibility: computed('_user.accessibility', function () {
|
|
|
|
let accessibility = this.get('_user.accessibility');
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(accessibility) || {};
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
fetch() {
|
2017-03-03 19:14:33 +03:00
|
|
|
return RSVP.hash({
|
2017-03-17 20:16:21 +03:00
|
|
|
settings: this.get('settings').fetch(),
|
2017-03-03 19:14:33 +03:00
|
|
|
user: this.get('session.user')
|
2017-03-17 20:16:21 +03:00
|
|
|
}).then(({user}) => {
|
2017-03-03 19:14:33 +03:00
|
|
|
this.set('_user', user);
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
return true;
|
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
},
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
update(key, value, user = false) {
|
|
|
|
let serviceProperty = user ? 'accessibility' : 'labs';
|
2017-03-17 20:16:21 +03:00
|
|
|
let model = this.get(user ? '_user' : 'settings');
|
2017-03-03 19:14:33 +03:00
|
|
|
let featureObject = this.get(serviceProperty);
|
|
|
|
|
|
|
|
// set the new key value for either the labs property or the accessibility property
|
|
|
|
set(featureObject, key, value);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
// update the 'labs' or 'accessibility' key of the model
|
|
|
|
model.set(serviceProperty, JSON.stringify(featureObject));
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return model.save().then(() => {
|
2016-05-05 17:03:09 +03:00
|
|
|
// return the labs key value that we get from the server
|
2017-03-03 19:14:33 +03:00
|
|
|
this.notifyPropertyChange(serviceProperty);
|
|
|
|
return this.get(`${serviceProperty}.${key}`);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
}).catch((error) => {
|
2017-03-03 19:14:33 +03:00
|
|
|
model.rollbackAttributes();
|
|
|
|
this.notifyPropertyChange(serviceProperty);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
|
|
|
// we'll always have an errors object unless we hit a
|
|
|
|
// validation error
|
2016-06-30 17:45:02 +03:00
|
|
|
if (!error) {
|
2017-03-03 19:14:33 +03:00
|
|
|
throw new EmberError(`Validation of the feature service ${user ? 'user' : 'settings'} model failed when updating ${serviceProperty}.`);
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
this.get('notifications').showAPIError(error);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return this.get(`${serviceProperty}.${key}`);
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|