2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'webhooks',
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
2020-09-14 13:33:37 +03:00
|
|
|
headers: {
|
|
|
|
// NOTE: remove if there is ever a 'read' method
|
|
|
|
location: false
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
options: [],
|
|
|
|
data: [],
|
|
|
|
permissions: true,
|
2020-09-24 04:55:25 +03:00
|
|
|
async query(frame) {
|
|
|
|
const isIntegrationRequest = frame.options.context && frame.options.context.integration && frame.options.context.integration.id;
|
|
|
|
|
|
|
|
// NOTE: this check can be removed once `webhooks.integration_id` gets foreigh ke constraint (Ghost 4.0)
|
|
|
|
if (!isIntegrationRequest && frame.data.webhooks[0].integration_id) {
|
|
|
|
const integration = await models.Integration.findOne({id: frame.data.webhooks[0].integration_id}, {context: {internal: true}});
|
|
|
|
|
|
|
|
if (!integration) {
|
|
|
|
throw new errors.ValidationError({
|
|
|
|
message: i18n.t('notices.data.validation.index.schemaValidationFailed', {
|
|
|
|
key: 'integration_id'
|
|
|
|
}),
|
|
|
|
context: i18n.t('errors.api.webhooks.nonExistingIntegrationIdProvided.context'),
|
|
|
|
help: i18n.t('errors.api.webhooks.nonExistingIntegrationIdProvided.help')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhook = await models.Webhook.getByEventAndTarget(
|
2019-08-09 17:11:24 +03:00
|
|
|
frame.data.webhooks[0].event,
|
|
|
|
frame.data.webhooks[0].target_url,
|
|
|
|
frame.options
|
2020-09-24 04:55:25 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (webhook) {
|
|
|
|
throw new errors.ValidationError({message: i18n.t('errors.api.webhooks.webhookAlreadyExists')});
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2020-09-24 04:55:25 +03:00
|
|
|
return models.Webhook.add(frame.data.webhooks[0], frame.options);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
2020-07-08 07:54:31 +03:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
2020-08-04 07:43:24 +03:00
|
|
|
if (frame.options.context && frame.options.context.integration && frame.options.context.integration.id) {
|
2020-07-08 07:54:31 +03:00
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
2020-08-03 14:08:47 +03:00
|
|
|
if (!webhook) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:43:24 +03:00
|
|
|
if (webhook.get('integration_id') !== frame.options.context.integration.id) {
|
2020-07-08 07:54:31 +03:00
|
|
|
throw new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.webhooks.noPermissionToEdit.message', {
|
|
|
|
method: 'edit'
|
|
|
|
}),
|
|
|
|
context: i18n.t('errors.api.webhooks.noPermissionToEdit.context', {
|
|
|
|
method: 'edit'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
data: [
|
|
|
|
'name',
|
|
|
|
'event',
|
|
|
|
'target_url',
|
|
|
|
'secret',
|
|
|
|
'api_version'
|
|
|
|
],
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query({data, options}) {
|
|
|
|
return models.Webhook.edit(data.webhooks[0], Object.assign(options, {require: true}))
|
|
|
|
.catch(models.Webhook.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
2019-08-09 17:11:24 +03:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-07-08 07:54:31 +03:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
2020-08-04 07:43:24 +03:00
|
|
|
if (frame.options.context && frame.options.context.integration && frame.options.context.integration.id) {
|
2020-07-08 07:54:31 +03:00
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
2020-08-03 14:08:47 +03:00
|
|
|
if (!webhook) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:43:24 +03:00
|
|
|
if (webhook.get('integration_id') !== frame.options.context.integration.id) {
|
2020-07-08 07:54:31 +03:00
|
|
|
throw new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.webhooks.noPermissionToEdit.message', {
|
2020-07-20 11:05:56 +03:00
|
|
|
method: 'destroy'
|
2020-07-08 07:54:31 +03:00
|
|
|
}),
|
|
|
|
context: i18n.t('errors.api.webhooks.noPermissionToEdit.context', {
|
|
|
|
method: 'destroy'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
2020-04-13 13:21:47 +03:00
|
|
|
|
|
|
|
return models.Webhook.destroy(frame.options)
|
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Webhook.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
2020-04-13 13:21:47 +03:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|