forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes TryGhost#2148 - Added core/server/xmlrpc.js - Hook into post::saved to ping when a published post gets saved - Added node package to hook into http requests
- Loading branch information
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var _ = require('lodash'), | ||
config = require('./config'), | ||
errors = require('./errorHandling'), | ||
http = require('http'), | ||
xml = require('xml'), | ||
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; | ||
|
||
// Only ping when in production and not a page | ||
if (process.env.NODE_ENV !== 'production' || post.page) { | ||
return; | ||
} | ||
|
||
// Need to require here because of circular dependency | ||
return config.urlForPost(require('./api').settings, post, true).then(function (url) { | ||
|
||
// 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 (error) { | ||
errors.logError( | ||
error, | ||
"Pinging services for updates on your blog failed, your blog will continue to function.", | ||
"If you get this error repeatedly, please seek help from https://ghost.org/forum." | ||
); | ||
}); | ||
req.end(); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
ping: ping | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*globals describe, beforeEach, afterEach, it*/ | ||
var assert = require('assert'), | ||
http = require('http'), | ||
nock = require('nock'), | ||
settings = require('../../server/api').settings; | ||
should = require('should'), | ||
sinon = require('sinon'), | ||
testUtils = require('../utils'), | ||
when = require('when'), | ||
xmlrpc = require('../../server/xmlrpc'), | ||
// storing current environment | ||
currentEnv = process.env.NODE_ENV; | ||
|
||
describe('XMLRPC', function () { | ||
var sandbox; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
// give environment a value that will ping | ||
process.env.NODE_ENV = "production"; | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
// reset the environment | ||
process.env.NODE_ENV = currentEnv; | ||
}); | ||
|
||
|
||
it('should execute two pings', function (done) { | ||
var ping1 = nock('http://blogsearch.google.com').post('/ping/RPC2').reply(200), | ||
ping2 = nock('http://rpc.pingomatic.com').post('/').reply(200), | ||
testPost = testUtils.DataGenerator.Content.posts[2], | ||
settingsStub = sandbox.stub(settings, 'read', function () { | ||
return when({value: '/:slug/'}); | ||
}); | ||
|
||
xmlrpc.ping(testPost).then(function () { | ||
ping1.isDone().should.be.true; | ||
ping2.isDone().should.be.true; | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters