2017-05-29 21:50:03 +03:00
|
|
|
import AuthConfiguration from 'ember-simple-auth/configuration';
|
|
|
|
import RSVP from 'rsvp';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Route from '@ember/routing/route';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ShortcutsRoute from 'ghost-admin/mixins/shortcuts-route';
|
|
|
|
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
|
|
|
|
import windowProxy from 'ghost-admin/utils/window-proxy';
|
2021-05-26 19:01:18 +03:00
|
|
|
import {InitSentryForEmber} from '@sentry/ember';
|
|
|
|
import {configureScope} from '@sentry/browser';
|
2017-11-04 01:59:39 +03:00
|
|
|
import {
|
|
|
|
isAjaxError,
|
|
|
|
isNotFoundError,
|
|
|
|
isUnauthorizedError
|
|
|
|
} from 'ember-ajax/errors';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2017-11-04 01:59:39 +03:00
|
|
|
import {
|
|
|
|
isMaintenanceError,
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2014-06-19 23:44:44 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
function K() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shortcuts = {};
|
2014-11-26 01:34:55 +03:00
|
|
|
|
2017-10-31 12:10:49 +03:00
|
|
|
shortcuts.esc = {action: 'closeMenus', scope: 'default'};
|
2015-10-28 14:36:45 +03:00
|
|
|
shortcuts[`${ctrlOrCmd}+s`] = {action: 'save', scope: 'all'};
|
2014-11-26 01:34:55 +03:00
|
|
|
|
2021-04-12 15:21:57 +03:00
|
|
|
export default Route.extend(ShortcutsRoute, {
|
2018-04-16 19:55:21 +03:00
|
|
|
ajax: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
feature: service(),
|
2018-04-16 19:55:21 +03:00
|
|
|
ghostPaths: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
2018-04-30 15:07:37 +03:00
|
|
|
router: service(),
|
2021-04-12 15:21:57 +03:00
|
|
|
session: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
settings: service(),
|
|
|
|
ui: service(),
|
2019-08-23 12:01:27 +03:00
|
|
|
whatsNew: service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
shortcuts,
|
|
|
|
|
2019-03-21 12:33:14 +03:00
|
|
|
routeAfterAuthentication: 'home',
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2019-03-06 20:15:44 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.router.on('routeDidChange', () => {
|
|
|
|
this.notifications.displayDelayed();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-28 16:07:50 +03:00
|
|
|
beforeModel() {
|
2021-05-26 19:01:18 +03:00
|
|
|
return this.config.fetchUnauthenticated()
|
|
|
|
.then(() => {
|
|
|
|
// init Sentry here rather than app.js so that we can use API-supplied
|
|
|
|
// sentry_dsn and sentry_env rather than building it into release assets
|
|
|
|
if (this.config.get('sentry_dsn')) {
|
|
|
|
InitSentryForEmber({
|
|
|
|
dsn: this.config.get('sentry_dsn'),
|
|
|
|
environment: this.config.get('sentry_env'),
|
|
|
|
release: `ghost@${this.config.get('version')}`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-10-28 16:07:50 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
afterModel(model, transition) {
|
2016-05-05 17:03:09 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-18 21:17:02 +03:00
|
|
|
if (this.get('session.isAuthenticated')) {
|
2021-04-12 15:21:57 +03:00
|
|
|
this.session.appLoadTransition = transition;
|
|
|
|
this.session.loadServerNotifications();
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
// return the feature/settings load promises so that we block until
|
|
|
|
// they are loaded to enable synchronous access everywhere
|
|
|
|
return RSVP.all([
|
2019-08-12 11:11:10 +03:00
|
|
|
this.config.fetchAuthenticated(),
|
|
|
|
this.feature.fetch(),
|
2021-03-02 17:29:26 +03:00
|
|
|
this.settings.fetch()
|
2018-01-12 19:11:46 +03:00
|
|
|
]).then((results) => {
|
|
|
|
this._appLoaded = true;
|
2019-08-23 12:01:27 +03:00
|
|
|
|
2021-05-26 19:01:18 +03:00
|
|
|
// update Sentry with the full Ghost version which we only get after authentication
|
|
|
|
if (this.config.get('sentry_dsn')) {
|
|
|
|
configureScope((scope) => {
|
|
|
|
scope.addEventProcessor((event) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
resolve({
|
|
|
|
...event,
|
|
|
|
release: `ghost@${this.config.get('version')}`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:01:27 +03:00
|
|
|
// kick off background update of "whats new"
|
|
|
|
// - we don't want to block the router for this
|
|
|
|
// - we need the user details to know what the user has seen
|
|
|
|
this.whatsNew.fetchLatest.perform();
|
|
|
|
|
2018-01-12 19:11:46 +03:00
|
|
|
return results;
|
|
|
|
});
|
2014-08-06 09:01:00 +04:00
|
|
|
}
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
this._appLoaded = true;
|
2014-08-06 09:01:00 +04:00
|
|
|
},
|
|
|
|
|
2014-03-31 08:07:05 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
closeMenus() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.ui.closeMenus();
|
2014-06-19 23:44:44 +04:00
|
|
|
},
|
2014-06-24 06:52:38 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didTransition() {
|
2021-04-12 15:21:57 +03:00
|
|
|
this.session.appLoadTransition = null;
|
2015-08-20 17:44:52 +03:00
|
|
|
this.send('closeMenus');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
authorizationFailed() {
|
2019-01-08 15:36:16 +03:00
|
|
|
windowProxy.replaceLocation(AuthConfiguration.rootURL);
|
2015-11-04 18:20:11 +03:00
|
|
|
},
|
|
|
|
|
2014-11-26 01:34:55 +03:00
|
|
|
// noop default for unhandled save (used from shortcuts)
|
2016-06-30 17:45:02 +03:00
|
|
|
save: K,
|
|
|
|
|
|
|
|
error(error, transition) {
|
2017-03-10 20:36:48 +03:00
|
|
|
// unauthoirized errors are already handled in the ajax service
|
|
|
|
if (isUnauthorizedError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
if (isNotFoundError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2019-03-07 20:24:35 +03:00
|
|
|
let routeInfo = transition.to;
|
2019-03-06 16:53:54 +03:00
|
|
|
let router = this.router;
|
2017-11-04 01:59:39 +03:00
|
|
|
let params = [];
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
for (let key of Object.keys(routeInfo.params)) {
|
|
|
|
params.push(routeInfo.params[key]);
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2018-04-30 14:09:30 +03:00
|
|
|
let url = router.urlFor(routeInfo.name, ...params)
|
|
|
|
.replace(/^#\//, '')
|
|
|
|
.replace(/^\//, '')
|
|
|
|
.replace(/^ghost\//, '');
|
|
|
|
|
|
|
|
return this.replaceWith('error404', url);
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
2016-11-14 16:16:51 +03:00
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.upgradeStatus.requireUpgrade();
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
if (this._appLoaded) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isMaintenanceError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
2016-11-14 16:16:51 +03:00
|
|
|
}
|
2016-07-08 16:54:36 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.upgradeStatus.maintenanceAlert();
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
if (this._appLoaded) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isAjaxError(error) || error && error.payload && isEmberArray(error.payload.errors)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2017-11-04 01:59:39 +03:00
|
|
|
// don't show the 500 page if we weren't navigating
|
|
|
|
if (!transition) {
|
2016-11-14 16:16:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to 500 error page
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-31 08:07:05 +04:00
|
|
|
}
|
|
|
|
});
|