Skip to content

Instantly share code, notes, and snippets.

@el-toxico
Created June 27, 2017 18:55
Show Gist options
  • Save el-toxico/329fe90f320cfcc8ee37da6eac395849 to your computer and use it in GitHub Desktop.
Save el-toxico/329fe90f320cfcc8ee37da6eac395849 to your computer and use it in GitHub Desktop.
diff -Nur ghost-upstream/config.example.js ghost-webhook/config.example.js
--- ghost-upstream/config.example.js 2017-06-27 11:49:15.000000000 -0700
+++ ghost-webhook/config.example.js 2017-06-27 11:47:42.000000000 -0700
@@ -54,6 +54,13 @@
// },
// ```
+ // Example webhook
+ //
+ // webhook: {
+ // host: 'prerelease.zyxyz.org',
+ // path: '/gh/webhook'
+ // },
+
// #### Database
// Ghost supports sqlite3 (default), MySQL & PostgreSQL
database: {
diff -Nur ghost-upstream/core/server/data/webhook/index.js ghost-webhook/core/server/data/webhook/index.js
--- ghost-upstream/core/server/data/webhook/index.js 1969-12-31 16:00:00.000000000 -0800
+++ ghost-webhook/core/server/data/webhook/index.js 2017-06-27 11:47:42.000000000 -0700
@@ -0,0 +1,68 @@
+var _ = require('lodash'),
+ https = require('https'),
+ querystring = require('querystring'),
+ config = require('../../config'),
+ errors = require('../../errors'),
+ events = require('../../events'),
+ i18n = require('../../i18n'),
+ req,
+ req_options,
+ pingHost;
+
+// ToDo: Make this configurable
+
+function ping(post) {
+ var post_data,
+ pingHost;
+
+ if (!config.webhook) {
+ return;
+ }
+
+
+ // Don't ping for the welcome to ghost post.
+ // This also handles the case where during ghost's first run
+ // models.init() inserts this post but permissions.init() hasn't
+ // (can't) run yet.
+ if (post.slug === 'welcome-to-ghost') {
+ return;
+ }
+
+ // Build query string
+ post_data = querystring.stringify( post );
+
+ req_options = {
+ hostname: config.webhook.host,
+ path: config.webhook.path,
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Content-Length': Buffer.byteLength(post_data)
+ }
+ };
+
+ req = https.request(req_options);
+ req.write(post_data);
+ req.on('error', function handleError(error) {
+ errors.logError(
+ error,
+ i18n.t('errors.data.webhook.pingUpdateFailed.error'),
+ i18n.t('errors.data.webhook.pingUpdateFailed.help', {url: 'http://support.ghost.org'})
+ );
+ }
+ );
+ req.end();
+}
+
+function listener(model) {
+ ping(model.toJSON());
+}
+
+function listen() {
+ events.on('post.published', listener);
+ events.on('post.published.edited', listener);
+}
+
+module.exports = {
+ listen: listen
+};
diff -Nur ghost-upstream/core/server/index.js ghost-webhook/core/server/index.js
--- ghost-upstream/core/server/index.js 2017-06-27 11:49:15.000000000 -0700
+++ ghost-webhook/core/server/index.js 2017-06-27 11:47:42.000000000 -0700
@@ -29,6 +29,7 @@
apps = require('./apps'),
xmlrpc = require('./data/xml/xmlrpc'),
slack = require('./data/slack'),
+ webhook = require('./data/webhook'),
GhostServer = require('./ghost-server'),
scheduling = require('./scheduling'),
dbHash;
@@ -180,7 +181,9 @@
// Initialize xmrpc ping
xmlrpc.listen(),
// Initialize slack ping
- slack.listen()
+ slack.listen(),
+ // Initialize webhook ping
+ webhook.listen()
);
}).then(function () {
// Get reference to an express app instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment