2016-06-30 13:21:47 +03:00
|
|
|
import Service from 'ember-service';
|
|
|
|
import get from 'ember-metal/get';
|
2016-06-30 17:45:02 +03:00
|
|
|
import injectService from 'ember-service/inject';
|
2017-05-29 21:50:03 +03:00
|
|
|
import set from 'ember-metal/set';
|
2016-06-14 14:46:24 +03:00
|
|
|
import {dasherize} from 'ember-string';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {A as emberA, isEmberArray} from 'ember-array/utils';
|
|
|
|
import {filter} from 'ember-computed';
|
|
|
|
import {isBlank} from 'ember-utils';
|
2016-07-08 16:54:36 +03:00
|
|
|
import {
|
|
|
|
isMaintenanceError,
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2015-10-07 17:44:23 +03:00
|
|
|
// Notification keys take the form of "noun.verb.message", eg:
|
|
|
|
//
|
|
|
|
// "invite.resend.api-error"
|
|
|
|
// "user.invite.already-invited"
|
|
|
|
//
|
|
|
|
// The "noun.verb" part will be used as the "key base" in duplicate checks
|
|
|
|
// to avoid stacking of multiple error messages whilst leaving enough
|
|
|
|
// specificity to re-use keys for i18n lookups
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Service.extend({
|
|
|
|
delayedNotifications: emberA(),
|
|
|
|
content: emberA(),
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
upgradeStatus: injectService(),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
alerts: filter('content', function (notification) {
|
|
|
|
let status = get(notification, 'status');
|
2015-06-19 00:56:18 +03:00
|
|
|
return status === 'alert';
|
|
|
|
}),
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
notifications: filter('content', function (notification) {
|
|
|
|
let status = get(notification, 'status');
|
2015-06-19 00:56:18 +03:00
|
|
|
return status === 'notification';
|
|
|
|
}),
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
handleNotification(message, delayed) {
|
2015-06-19 00:56:18 +03:00
|
|
|
// If this is an alert message from the server, treat it as html safe
|
|
|
|
if (typeof message.toJSON === 'function' && message.get('status') === 'alert') {
|
|
|
|
message.set('message', message.get('message').htmlSafe());
|
2014-07-09 08:17:30 +04:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!get(message, 'status')) {
|
|
|
|
set(message, 'status', 'notification');
|
2014-06-30 01:45:03 +04:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:44:23 +03:00
|
|
|
// close existing duplicate alerts/notifications to avoid stacking
|
2015-10-28 14:36:45 +03:00
|
|
|
if (get(message, 'key')) {
|
|
|
|
this._removeItems(get(message, 'status'), get(message, 'key'));
|
2015-10-07 17:44:23 +03:00
|
|
|
}
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
if (!delayed) {
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('content').pushObject(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
} else {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.get('delayedNotifications').pushObject(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
showAlert(message, options) {
|
2014-08-01 09:40:49 +04:00
|
|
|
options = options || {};
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
this.handleNotification({
|
2015-10-28 14:36:45 +03:00
|
|
|
message,
|
2015-06-19 00:56:18 +03:00
|
|
|
status: 'alert',
|
2015-10-07 17:44:23 +03:00
|
|
|
type: options.type,
|
|
|
|
key: options.key
|
2015-06-19 00:56:18 +03:00
|
|
|
}, options.delayed);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
showNotification(message, options) {
|
2015-06-19 00:56:18 +03:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (!options.doNotCloseNotifications) {
|
|
|
|
this.closeNotifications();
|
2015-10-07 17:44:23 +03:00
|
|
|
} else {
|
|
|
|
// TODO: this should be removed along with showErrors
|
|
|
|
options.key = undefined;
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
this.handleNotification({
|
2015-10-28 14:36:45 +03:00
|
|
|
message,
|
2015-06-19 00:56:18 +03:00
|
|
|
status: 'notification',
|
2015-10-07 17:44:23 +03:00
|
|
|
type: options.type,
|
|
|
|
key: options.key
|
2014-08-01 09:40:49 +04:00
|
|
|
}, options.delayed);
|
2014-03-22 16:08:15 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
showAPIError(resp, options) {
|
2016-06-30 17:45:02 +03:00
|
|
|
// handle "global" errors
|
|
|
|
if (isVersionMismatchError(resp)) {
|
|
|
|
return this.get('upgradeStatus').requireUpgrade();
|
2016-07-08 16:54:36 +03:00
|
|
|
} else if (isMaintenanceError(resp)) {
|
|
|
|
return this.get('upgradeStatus').maintenanceAlert();
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 19:59:04 +03:00
|
|
|
// loop over ember-ajax errors object
|
2016-06-30 17:45:02 +03:00
|
|
|
if (resp && isEmberArray(resp.errors)) {
|
|
|
|
return resp.errors.forEach((error) => {
|
|
|
|
this._showAPIError(error, options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._showAPIError(resp, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
_showAPIError(resp, options) {
|
2014-08-01 09:40:49 +04:00
|
|
|
options = options || {};
|
2015-06-19 00:56:18 +03:00
|
|
|
options.type = options.type || 'error';
|
2014-08-01 09:40:49 +04:00
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
// if possible use the title to get a unique key
|
|
|
|
// - we only show one alert for each key so if we get multiple errors
|
|
|
|
// only the last one will be shown
|
|
|
|
if (!options.key && !isBlank(get(resp, 'title'))) {
|
|
|
|
options.key = dasherize(get(resp, 'title'));
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
options.key = ['api-error', options.key].compact().join('.');
|
2014-08-01 09:40:49 +04:00
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
let msg = options.defaultErrorText || 'There was a problem on the server, please try again.';
|
2014-05-15 03:36:13 +04:00
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
if (resp instanceof String) {
|
|
|
|
msg = resp;
|
|
|
|
} else if (!isBlank(get(resp, 'detail'))) {
|
|
|
|
msg = resp.detail;
|
|
|
|
} else if (!isBlank(get(resp, 'message'))) {
|
|
|
|
msg = resp.message;
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
|
|
|
|
this.showAlert(msg, options);
|
2014-06-24 14:00:28 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
displayDelayed() {
|
|
|
|
this.delayedNotifications.forEach((message) => {
|
|
|
|
this.get('content').pushObject(message);
|
2014-03-22 16:08:15 +04:00
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
this.delayedNotifications = [];
|
2014-06-19 23:44:44 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
closeNotification(notification) {
|
|
|
|
let content = this.get('content');
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
if (typeof notification.toJSON === 'function') {
|
2014-06-30 01:45:03 +04:00
|
|
|
notification.deleteRecord();
|
2015-10-28 14:36:45 +03:00
|
|
|
notification.save().finally(() => {
|
2015-05-26 05:10:50 +03:00
|
|
|
content.removeObject(notification);
|
2014-06-30 01:45:03 +04:00
|
|
|
});
|
|
|
|
} else {
|
2015-05-26 05:10:50 +03:00
|
|
|
content.removeObject(notification);
|
2014-06-30 01:45:03 +04:00
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
closeNotifications(key) {
|
2015-10-07 17:44:23 +03:00
|
|
|
this._removeItems('notification', key);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
closeAlerts(key) {
|
2015-10-07 17:44:23 +03:00
|
|
|
this._removeItems('alert', key);
|
2014-06-30 01:45:03 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
clearAll() {
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('content').clear();
|
2015-10-07 17:44:23 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_removeItems(status, key) {
|
2015-10-07 17:44:23 +03:00
|
|
|
if (key) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let keyBase = this._getKeyBase(key);
|
|
|
|
// TODO: keys should only have . special char but we should
|
|
|
|
// probably use a better regexp escaping function/polyfill
|
|
|
|
let escapedKeyBase = keyBase.replace('.', '\\.');
|
|
|
|
let keyRegex = new RegExp(`^${escapedKeyBase}`);
|
2015-10-07 17:44:23 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('content', this.get('content').reject((item) => {
|
|
|
|
let itemKey = get(item, 'key');
|
|
|
|
let itemStatus = get(item, 'status');
|
2015-10-07 17:44:23 +03:00
|
|
|
|
|
|
|
return itemStatus === status && (itemKey && itemKey.match(keyRegex));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
this.set('content', this.get('content').rejectBy('status', status));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// take a key and return the first two elements, eg:
|
|
|
|
// "invite.revoke.failed" => "invite.revoke"
|
2015-10-28 14:36:45 +03:00
|
|
|
_getKeyBase(key) {
|
2015-10-07 17:44:23 +03:00
|
|
|
return key.split('.').slice(0, 2).join('.');
|
2014-03-22 16:08:15 +04:00
|
|
|
}
|
|
|
|
});
|