Handled empty content when sending webmentions

refs https://ghost.slack.com/archives/C02G9E68C/p1676564978732119

- `cheerio` errors when trying to parse `null`
This commit is contained in:
Steve Larson 2023-02-16 11:07:04 -06:00 committed by Daniel Lockyer
parent f27c7ba044
commit 1e00bd2af9
3 changed files with 51 additions and 7 deletions

View File

@ -77,6 +77,17 @@ describe('Mentions Service', function () {
assert.equal(mentionMock.isDone(), false);
assert.equal(endpointMock.isDone(), false);
});
it('Post without content', async function () {
let publishedPost = {status: 'published', mobiledoc: markdownToMobiledoc(''), title: 'empty post'};
await agent
.post('posts/')
.body({posts: [publishedPost]})
.expectStatus(201);
assert.equal(mentionMock.isDone(), false);
assert.equal(endpointMock.isDone(), false);
});
});
describe(`does send when we expect it to send`, function () {

View File

@ -59,13 +59,18 @@ module.exports = class MentionSendingService {
// Post should be or should have been published
return;
}
await this.#jobService.addJob('sendWebmentions', async () => {
await this.sendAll({
url: new URL(this.#getPostUrl(post)),
html: post.get('html'),
previousHtml: post.previous('status') === 'published' ? post.previous('html') : null
// make sure we have something to parse before we create a job
let html = post.get('html');
let previousHtml = post.previous('status') === 'published' ? post.previous('html') : null;
if (html || previousHtml) {
await this.#jobService.addJob('sendWebmentions', async () => {
await this.sendAll({
url: new URL(this.#getPostUrl(post)),
html: html,
previousHtml: previousHtml
});
});
});
}
} catch (e) {
logging.error('Error in webmention sending service post update event handler:');
logging.error(e);
@ -104,7 +109,7 @@ module.exports = class MentionSendingService {
* @param {string|null} [resource.previousHtml]
*/
async sendAll(resource) {
const links = this.getLinks(resource.html);
const links = resource.html ? this.getLinks(resource.html) : [];
if (resource.previousHtml) {
// We also need to send webmentions for removed links
const oldLinks = this.getLinks(resource.previousHtml);

View File

@ -188,6 +188,24 @@ describe('MentionSendingService', function () {
}));
assert(errorLogStub.calledTwice);
});
it('Sends no mentions for posts without html and previous html', async function () {
const service = new MentionSendingService({
isEnabled: () => true,
getPostUrl: () => 'https://site.com/post/',
jobService: jobService
});
const stub = sinon.stub(service, 'sendAll');
await service.sendForPost(createModel({
status: 'published',
html: '',
previous: {
status: 'draft',
html: ''
}
}));
assert(stub.notCalled);
});
});
describe('sendAll', function () {
@ -283,6 +301,16 @@ describe('MentionSendingService', function () {
assert.strictEqual(scope.isDone(), true);
assert.equal(counter, 2);
});
// cheerio must be served a string
it('Does not evaluate links for an empty post', async function () {
const service = new MentionSendingService({
isEnabled: () => true
});
const linksStub = sinon.stub(service, 'getLinks');
await service.sendAll({html: ``,previousHtml: ``});
sinon.assert.notCalled(linksStub);
});
});
describe('getLinks', function () {