Skip to content

Commit 1e00bd2

Browse files
9larsonsdaniellockyer
authored andcommitted
Handled empty content when sending webmentions
refs https://ghost.slack.com/archives/C02G9E68C/p1676564978732119 - `cheerio` errors when trying to parse `null`
1 parent f27c7ba commit 1e00bd2

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

ghost/core/test/e2e-server/services/mentions.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ describe('Mentions Service', function () {
7777
assert.equal(mentionMock.isDone(), false);
7878
assert.equal(endpointMock.isDone(), false);
7979
});
80+
81+
it('Post without content', async function () {
82+
let publishedPost = {status: 'published', mobiledoc: markdownToMobiledoc(''), title: 'empty post'};
83+
await agent
84+
.post('posts/')
85+
.body({posts: [publishedPost]})
86+
.expectStatus(201);
87+
88+
assert.equal(mentionMock.isDone(), false);
89+
assert.equal(endpointMock.isDone(), false);
90+
});
8091
});
8192

8293
describe(`does send when we expect it to send`, function () {

ghost/webmentions/lib/MentionSendingService.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ module.exports = class MentionSendingService {
5959
// Post should be or should have been published
6060
return;
6161
}
62-
await this.#jobService.addJob('sendWebmentions', async () => {
63-
await this.sendAll({
64-
url: new URL(this.#getPostUrl(post)),
65-
html: post.get('html'),
66-
previousHtml: post.previous('status') === 'published' ? post.previous('html') : null
62+
// make sure we have something to parse before we create a job
63+
let html = post.get('html');
64+
let previousHtml = post.previous('status') === 'published' ? post.previous('html') : null;
65+
if (html || previousHtml) {
66+
await this.#jobService.addJob('sendWebmentions', async () => {
67+
await this.sendAll({
68+
url: new URL(this.#getPostUrl(post)),
69+
html: html,
70+
previousHtml: previousHtml
71+
});
6772
});
68-
});
73+
}
6974
} catch (e) {
7075
logging.error('Error in webmention sending service post update event handler:');
7176
logging.error(e);
@@ -104,7 +109,7 @@ module.exports = class MentionSendingService {
104109
* @param {string|null} [resource.previousHtml]
105110
*/
106111
async sendAll(resource) {
107-
const links = this.getLinks(resource.html);
112+
const links = resource.html ? this.getLinks(resource.html) : [];
108113
if (resource.previousHtml) {
109114
// We also need to send webmentions for removed links
110115
const oldLinks = this.getLinks(resource.previousHtml);

ghost/webmentions/test/MentionSendingService.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ describe('MentionSendingService', function () {
188188
}));
189189
assert(errorLogStub.calledTwice);
190190
});
191+
192+
it('Sends no mentions for posts without html and previous html', async function () {
193+
const service = new MentionSendingService({
194+
isEnabled: () => true,
195+
getPostUrl: () => 'https://site.com/post/',
196+
jobService: jobService
197+
});
198+
const stub = sinon.stub(service, 'sendAll');
199+
await service.sendForPost(createModel({
200+
status: 'published',
201+
html: '',
202+
previous: {
203+
status: 'draft',
204+
html: ''
205+
}
206+
}));
207+
assert(stub.notCalled);
208+
});
191209
});
192210

193211
describe('sendAll', function () {
@@ -283,6 +301,16 @@ describe('MentionSendingService', function () {
283301
assert.strictEqual(scope.isDone(), true);
284302
assert.equal(counter, 2);
285303
});
304+
305+
// cheerio must be served a string
306+
it('Does not evaluate links for an empty post', async function () {
307+
const service = new MentionSendingService({
308+
isEnabled: () => true
309+
});
310+
const linksStub = sinon.stub(service, 'getLinks');
311+
await service.sendAll({html: ``,previousHtml: ``});
312+
sinon.assert.notCalled(linksStub);
313+
});
286314
});
287315

288316
describe('getLinks', function () {

0 commit comments

Comments
 (0)