0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/webhooks/trigger.js
Naz 67dca08df8 Refactored trigger module to be testable
refs https://github.com/TryGhost/Toolbox/issues/283

- Current trigger module handling webhook paypload delivery isn't testable! It sucks to add features to it without assurance things still work
- Apart from expanding the test suite this changeset also needs live testing - setting up webhooks etc.
2022-05-12 13:54:21 +08:00

99 lines
3 KiB
JavaScript

const debug = require('@tryghost/debug')('services:webhooks:trigger');
const logging = require('@tryghost/logging');
const request = require('@tryghost/request');
const ghostVersion = require('@tryghost/version');
class WebhookTrigger {
constructor({models, payload}){
this.models = models;
this.payload = payload;
}
getAll(event) {
return this.models
.Webhook
.findAllByEvent(event, {context: {internal: true}});
}
update(webhook, data) {
this.models
.Webhook
.edit({
last_triggered_at: Date.now(),
last_triggered_status: data.statusCode,
last_triggered_error: data.error || null
}, {id: webhook.id})
.catch(() => {
logging.warn(`Unable to update "last_triggered" for webhook: ${webhook.id}`);
});
}
destroy(webhook) {
return this.models
.Webhook
.destroy({id: webhook.id}, {context: {internal: true}})
.catch(() => {
logging.warn(`Unable to destroy webhook ${webhook.id}.`);
});
}
onSuccess(webhook) {
return (res) => {
this.update(webhook, {
statusCode: res.statusCode
});
};
}
onError(webhook) {
return (err) => {
if (err.statusCode === 410) {
logging.info(`Webhook destroyed (410 response) for "${webhook.get('event')}" with url "${webhook.get('target_url')}".`);
return this.destroy(webhook);
}
this.update(webhook, {
statusCode: err.statusCode,
error: `Request failed: ${err.code || 'unknown'}`
});
logging.warn(`Request to ${webhook.get('target_url') || null} failed because of: ${err.code || ''}.`);
};
}
async trigger(event, model) {
const response = {
onSuccess: this.onSuccess.bind(this),
onError: this.onError.bind(this)
};
const hooks = await this.getAll(event);
debug(`${hooks.models.length} webhooks found for ${event}.`);
hooks.models.forEach(async (webhook) => {
const hookPayload = await this.payload(webhook.get('event'), model);
const reqPayload = JSON.stringify(hookPayload);
const url = webhook.get('target_url');
const opts = {
body: reqPayload,
headers: {
'Content-Length': Buffer.byteLength(reqPayload),
'Content-Type': 'application/json'
},
timeout: 2 * 1000,
retry: 5
};
logging.info(`Triggering webhook for "${webhook.get('event')}" with url "${url}"`);
request(url, opts)
.then(response.onSuccess(webhook))
.catch(response.onError(webhook));
});
}
}
module.exports = WebhookTrigger;