0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

updated events that send mentions (#16219)

refs TryGhost/Team#2477
-removed post.edited as it was too inclusive
-changed to post.published, post.published.edited, post.unpublished
-blocked import and internal data from triggering mentions
This commit is contained in:
Steve Larson 2023-02-01 16:49:58 -06:00 committed by GitHub
parent 5367fa94cc
commit ae92d1425d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 14 deletions

View file

@ -25,15 +25,25 @@ module.exports = class MentionSendingService {
} }
/** /**
* Listen for changes in posts and automatically send webmentions. * Listen for new and edited published posts and automatically send webmentions. Unpublished posts should send mentions
* so the receiver can discover a 404 response and remove the mentions.
* @param {*} events * @param {*} events
*/ */
listen(events) { listen(events) {
// Note: we don't need to listen for post.published (post.edited is also called at that time) events.on('post.published', this.sendForPost.bind(this));
events.on('post.edited', this.sendForEditedPost.bind(this)); events.on('post.published.edited', this.sendForPost.bind(this));
events.on('post.unpublished', this.sendForPost.bind(this));
} }
async sendForEditedPost(post) { async sendForPost(post, options) {
// NOTE: this is not ideal and shouldn't really be handled within the package...
// for now we don't want to evaluate mentions when importing data (at least needs queueing set up)
// we do not want to evaluate mentions with fixture (internal) data, e.g. generating posts
// TODO: real solution is likely suppressing event emission when building fixture data
if (options && (options.importing || options.context.internal)) {
return;
}
try { try {
if (!this.#isEnabled()) { if (!this.#isEnabled()) {
return; return;
@ -53,7 +63,7 @@ module.exports = class MentionSendingService {
previousHtml: post.previous('status') === 'published' ? post.previous('html') : null previousHtml: post.previous('status') === 'published' ? post.previous('html') : null
}); });
} catch (e) { } catch (e) {
logging.error('Error in webmention sending service post.added event handler:'); logging.error('Error in webmention sending service post update event handler:');
logging.error(e); logging.error(e);
} }
} }

View file

@ -29,7 +29,7 @@ describe('MentionSendingService', function () {
describe('listen', function () { describe('listen', function () {
it('Calls on post.edited', async function () { it('Calls on post.edited', async function () {
const service = new MentionSendingService({}); const service = new MentionSendingService({});
const stub = sinon.stub(service, 'sendForEditedPost').resolves(); const stub = sinon.stub(service, 'sendForPost').resolves();
let callback; let callback;
const events = { const events = {
on: sinon.stub().callsFake((event, c) => { on: sinon.stub().callsFake((event, c) => {
@ -43,13 +43,13 @@ describe('MentionSendingService', function () {
}); });
}); });
describe('sendForEditedPost', function () { describe('sendForPost', function () {
it('Ignores if disabled', async function () { it('Ignores if disabled', async function () {
const service = new MentionSendingService({ const service = new MentionSendingService({
isEnabled: () => false isEnabled: () => false
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost({}); await service.sendForPost({});
sinon.assert.notCalled(stub); sinon.assert.notCalled(stub);
}); });
@ -58,7 +58,7 @@ describe('MentionSendingService', function () {
isEnabled: () => true isEnabled: () => true
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'draft', status: 'draft',
html: 'changed', html: 'changed',
previous: { previous: {
@ -74,7 +74,7 @@ describe('MentionSendingService', function () {
isEnabled: () => true isEnabled: () => true
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'published', status: 'published',
html: 'same', html: 'same',
previous: { previous: {
@ -90,7 +90,7 @@ describe('MentionSendingService', function () {
isEnabled: () => true isEnabled: () => true
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'send', status: 'send',
html: 'changed', html: 'changed',
previous: { previous: {
@ -107,7 +107,7 @@ describe('MentionSendingService', function () {
getPostUrl: () => 'https://site.com/post/' getPostUrl: () => 'https://site.com/post/'
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'published', status: 'published',
html: 'same', html: 'same',
previous: { previous: {
@ -128,7 +128,7 @@ describe('MentionSendingService', function () {
getPostUrl: () => 'https://site.com/post/' getPostUrl: () => 'https://site.com/post/'
}); });
const stub = sinon.stub(service, 'sendAll'); const stub = sinon.stub(service, 'sendAll');
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'published', status: 'published',
html: 'updated', html: 'updated',
previous: { previous: {
@ -149,7 +149,7 @@ describe('MentionSendingService', function () {
getPostUrl: () => 'https://site.com/post/' getPostUrl: () => 'https://site.com/post/'
}); });
sinon.stub(service, 'sendAll').rejects(new Error('Internal error test')); sinon.stub(service, 'sendAll').rejects(new Error('Internal error test'));
await service.sendForEditedPost(createModel({ await service.sendForPost(createModel({
status: 'published', status: 'published',
html: 'same', html: 'same',
previous: { previous: {