mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
bffb3dbd90
no issue Support for http://resthooks.org style webhooks that can be used with Zapier triggers. This can currently be used in two ways: a) adding a webhook record to the DB manually b) using the API with password auth and POSTing to /webhooks/ (this is private API so not documented) ⚠️ only _https_ URLs are supported in the webhook `target_url` field 🚨 - add `webhooks` table to store event names and target urls - add `POST` and `DELETE` endpoints for `/webhooks/` - configure `subscribers.added` and `subscribers.deleted` events to trigger registered webhooks
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
var _ = require('lodash'),
|
|
events = require('../events'),
|
|
api = require('../api'),
|
|
modelAttrs;
|
|
|
|
// TODO: this can be removed once all events pass a .toJSON object through
|
|
modelAttrs = {
|
|
subscriber: ['id', 'name', 'email']
|
|
};
|
|
|
|
// TODO: this works for basic models but we eventually want a full API response
|
|
// with embedded models (?include=tags) and so on
|
|
function generatePayload(event, model) {
|
|
var modelName = event.split('.')[0],
|
|
pluralModelName = modelName + 's',
|
|
action = event.split('.')[1],
|
|
payload = {},
|
|
data;
|
|
|
|
if (action === 'deleted') {
|
|
data = {};
|
|
modelAttrs[modelName].forEach(function (key) {
|
|
if (model._previousAttributes[key] !== undefined) {
|
|
data[key] = model._previousAttributes[key];
|
|
}
|
|
});
|
|
} else {
|
|
data = model.toJSON();
|
|
}
|
|
|
|
payload[pluralModelName] = [data];
|
|
|
|
return payload;
|
|
}
|
|
|
|
function listener(event, model, options) {
|
|
var payload = generatePayload(event, model);
|
|
|
|
// avoid triggering webhooks when importing
|
|
if (options && options.importing) {
|
|
return;
|
|
}
|
|
|
|
api.webhooks.trigger(event, payload, options);
|
|
}
|
|
|
|
// TODO: use a wildcard with the new event emitter or use the webhooks API to
|
|
// register listeners only for events that have webhooks
|
|
function listen() {
|
|
events.on('subscriber.added', _.partial(listener, 'subscriber.added'));
|
|
events.on('subscriber.deleted', _.partial(listener, 'subscriber.deleted'));
|
|
}
|
|
|
|
// Public API
|
|
module.exports = {
|
|
listen: listen
|
|
};
|