0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/data/xml/xmlrpc.js
Hannah Wolfe f489d7df72 xmlrpc + slack init() -> listen() & fixup tests
no issue

- changes xmlrcp & slack `init` function to be `listen`
- update the code to use `listen` instead of `init`
- changes the tests to make sure that event listeners are not wired up
- adds 100% test coverage

Since we added slack event listeners, the xmlrpc event tests have been throwing an error:
 > Unhandled rejection Error
See: http://puu.sh/phvjZ.png

This is because both xmlrpc & slack are listening to `post.published` events.
xmlrpc didn't require any extra stubbing, but the slack listener did
By turning the listeners off after the tests, we reset the environment to not impact the next event test

We probably need to do more work like this to improve the systems around event handling and
make them more robust
2016-06-09 15:38:19 +01:00

91 lines
2.3 KiB
JavaScript

var _ = require('lodash'),
http = require('http'),
xml = require('xml'),
config = require('../../config'),
errors = require('../../errors'),
events = require('../../events'),
i18n = require('../../i18n'),
pingList;
// ToDo: Make this configurable
pingList = [{
host: 'blogsearch.google.com',
path: '/ping/RPC2'
}, {
host: 'rpc.pingomatic.com',
path: '/'
}];
function ping(post) {
var pingXML,
title = post.title,
url = config.urlFor('post', {post: post}, true);
// Only ping when in production and not a page
if (process.env.NODE_ENV !== 'production' || post.page || config.isPrivacyDisabled('useRpcPing')) {
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 XML object.
pingXML = xml({
methodCall: [{
methodName: 'weblogUpdate.ping'
}, {
params: [{
param: [{
value: [{
string: title
}]
}]
}, {
param: [{
value: [{
string: url
}]
}]
}]
}]
}, {declaration: true});
// Ping each of the defined services.
_.each(pingList, function (pingHost) {
var options = {
hostname: pingHost.host,
path: pingHost.path,
method: 'POST'
},
req;
req = http.request(options);
req.write(pingXML);
req.on('error', function handleError(error) {
errors.logError(
error,
i18n.t('errors.data.xml.xmlrpc.pingUpdateFailed.error'),
i18n.t('errors.data.xml.xmlrpc.pingUpdateFailed.help', {url: 'http://support.ghost.org'})
);
}
);
req.end();
});
}
function listener(model) {
ping(model.toJSON());
}
function listen() {
events.on('post.published', listener);
}
module.exports = {
listen: listen
};