2018-10-12 00:22:38 +05:30
|
|
|
const models = require('../../models');
|
2021-10-06 18:09:05 +05:30
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 13:22:20 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2018-10-12 00:22:38 +05:30
|
|
|
|
2021-10-06 18:09:05 +05:30
|
|
|
const messages = {
|
|
|
|
resourceNotFound: '{resource} not found.',
|
|
|
|
noPermissionToEdit: {
|
|
|
|
message: 'You do not have permission to {method} this webhook.',
|
|
|
|
context: 'You may only {method} webhooks that belong to the authenticated integration. Check the supplied Admin API Key.'
|
|
|
|
},
|
|
|
|
webhookAlreadyExists: 'Target URL has already been used for this event.'
|
|
|
|
};
|
|
|
|
|
2018-10-12 00:22:38 +05:30
|
|
|
module.exports = {
|
|
|
|
docName: 'webhooks',
|
2019-02-07 23:21:59 +01:00
|
|
|
|
2018-10-12 00:22:38 +05:30
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
2020-09-14 22:33:37 +12:00
|
|
|
headers: {
|
|
|
|
// NOTE: remove if there is ever a 'read' method
|
|
|
|
location: false
|
|
|
|
},
|
2018-10-12 00:22:38 +05:30
|
|
|
options: [],
|
2019-02-07 23:21:59 +01:00
|
|
|
data: [],
|
|
|
|
validation: {
|
|
|
|
data: {
|
|
|
|
event: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
target_url: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-12 00:22:38 +05:30
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Webhook.getByEventAndTarget(
|
|
|
|
frame.data.webhooks[0].event,
|
|
|
|
frame.data.webhooks[0].target_url,
|
|
|
|
frame.options
|
|
|
|
).then((webhook) => {
|
|
|
|
if (webhook) {
|
|
|
|
return Promise.reject(
|
2021-10-06 18:09:05 +05:30
|
|
|
new errors.ValidationError({message: tpl(messages.webhookAlreadyExists)})
|
2018-10-12 00:22:38 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.Webhook.add(frame.data.webhooks[0], frame.options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2019-02-07 23:21:59 +01:00
|
|
|
|
2018-10-19 18:35:55 +01:00
|
|
|
edit: {
|
2020-07-08 16:54:31 +12:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
2020-08-04 16:43:24 +12:00
|
|
|
if (frame.options.context && frame.options.context.integration && frame.options.context.integration.id) {
|
2020-07-08 16:54:31 +12:00
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
2020-08-03 23:08:47 +12:00
|
|
|
if (!webhook) {
|
|
|
|
throw new errors.NotFoundError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.resourceNotFound, {
|
2020-08-03 23:08:47 +12:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-04 16:43:24 +12:00
|
|
|
if (webhook.get('integration_id') !== frame.options.context.integration.id) {
|
2020-07-08 16:54:31 +12:00
|
|
|
throw new errors.NoPermissionError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.noPermissionToEdit.message, {
|
2020-07-08 16:54:31 +12:00
|
|
|
method: 'edit'
|
|
|
|
}),
|
2021-10-06 18:09:05 +05:30
|
|
|
context: tpl(messages.noPermissionToEdit.context, {
|
2020-07-08 16:54:31 +12:00
|
|
|
method: 'edit'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-19 18:35:55 +01: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 13:22:20 -05:00
|
|
|
throw new errors.NotFoundError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.resourceNotFound, {
|
2018-10-19 18:35:55 +01:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2019-02-07 23:21:59 +01:00
|
|
|
|
2018-10-12 00:22:38 +05:30
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-07-08 16:54:31 +12:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
2020-08-04 16:43:24 +12:00
|
|
|
if (frame.options.context && frame.options.context.integration && frame.options.context.integration.id) {
|
2020-07-08 16:54:31 +12:00
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
2020-08-03 23:08:47 +12:00
|
|
|
if (!webhook) {
|
|
|
|
throw new errors.NotFoundError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.resourceNotFound, {
|
2020-08-03 23:08:47 +12:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-04 16:43:24 +12:00
|
|
|
if (webhook.get('integration_id') !== frame.options.context.integration.id) {
|
2020-07-08 16:54:31 +12:00
|
|
|
throw new errors.NoPermissionError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.noPermissionToEdit.message, {
|
2020-07-20 09:05:56 +01:00
|
|
|
method: 'destroy'
|
2020-07-08 16:54:31 +12:00
|
|
|
}),
|
2021-10-06 18:09:05 +05:30
|
|
|
context: tpl(messages.noPermissionToEdit.context, {
|
2020-07-08 16:54:31 +12:00
|
|
|
method: 'destroy'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-12 00:22:38 +05:30
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
2020-04-13 11:21:47 +01:00
|
|
|
return models.Webhook.destroy(frame.options)
|
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Webhook.NotFoundError, () => {
|
2020-05-22 13:22:20 -05:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-10-06 18:09:05 +05:30
|
|
|
message: tpl(messages.resourceNotFound, {
|
2020-04-13 11:21:47 +01:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
});
|
2018-10-12 00:22:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|