2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
|
|
|
import {alias} from '@ember/object/computed';
|
|
|
|
import {empty} from '@ember/object/computed';
|
|
|
|
import {inject as injectService} from '@ember/service';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
|
|
|
import {task} from 'ember-concurrency';
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2016-06-30 13:21:47 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
ajax: injectService(),
|
|
|
|
notifications: injectService(),
|
2017-03-17 20:16:21 +03:00
|
|
|
settings: injectService(),
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
model: alias('settings.slack.firstObject'),
|
2016-03-29 11:40:44 +03:00
|
|
|
testNotificationDisabled: empty('model.url'),
|
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
save: task(function* () {
|
|
|
|
let slack = this.get('model');
|
|
|
|
let settings = this.get('settings');
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield slack.validate();
|
|
|
|
settings.get('slack').clear().pushObject(slack);
|
|
|
|
return yield settings.save();
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
throw error;
|
2016-03-29 11:40:44 +03:00
|
|
|
}
|
2017-03-08 16:55:35 +03:00
|
|
|
}
|
|
|
|
}).drop(),
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
sendTestNotification: task(function* () {
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
let slackApi = this.get('ghostPaths.url').api('slack', 'test');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
try {
|
|
|
|
yield this.get('save').perform();
|
|
|
|
yield this.get('ajax').post(slackApi);
|
2017-07-31 10:49:49 +03:00
|
|
|
notifications.showNotification('Check your Slack channel for the test message!', {type: 'info', key: 'slack-test.send.success'});
|
2017-03-08 16:55:35 +03:00
|
|
|
return true;
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
} catch (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'slack-test:send'});
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
if (!isInvalidError(error)) {
|
|
|
|
throw error;
|
2016-03-29 11:40:44 +03:00
|
|
|
}
|
2017-03-08 16:55:35 +03:00
|
|
|
}
|
|
|
|
}).drop(),
|
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) {
|
|
|
|
this.set('model.url', value);
|
|
|
|
this.get('model.errors').clear();
|
2016-03-29 11:40:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|