2021-10-18 15:46:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
2022-02-01 12:34:03 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2021-10-18 15:46:29 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
|
|
|
import copyTextToClipboard from 'ghost-admin/utils/copy-text-to-clipboard';
|
|
|
|
import {
|
|
|
|
IMAGE_EXTENSIONS,
|
|
|
|
IMAGE_MIME_TYPES
|
|
|
|
} from 'ghost-admin/components/gh-image-uploader';
|
2022-02-01 12:34:03 +03:00
|
|
|
import {action, computed} from '@ember/object';
|
2021-10-18 15:46:29 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
|
|
|
import {htmlSafe} from '@ember/template';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@classic
|
|
|
|
export default class IntegrationController extends Controller {
|
2022-02-01 20:03:45 +03:00
|
|
|
@service config;
|
|
|
|
@service ghostPaths;
|
2022-02-01 12:34:03 +03:00
|
|
|
|
|
|
|
imageExtensions = IMAGE_EXTENSIONS;
|
|
|
|
imageMimeTypes = IMAGE_MIME_TYPES;
|
|
|
|
showRegenerateKeyModal = false;
|
|
|
|
selectedApiKey = null;
|
|
|
|
isApiKeyRegenerated = false;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
|
|
|
init() {
|
2022-02-01 12:34:03 +03:00
|
|
|
super.init(...arguments);
|
2021-10-18 15:46:29 +03:00
|
|
|
if (this.isTesting === undefined) {
|
|
|
|
this.isTesting = config.environment === 'test';
|
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@alias('model')
|
2022-02-10 13:41:36 +03:00
|
|
|
integration;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed
|
|
|
|
get apiUrl() {
|
2021-10-18 15:46:29 +03:00
|
|
|
let origin = window.location.origin;
|
|
|
|
let subdir = this.ghostPaths.subdir;
|
|
|
|
let url = this.ghostPaths.url.join(origin, subdir);
|
|
|
|
|
|
|
|
return url.replace(/\/$/, '');
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed('isApiKeyRegenerated', 'selectedApiKey')
|
|
|
|
get regeneratedKeyType() {
|
2021-10-18 15:46:29 +03:00
|
|
|
if (this.isApiKeyRegenerated) {
|
|
|
|
return this.get('selectedApiKey.type');
|
|
|
|
}
|
|
|
|
return null;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed
|
|
|
|
get allWebhooks() {
|
2021-10-18 15:46:29 +03:00
|
|
|
return this.store.peekAll('webhook');
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed('integration.id', 'allWebhooks.@each.{isNew,isDeleted}')
|
|
|
|
get filteredWebhooks() {
|
2021-10-18 15:46:29 +03:00
|
|
|
return this.allWebhooks.filter((webhook) => {
|
|
|
|
let matchesIntegration = webhook.belongsTo('integration').id() === this.integration.id;
|
|
|
|
|
|
|
|
return matchesIntegration
|
|
|
|
&& !webhook.isNew
|
|
|
|
&& !webhook.isDeleted;
|
|
|
|
});
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed('integration.iconImage')
|
|
|
|
get iconImageStyle() {
|
2021-10-18 15:46:29 +03:00
|
|
|
let url = this.integration.iconImage;
|
|
|
|
if (url) {
|
|
|
|
let styles = [
|
|
|
|
`background-image: url(${url})`,
|
|
|
|
'background-size: 50%',
|
|
|
|
'background-position: 50%',
|
|
|
|
'background-repeat: no-repeat'
|
|
|
|
];
|
|
|
|
return htmlSafe(styles.join('; '));
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlSafe('');
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
triggerIconFileDialog() {
|
|
|
|
let input = document.querySelector('input[type="file"][name="iconImage"]');
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setIconImage([image]) {
|
|
|
|
this.integration.set('iconImage', image.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
save() {
|
2022-02-10 13:20:03 +03:00
|
|
|
return this.saveTask.perform();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.set('leaveScreenTransition', null);
|
|
|
|
this.set('showUnsavedChangesModal', false);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveScreenTransition', transition);
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
// if a save is running, wait for it to finish then transition
|
2022-02-10 13:20:03 +03:00
|
|
|
if (this.saveTask.isRunning) {
|
|
|
|
return this.saveTask.last.then(() => {
|
2022-02-01 12:34:03 +03:00
|
|
|
transition.retry();
|
|
|
|
});
|
2021-10-18 15:46:29 +03:00
|
|
|
}
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showUnsavedChangesModal', true);
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@action
|
|
|
|
leaveScreen() {
|
|
|
|
let transition = this.leaveScreenTransition;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
if (!transition) {
|
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
2021-10-18 15:46:29 +03:00
|
|
|
}
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
// roll back changes on model props
|
|
|
|
this.integration.rollbackAttributes();
|
|
|
|
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
deleteIntegration() {
|
|
|
|
this.integration.destroyRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
confirmIntegrationDeletion() {
|
|
|
|
this.set('showDeleteIntegrationModal', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelIntegrationDeletion() {
|
|
|
|
this.set('showDeleteIntegrationModal', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
confirmRegenerateKeyModal(apiKey) {
|
|
|
|
this.set('showRegenerateKeyModal', true);
|
|
|
|
this.set('isApiKeyRegenerated', false);
|
|
|
|
this.set('selectedApiKey', apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelRegenerateKeyModal() {
|
|
|
|
this.set('showRegenerateKeyModal', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
regenerateKey() {
|
|
|
|
this.set('isApiKeyRegenerated', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
confirmWebhookDeletion(webhook) {
|
|
|
|
this.set('webhookToDelete', webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelWebhookDeletion() {
|
|
|
|
this.set('webhookToDelete', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
deleteWebhook() {
|
|
|
|
return this.webhookToDelete.destroyRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
@task(function* () {
|
2021-10-18 15:46:29 +03:00
|
|
|
return yield this.integration.save();
|
2022-02-01 12:34:03 +03:00
|
|
|
})
|
2022-02-10 13:41:36 +03:00
|
|
|
saveTask;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@task(function* () {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.integration.contentKey.secret);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2022-02-01 12:34:03 +03:00
|
|
|
})
|
2022-02-10 13:41:36 +03:00
|
|
|
copyContentKey;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@task(function* () {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.integration.adminKey.secret);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2022-02-01 12:34:03 +03:00
|
|
|
})
|
2022-02-10 13:41:36 +03:00
|
|
|
copyAdminKey;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@task(function* () {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.apiUrl);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
|
|
|
})
|
2022-02-10 13:41:36 +03:00
|
|
|
copyApiUrl;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|