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');
|
2021-10-18 17:27:57 +03:00
|
|
|
const urlService = require('./url');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-07-01 14:37:32 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-06-16 14:54:14 +03:00
|
|
|
const request = require('@tryghost/request');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../shared/settings-cache');
|
2021-02-18 21:21:15 +03:00
|
|
|
const sentry = require('../../shared/sentry');
|
2014-03-15 02:10:50 +04:00
|
|
|
|
2021-07-07 17:49:45 +03:00
|
|
|
// Used to receive post.published model event
|
|
|
|
const events = require('../lib/common/events');
|
|
|
|
|
2021-07-01 14:37:32 +03:00
|
|
|
const messages = {
|
|
|
|
requestFailedError: 'The {service} service was unable to send a ping request, your site will continue to function.',
|
|
|
|
requestFailedHelp: 'If you get this error repeatedly, please seek help on {url}.'
|
|
|
|
};
|
|
|
|
|
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;
|
2021-12-01 13:22:01 +03:00
|
|
|
throw new errors.InternalServerError({message});
|
2018-08-23 16:20:29 +03:00
|
|
|
}
|
|
|
|
})
|
2017-12-15 12:50:18 +03:00
|
|
|
.catch(function (err) {
|
2021-02-19 16:00:50 +03:00
|
|
|
let error;
|
2021-02-16 17:31:44 +03:00
|
|
|
if (err.statusCode === 429) {
|
2021-02-19 16:00:50 +03:00
|
|
|
error = new errors.TooManyRequestsError({
|
2021-02-16 17:31:44 +03:00
|
|
|
err,
|
|
|
|
message: err.message,
|
2021-07-01 14:37:32 +03:00
|
|
|
context: tpl(messages.requestFailedError, {service: 'xmlrpc'}),
|
|
|
|
help: tpl(messages.requestFailedHelp, {url: 'https://ghost.org/docs/'})
|
2021-02-18 21:21:15 +03:00
|
|
|
});
|
2021-02-16 17:31:44 +03:00
|
|
|
} else {
|
2021-12-01 13:22:01 +03:00
|
|
|
error = new errors.InternalServerError({
|
2021-02-16 17:31:44 +03:00
|
|
|
err: err,
|
|
|
|
message: err.message,
|
2021-07-01 14:37:32 +03:00
|
|
|
context: tpl(messages.requestFailedError, {service: 'xmlrpc'}),
|
|
|
|
help: tpl(messages.requestFailedHelp, {url: 'https://ghost.org/docs/'})
|
2021-02-19 16:00:50 +03:00
|
|
|
});
|
2021-02-16 17:31:44 +03:00
|
|
|
}
|
2021-02-19 16:00:50 +03:00
|
|
|
logging.error(error);
|
|
|
|
sentry.captureException(error);
|
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
|
|
|
};
|