2021-11-08 15:34:23 +03:00
|
|
|
import * as Sentry from '@sentry/browser';
|
2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-05-25 14:16:31 +03:00
|
|
|
import {TrackedArray} from 'tracked-built-ins';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {dasherize} from '@ember/string';
|
|
|
|
import {htmlSafe} from '@ember/template';
|
2022-05-25 14:16:31 +03:00
|
|
|
import {isArray} from '@ember/array';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2016-07-08 16:54:36 +03:00
|
|
|
import {
|
|
|
|
isMaintenanceError,
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2022-05-25 14:16:31 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
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
|
|
|
|
|
2022-05-27 13:52:39 +03:00
|
|
|
// Rather than showing raw JS error messages to users we want to show a generic one.
|
|
|
|
// This list is used to check obj.name in `showApiError(obj)` as the first line
|
|
|
|
// of defence, then at the lowest `handleNotification(msg)` level we check the
|
|
|
|
// first word of the message text as a fallback in case we get JS error messages
|
|
|
|
// from the API. If there's a match we show the generic message.
|
|
|
|
const GENERIC_ERROR_NAMES = [
|
|
|
|
'AggregateError',
|
|
|
|
'EvalError',
|
|
|
|
'RangeError',
|
|
|
|
'ReferenceError',
|
|
|
|
'SyntaxError',
|
|
|
|
'TypeError',
|
|
|
|
'URIError'
|
|
|
|
];
|
|
|
|
|
|
|
|
export const GENERIC_ERROR_MESSAGE = 'An unexpected error occurred, please try again.';
|
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
export default class NotificationsService extends Service {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service config;
|
|
|
|
@service upgradeStatus;
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
@tracked delayedNotifications = new TrackedArray([]);
|
|
|
|
@tracked content = new TrackedArray([]);
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
get alerts() {
|
|
|
|
return this.content.filter(n => n.status === 'alert');
|
|
|
|
}
|
|
|
|
|
|
|
|
get notifications() {
|
|
|
|
return this.content.filter(n => n.status === 'notification');
|
|
|
|
}
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2022-05-27 13:52:39 +03:00
|
|
|
handleNotification(message, delayed = false) {
|
|
|
|
const wordRegex = /[a-z]+/igm;
|
|
|
|
const wordMatches = (message.message.string || message.message).matchAll(wordRegex);
|
|
|
|
|
|
|
|
for (const wordMatch of wordMatches) {
|
|
|
|
if (GENERIC_ERROR_NAMES.includes(wordMatch[0])) {
|
|
|
|
message.message = GENERIC_ERROR_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
// If this is an alert message from the server, treat it as html safe
|
2022-05-25 14:16:31 +03:00
|
|
|
if (message.constructor.modelName === 'notification' && message.status === 'alert') {
|
|
|
|
message.message = htmlSafe(message.message);
|
2014-07-09 08:17:30 +04:00
|
|
|
}
|
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
if (!message.status) {
|
|
|
|
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
|
2022-05-25 14:16:31 +03:00
|
|
|
if (message.key) {
|
|
|
|
this._removeItems(message.status, message.key);
|
2015-10-07 17:44:23 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:50:45 +03:00
|
|
|
// close existing alerts/notifications which have the same text to avoid stacking
|
2022-05-25 14:16:31 +03:00
|
|
|
let newText = message.message.string || message.message;
|
|
|
|
this.content = new TrackedArray(this.content.reject((notification) => {
|
|
|
|
let existingText = notification.message.string || notification.message;
|
2019-03-12 20:50:45 +03:00
|
|
|
return existingText === newText;
|
|
|
|
}));
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
if (!delayed) {
|
2022-05-25 14:16:31 +03:00
|
|
|
this.content.push(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
} else {
|
2022-05-25 14:16:31 +03:00
|
|
|
this.delayedNotifications.push(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
showAlert(message, options = {}) {
|
2014-08-01 09:40:49 +04:00
|
|
|
options = options || {};
|
|
|
|
|
2021-11-08 15:34:23 +03:00
|
|
|
if (!options.isApiError) {
|
|
|
|
if (this.config.get('sentry_dsn')) {
|
|
|
|
// message could be a htmlSafe object rather than a string
|
2022-05-25 14:16:31 +03:00
|
|
|
const displayedMessage = message.string || message;
|
2021-11-08 15:34:23 +03:00
|
|
|
|
|
|
|
const contexts = {
|
|
|
|
ghost: {
|
|
|
|
displayed_message: displayedMessage,
|
2022-05-25 14:16:31 +03:00
|
|
|
ghost_error_code: options.ghostErrorCode,
|
2021-11-08 15:34:23 +03:00
|
|
|
full_error: message,
|
|
|
|
source: 'showAlert'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Sentry.captureException(displayedMessage, {
|
|
|
|
contexts,
|
|
|
|
tags: {
|
|
|
|
shown_to_user: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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',
|
2020-02-27 12:19:29 +03:00
|
|
|
description: options.description,
|
|
|
|
icon: options.icon,
|
2015-10-07 17:44:23 +03:00
|
|
|
type: options.type,
|
2020-02-27 12:19:29 +03:00
|
|
|
key: options.key,
|
|
|
|
actions: options.actions
|
2015-06-19 00:56:18 +03:00
|
|
|
}, options.delayed);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-06-19 00:56:18 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
showNotification(message, options) {
|
2015-06-19 00:56:18 +03:00
|
|
|
options = options || {};
|
|
|
|
|
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',
|
2020-02-27 12:19:29 +03:00
|
|
|
description: options.description,
|
|
|
|
icon: options.icon,
|
2015-10-07 17:44:23 +03:00
|
|
|
type: options.type,
|
2020-02-27 12:19:29 +03:00
|
|
|
key: options.key,
|
|
|
|
actions: options.actions
|
2014-08-01 09:40:49 +04:00
|
|
|
}, options.delayed);
|
2022-02-03 01:11:11 +03: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)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.upgradeStatus.requireUpgrade();
|
2016-07-08 16:54:36 +03:00
|
|
|
} else if (isMaintenanceError(resp)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.upgradeStatus.maintenanceAlert();
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 19:59:04 +03:00
|
|
|
// loop over ember-ajax errors object
|
2022-05-27 13:52:39 +03:00
|
|
|
if (isArray(resp?.payload?.errors)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return resp.payload.errors.forEach((error) => {
|
2016-06-30 17:45:02 +03:00
|
|
|
this._showAPIError(error, options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._showAPIError(resp, options);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
|
|
|
_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
|
2022-05-25 14:16:31 +03:00
|
|
|
if (!options.key && !isBlank(resp?.title)) {
|
|
|
|
options.key = dasherize(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
|
|
|
|
2022-05-27 13:52:39 +03:00
|
|
|
if (resp?.name && GENERIC_ERROR_NAMES.includes(resp.name)) {
|
|
|
|
msg = GENERIC_ERROR_MESSAGE;
|
|
|
|
} else if (resp instanceof String) {
|
2016-06-14 14:46:24 +03:00
|
|
|
msg = resp;
|
2022-05-25 14:16:31 +03:00
|
|
|
} else if (!isBlank(resp?.detail)) {
|
2016-06-14 14:46:24 +03:00
|
|
|
msg = resp.detail;
|
2022-05-25 14:16:31 +03:00
|
|
|
} else if (!isBlank(resp?.message)) {
|
2016-06-14 14:46:24 +03:00
|
|
|
msg = resp.message;
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
if (!isBlank(resp?.context)) {
|
|
|
|
msg = `${msg} ${resp.context}`;
|
2019-03-06 14:45:47 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:34:23 +03:00
|
|
|
if (this.config.get('sentry_dsn')) {
|
|
|
|
const reportedError = resp instanceof Error ? resp : msg;
|
|
|
|
|
|
|
|
Sentry.captureException(reportedError, {
|
|
|
|
contexts: {
|
|
|
|
ghost: {
|
2022-05-25 14:16:31 +03:00
|
|
|
ghost_error_code: resp.ghostErrorCode,
|
2021-11-08 15:34:23 +03:00
|
|
|
displayed_message: msg,
|
|
|
|
full_error: resp,
|
|
|
|
source: 'showAPIError'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
shown_to_user: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
options.isApiError = true;
|
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
this.showAlert(msg, options);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
displayDelayed() {
|
|
|
|
this.delayedNotifications.forEach((message) => {
|
2022-05-25 14:16:31 +03:00
|
|
|
this.content.push(message);
|
2014-03-22 16:08:15 +04:00
|
|
|
});
|
2022-05-25 14:16:31 +03:00
|
|
|
this.delayedNotifications = new TrackedArray([]);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
closeNotification(notification) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let content = this.content;
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2020-01-07 16:23:15 +03:00
|
|
|
if (notification.constructor.modelName === 'notification') {
|
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
|
|
|
}
|
2022-02-03 01:11:11 +03: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);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-10-07 17:44:23 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
closeAlerts(key) {
|
2015-10-07 17:44:23 +03:00
|
|
|
this._removeItems('alert', key);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
clearAll() {
|
2022-05-25 14:16:31 +03:00
|
|
|
this.content = new TrackedArray([]);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
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
|
|
|
|
2022-05-25 14:16:31 +03:00
|
|
|
this.content = new TrackedArray(this.content.reject((item) => {
|
|
|
|
let itemKey = item.key;
|
|
|
|
let itemStatus = item.status;
|
2015-10-07 17:44:23 +03:00
|
|
|
|
|
|
|
return itemStatus === status && (itemKey && itemKey.match(keyRegex));
|
|
|
|
}));
|
|
|
|
} else {
|
2022-05-25 14:16:31 +03:00
|
|
|
this.content = new TrackedArray(this.content.rejectBy('status', status));
|
2015-10-07 17:44:23 +03:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2015-10-07 17:44:23 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|