2021-10-18 15:46:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
2022-09-08 11:18:48 +03:00
|
|
|
import DeleteIntegrationModal from '../../components/settings/integrations/delete-integration-modal';
|
2022-09-08 11:18:56 +03:00
|
|
|
import DeleteWebhookModal from '../../components/settings/integrations/delete-webhook-modal';
|
2022-11-14 12:41:09 +03:00
|
|
|
import RegenerateKeyModal from '../../components/settings/integrations/regenerate-key-modal';
|
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-21 20:54:33 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-10-18 15:46:29 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2021-10-18 15:46:29 +03:00
|
|
|
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 ghostPaths;
|
2022-09-08 11:18:48 +03:00
|
|
|
@service modals;
|
2022-02-01 12:34:03 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
imageExtensions = IMAGE_EXTENSIONS;
|
|
|
|
imageMimeTypes = IMAGE_MIME_TYPES;
|
2021-10-18 15:46:29 +03:00
|
|
|
|
2022-11-14 12:41:09 +03:00
|
|
|
@tracked regeneratedApiKey = null;
|
2022-02-21 20:54:33 +03:00
|
|
|
|
|
|
|
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 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
|
2022-02-21 20:54:33 +03:00
|
|
|
confirmIntegrationDeletion(event) {
|
|
|
|
event?.preventDefault();
|
2022-09-08 11:18:48 +03:00
|
|
|
return this.modals.open(DeleteIntegrationModal, {
|
|
|
|
integration: this.integration
|
|
|
|
});
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-11-14 12:41:09 +03:00
|
|
|
async confirmRegenerateKey(apiKey, event) {
|
2022-02-21 20:54:33 +03:00
|
|
|
event?.preventDefault();
|
2022-11-14 12:41:09 +03:00
|
|
|
this.regeneratedApiKey = null;
|
|
|
|
this.regeneratedApiKey = await this.modals.open(RegenerateKeyModal, {
|
|
|
|
apiKey,
|
|
|
|
integration: this.integration
|
|
|
|
});
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-21 20:54:33 +03:00
|
|
|
confirmWebhookDeletion(webhook, event) {
|
|
|
|
event?.preventDefault();
|
2022-09-08 11:18:56 +03:00
|
|
|
return this.modals.open(DeleteWebhookModal, {webhook});
|
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
|
|
|
}
|