2021-10-18 15:46:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
|
|
|
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-21 20:54:33 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-10-18 15:46:29 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2022-02-21 20:54:33 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
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;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@tracked showDeleteIntegrationModal = false;
|
|
|
|
@tracked showRegenerateKeyModal = false;
|
|
|
|
@tracked showUnsavedChangesModal = false;
|
|
|
|
@tracked selectedApiKey = null;
|
|
|
|
@tracked isApiKeyRegenerated = false;
|
|
|
|
@tracked webhookToDelete;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...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-21 20:54:33 +03:00
|
|
|
get integration() {
|
|
|
|
return this.model;
|
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
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
|
|
|
get regeneratedKeyType() {
|
2021-10-18 15:46:29 +03:00
|
|
|
if (this.isApiKeyRegenerated) {
|
2022-02-21 20:54:33 +03:00
|
|
|
return this.selectedApiKey.type;
|
2021-10-18 15:46:29 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
2022-02-21 20:54:33 +03:00
|
|
|
triggerIconFileDialog(event) {
|
|
|
|
event.preventDefault();
|
2022-02-01 12:34:03 +03:00
|
|
|
let input = document.querySelector('input[type="file"][name="iconImage"]');
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@action
|
|
|
|
updateProperty(property, event) {
|
|
|
|
this.integration.set(property, event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
validateProperty(property) {
|
|
|
|
this.integration.validate({property});
|
|
|
|
}
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@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) {
|
2022-02-21 20:54:33 +03:00
|
|
|
this.leaveScreenTransition = null;
|
|
|
|
this.showUnsavedChangesModal = false;
|
2022-02-01 12:34:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
2022-02-21 20:54:33 +03:00
|
|
|
this.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
|
2022-02-21 20:54:33 +03:00
|
|
|
this.showUnsavedChangesModal = true;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
leaveScreen(event) {
|
|
|
|
event?.preventDefault();
|
2022-02-01 12:34:03 +03:00
|
|
|
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
|
2022-02-21 20:54:33 +03:00
|
|
|
deleteIntegration(event) {
|
|
|
|
event?.preventDefault();
|
2022-02-01 12:34:03 +03:00
|
|
|
this.integration.destroyRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
confirmIntegrationDeletion(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.showDeleteIntegrationModal = true;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
cancelIntegrationDeletion(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.showDeleteIntegrationModal = false;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
confirmRegenerateKeyModal(apiKey, event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.showRegenerateKeyModal = true;
|
|
|
|
this.isApiKeyRegenerated = false;
|
|
|
|
this.selectedApiKey = apiKey;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
cancelRegenerateKeyModal(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.showRegenerateKeyModal = false;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
regenerateKey(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.isApiKeyRegenerated = true;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
confirmWebhookDeletion(webhook, event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.webhookToDelete = webhook;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
cancelWebhookDeletion(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.webhookToDelete = null;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
deleteWebhook(event) {
|
|
|
|
event?.preventDefault();
|
2022-02-01 12:34:03 +03:00
|
|
|
return this.webhookToDelete.destroyRecord();
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@task
|
|
|
|
*saveTask() {
|
2022-02-21 20:25:12 +03:00
|
|
|
try {
|
|
|
|
return yield this.integration.save();
|
|
|
|
} catch (e) {
|
|
|
|
if (e === undefined) {
|
|
|
|
// validation error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
2022-02-21 20:54:33 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@task
|
|
|
|
*copyContentKey() {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.integration.contentKey.secret);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2022-02-21 20:54:33 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@task
|
|
|
|
*copyAdminKey() {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.integration.adminKey.secret);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2022-02-21 20:54:33 +03:00
|
|
|
}
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
@task
|
|
|
|
*copyApiUrl() {
|
2021-10-18 15:46:29 +03:00
|
|
|
copyTextToClipboard(this.apiUrl);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2022-02-21 20:54:33 +03:00
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|