2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-10-31 18:27:25 +03:00
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {empty} from '@ember/object/computed';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
|
|
|
notifications: service(),
|
|
|
|
settings: service(),
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
leaveSettingsTransition: null,
|
|
|
|
slackArray: null,
|
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.slackArray = [];
|
|
|
|
},
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
slackSettings: boundOneWay('settings.slack.firstObject'),
|
|
|
|
testNotificationDisabled: empty('slackSettings.url'),
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
actions: {
|
|
|
|
save() {
|
2017-05-18 13:48:37 +03:00
|
|
|
this.get('save').perform();
|
2017-03-08 16:55:35 +03:00
|
|
|
},
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
updateURL(value) {
|
2018-01-08 13:31:53 +03:00
|
|
|
value = typeof value === 'string' ? value.trim() : value;
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('slackSettings.url', value);
|
|
|
|
this.get('slackSettings.errors').clear();
|
2017-10-31 18:27:25 +03:00
|
|
|
},
|
|
|
|
|
2019-01-21 15:26:21 +03:00
|
|
|
updateUsername(value) {
|
|
|
|
value = typeof value === 'string' ? value.trimLeft() : value;
|
|
|
|
this.set('slackSettings.username', value);
|
|
|
|
this.get('slackSettings.errors').clear();
|
|
|
|
},
|
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
triggerDirtyState() {
|
2018-01-11 01:57:43 +03:00
|
|
|
let slack = this.get('slackSettings');
|
2017-10-31 18:27:25 +03:00
|
|
|
let slackArray = this.get('slackArray');
|
|
|
|
let settings = this.get('settings');
|
|
|
|
|
|
|
|
// Hack to trigger the `isDirty` state on the settings model by setting a new Array
|
|
|
|
// for slack rather that replacing the existing one which would still point to the
|
|
|
|
// same reference and therfore not setting the model into a dirty state
|
|
|
|
slackArray.clear().pushObject(slack);
|
|
|
|
settings.set('slack', slackArray);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
|
|
|
let leaveTransition = this.get('leaveSettingsTransition');
|
|
|
|
|
|
|
|
if (!transition && this.get('showLeaveSettingsModal')) {
|
|
|
|
this.set('leaveSettingsTransition', null);
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveSettingsTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.get('save.isRunning')) {
|
|
|
|
return this.get('save.last').then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveSettings() {
|
|
|
|
let transition = this.get('leaveSettingsTransition');
|
|
|
|
let settings = this.get('settings');
|
|
|
|
let slackArray = this.get('slackArray');
|
|
|
|
|
|
|
|
if (!transition) {
|
|
|
|
this.get('notifications').showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// roll back changes on model props
|
|
|
|
settings.rollbackAttributes();
|
|
|
|
slackArray.clear();
|
|
|
|
|
|
|
|
return transition.retry();
|
2016-03-29 11:40:44 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
save: task(function* () {
|
|
|
|
let slack = this.get('slackSettings');
|
|
|
|
let settings = this.get('settings');
|
|
|
|
let slackArray = this.get('slackArray');
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield slack.validate();
|
|
|
|
// clear existing objects in slackArray to make sure we only push the validated one
|
|
|
|
slackArray.clear().pushObject(slack);
|
|
|
|
yield settings.set('slack', slackArray);
|
|
|
|
return yield settings.save();
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
sendTestNotification: task(function* () {
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
let slackApi = this.get('ghostPaths.url').api('slack', 'test');
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield this.get('save').perform();
|
|
|
|
yield this.get('ajax').post(slackApi);
|
|
|
|
notifications.showNotification('Check your Slack channel for the test message!', {type: 'info', key: 'slack-test.send.success'});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'slack-test:send'});
|
|
|
|
|
|
|
|
if (!isInvalidError(error)) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop()
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|