2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const xml = require('xml');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../shared/config');
|
2020-04-29 18:44:27 +03:00
|
|
|
const urlService = require('../../frontend/services/url');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {events, i18n} = require('../lib/common');
|
|
|
|
const logging = require('../../shared/logging');
|
2020-04-29 18:44:27 +03:00
|
|
|
const request = require('../lib/request');
|
|
|
|
const settingsCache = require('./settings/cache');
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const defaultPostSlugs = [
|
|
|
|
'welcome',
|
|
|
|
'the-editor',
|
|
|
|
'using-tags',
|
|
|
|
'managing-users',
|
|
|
|
'private-sites',
|
|
|
|
'advanced-markdown',
|
|
|
|
'themes'
|
|
|
|
];
|
|
|
|
|
|
|
|
// ToDo: Make this configurable
|
|
|
|
const pingList = [
|
|
|
|
{
|
|
|
|
url: 'http://rpc.pingomatic.com'
|
|
|
|
}
|
|
|
|
];
|
2014-03-15 02:10:50 +04:00
|
|
|
|
|
|
|
function ping(post) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pingXML;
|
|
|
|
const title = post.title;
|
|
|
|
const url = urlService.getUrlByResourceId(post.id, {absolute: true});
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2019-09-16 13:51:54 +03:00
|
|
|
if (post.type === '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: [{
|
2018-08-23 16:20:29 +03:00
|
|
|
methodName: 'weblogUpdates.ping'
|
2014-12-10 17:03:39 +03:00
|
|
|
}, {
|
|
|
|
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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const options = {
|
2017-12-15 12:50:18 +03:00
|
|
|
body: pingXML,
|
|
|
|
timeout: 2 * 1000
|
|
|
|
};
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2018-08-23 16:20:29 +03:00
|
|
|
const goodResponse = /<member>[\s]*<name>flerror<\/name>[\s]*<value>[\s]*<boolean>0<\/boolean><\/value><\/member>/;
|
|
|
|
const errorMessage = /<name>(?:faultString|message)<\/name>[\s]*<value>[\s]*<string>([^<]+)/;
|
|
|
|
|
2017-12-15 12:50:18 +03:00
|
|
|
request(pingHost.url, options)
|
2018-08-23 16:20:29 +03:00
|
|
|
.then(function (res) {
|
|
|
|
if (!goodResponse.test(res.body)) {
|
|
|
|
const matches = res.body.match(errorMessage);
|
|
|
|
const message = matches ? matches[1] : res.body;
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
})
|
2017-12-15 12:50:18 +03:00
|
|
|
.catch(function (err) {
|
2021-02-16 17:31:44 +03:00
|
|
|
if (err.statusCode === 429) {
|
|
|
|
logging.error(new errors.TooManyRequestsError({
|
|
|
|
err,
|
|
|
|
message: err.message,
|
|
|
|
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'xmlrpc'}),
|
|
|
|
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
logging.error(new errors.GhostError({
|
|
|
|
err: err,
|
|
|
|
message: err.message,
|
|
|
|
context: i18n.t('errors.services.ping.requestFailed.error', {service: 'xmlrpc'}),
|
|
|
|
help: i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
|
|
|
|
}));
|
|
|
|
}
|
2017-12-15 12:50:18 +03:00
|
|
|
});
|
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() {
|
2020-04-30 22:26:12 +03:00
|
|
|
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
|
|
|
};
|