2017-10-23 20:30:33 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
http = require('http'),
|
|
|
|
xml = require('xml'),
|
2017-10-25 17:27:56 +03:00
|
|
|
config = require('../config'),
|
2017-12-11 21:14:05 +03:00
|
|
|
urlService = require('../services/url'),
|
2017-12-11 22:27:09 +03:00
|
|
|
errors = require('../lib/common/errors'),
|
|
|
|
logging = require('../lib/common/logging'),
|
|
|
|
events = require('../lib/common/events'),
|
|
|
|
i18n = require('../lib/common/i18n'),
|
2017-10-25 17:27:56 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2017-10-25 17:27:56 +03:00
|
|
|
defaultPostSlugs = [
|
|
|
|
'welcome',
|
|
|
|
'the-editor',
|
|
|
|
'using-tags',
|
|
|
|
'managing-users',
|
|
|
|
'private-sites',
|
|
|
|
'advanced-markdown',
|
|
|
|
'themes'
|
|
|
|
],
|
|
|
|
// ToDo: Make this configurable
|
|
|
|
pingList = [{
|
|
|
|
host: 'blogsearch.google.com',
|
|
|
|
path: '/ping/RPC2'
|
|
|
|
}, {
|
|
|
|
host: 'rpc.pingomatic.com',
|
|
|
|
path: '/'
|
|
|
|
}];
|
2014-03-15 02:10:50 +04:00
|
|
|
|
|
|
|
function ping(post) {
|
|
|
|
var pingXML,
|
2014-12-10 17:03:39 +03:00
|
|
|
title = post.title,
|
2017-12-11 21:14:05 +03:00
|
|
|
url = urlService.utils.urlFor('post', {post: post}, true);
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2017-10-23 20:30:33 +03:00
|
|
|
if (post.page || config.isPrivacyDisabled('useRpcPing') || settingsCache.get('is_private')) {
|
2014-03-15 02:10:50 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:36:14 +03:00
|
|
|
// Don't ping for the default posts.
|
2014-05-08 06:58:01 +04:00
|
|
|
// 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.
|
2017-06-08 18:36:14 +03:00
|
|
|
if (defaultPostSlugs.indexOf(post.slug) > -1) {
|
2014-05-08 06:58:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
// Build XML object.
|
|
|
|
pingXML = xml({
|
|
|
|
methodCall: [{
|
|
|
|
methodName: 'weblogUpdate.ping'
|
|
|
|
}, {
|
|
|
|
params: [{
|
|
|
|
param: [{
|
|
|
|
value: [{
|
|
|
|
string: title
|
2014-09-10 08:06:24 +04:00
|
|
|
}]
|
2014-12-10 17:03:39 +03:00
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
param: [{
|
|
|
|
value: [{
|
|
|
|
string: url
|
2014-03-15 02:10:50 +04:00
|
|
|
}]
|
2014-09-10 08:06:24 +04:00
|
|
|
}]
|
|
|
|
}]
|
2014-12-10 17:03:39 +03:00
|
|
|
}]
|
|
|
|
}, {declaration: true});
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
// Ping each of the defined services.
|
|
|
|
_.each(pingList, function (pingHost) {
|
|
|
|
var options = {
|
|
|
|
hostname: pingHost.host,
|
|
|
|
path: pingHost.path,
|
|
|
|
method: 'POST'
|
|
|
|
},
|
|
|
|
req;
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
req = http.request(options);
|
|
|
|
req.write(pingXML);
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2016-10-04 18:33:43 +03:00
|
|
|
req.on('error', function handleError(err) {
|
2016-10-06 15:27:35 +03:00
|
|
|
logging.error(new errors.GhostError({
|
|
|
|
err: err,
|
2017-01-23 14:04:01 +03:00
|
|
|
message: err.message,
|
2017-10-25 17:27:56 +03:00
|
|
|
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'slack'}),
|
|
|
|
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'http://docs.ghost.org'})
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2016-10-04 18:33:43 +03:00
|
|
|
});
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
req.end();
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-23 20:30:33 +03:00
|
|
|
function listener(model, options) {
|
|
|
|
// CASE: do not rpc ping if we import a database
|
|
|
|
// TODO: refactor post.published events to never fire on importing
|
|
|
|
if (options && options.importing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-05 14:22:11 +03:00
|
|
|
ping(model.toJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
function listen() {
|
|
|
|
events.on('post.published', listener);
|
2015-03-24 23:23:23 +03:00
|
|
|
}
|
|
|
|
|
2014-03-15 02:10:50 +04:00
|
|
|
module.exports = {
|
2016-06-05 14:22:11 +03:00
|
|
|
listen: listen
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|