2018-10-18 21:31:30 +03:00
|
|
|
const _ = require('lodash');
|
2019-02-03 15:36:08 +03:00
|
|
|
const debug = require('ghost-ignition').debug('services:webhooks:trigger');
|
2018-10-18 21:31:30 +03:00
|
|
|
const common = require('../../lib/common');
|
|
|
|
const request = require('../../../server/lib/request');
|
2019-02-03 15:36:08 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const payload = require('./payload');
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
const webhooks = {
|
|
|
|
getAll(event) {
|
|
|
|
return models
|
|
|
|
.Webhook
|
|
|
|
.findAllByEvent(event, {context: {internal: true}});
|
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
update(webhook, data) {
|
|
|
|
models
|
|
|
|
.Webhook
|
|
|
|
.edit({
|
|
|
|
last_triggered_at: Date.now(),
|
|
|
|
last_triggered_status: data.statusCode,
|
|
|
|
last_triggered_error: data.error || null
|
|
|
|
}, {id: webhook.id})
|
|
|
|
.catch(() => {
|
|
|
|
common.logging.warn(`Unable to update "last_triggered" for webhook: ${webhook.id}`);
|
|
|
|
});
|
|
|
|
},
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
destroy(webhook) {
|
|
|
|
return models
|
|
|
|
.Webhook
|
|
|
|
.destroy({id: webhook.id}, {context: {internal: true}})
|
|
|
|
.catch(() => {
|
|
|
|
common.logging.warn(`Unable to destroy webhook ${webhook.id}.`);
|
2018-10-18 21:31:30 +03:00
|
|
|
});
|
2019-02-03 15:36:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
onSuccess(webhook) {
|
|
|
|
return (res) => {
|
|
|
|
webhooks.update(webhook, {
|
|
|
|
statusCode: res.statusCode
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
onError(webhook) {
|
|
|
|
return (err) => {
|
2018-10-18 21:31:30 +03:00
|
|
|
if (err.statusCode === 410) {
|
2019-02-03 15:36:08 +03:00
|
|
|
common.logging.info(`Webhook destroyed (410 response) for "${webhook.get('event')}" with url "${webhook.get('target_url')}".`);
|
|
|
|
|
|
|
|
return webhooks.destroy(webhook);
|
2018-10-18 21:31:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
webhooks.update(webhook, {
|
|
|
|
statusCode: err.statusCode,
|
|
|
|
error: `Request failed: ${err.code || 'unknown'}`
|
|
|
|
});
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
common.logging.warn(`Request to ${webhook.get('target_url') || null} failed because of: ${err.code || ''}.`);
|
|
|
|
};
|
2018-10-18 21:31:30 +03:00
|
|
|
}
|
2019-02-03 15:36:08 +03:00
|
|
|
};
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
module.exports = (event, model) => {
|
|
|
|
webhooks.getAll(event)
|
|
|
|
.then((webhooks) => {
|
2019-02-24 00:03:49 +03:00
|
|
|
debug(`${webhooks.models.length} webhooks found for ${event}.`);
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-03 15:36:08 +03:00
|
|
|
_.each(webhooks.models, (webhook) => {
|
2019-02-25 11:38:08 +03:00
|
|
|
payload(webhook.get('event'), model)
|
|
|
|
.then((payload) => {
|
|
|
|
const reqPayload = JSON.stringify(payload);
|
|
|
|
const url = webhook.get('target_url');
|
|
|
|
const opts = {
|
|
|
|
body: reqPayload,
|
|
|
|
headers: {
|
|
|
|
'Content-Length': Buffer.byteLength(reqPayload),
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
timeout: 2 * 1000,
|
|
|
|
retries: 5
|
|
|
|
};
|
2018-10-18 21:31:30 +03:00
|
|
|
|
2019-02-25 11:38:08 +03:00
|
|
|
common.logging.info(`Trigger Webhook for "${webhook.get('event')}" with url "${url}".`);
|
2019-02-03 15:36:08 +03:00
|
|
|
|
2019-02-25 11:38:08 +03:00
|
|
|
request(url, opts)
|
|
|
|
.then(response.onSuccess(webhook))
|
|
|
|
.catch(response.onError(webhook));
|
|
|
|
});
|
2019-02-03 15:36:08 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|