diff --git a/ghost/core/core/server/services/slack.js b/ghost/core/core/server/services/slack.js index b2027ea85f..be9916e4bc 100644 --- a/ghost/core/core/server/services/slack.js +++ b/ghost/core/core/server/services/slack.js @@ -63,7 +63,24 @@ function ping(post) { if (post.custom_excerpt) { description = post.custom_excerpt; } else if (post.html) { - description = `${post.html.replace(/<[^>]+>/g, '').split('.').slice(0, 3).join('.')}.`; + const membersContentIdx = post.html.indexOf(''); + const substringEnd = membersContentIdx > -1 ? membersContentIdx : post.html.length; + + description = `${ + post.html + // Remove members-only content + .substring(0, substringEnd) + // Strip out HTML + .replace(/<[^>]+>/g, '') + // Split into sentences + .split('.') + // Remove empty strings + .filter(sentence => sentence.trim() !== '') + // Get the first three sentences + .slice(0, 3) + // Join 'em back together + .join('.') + }.`; } else { description = null; } @@ -160,7 +177,10 @@ function slackListener(model, options) { return; } - ping(model.toJSON()); + ping({ + ...model.toJSON(), + authors: model.related('authors').toJSON() + }); } function slackTestPing() { diff --git a/ghost/core/test/unit/server/services/slack.test.js b/ghost/core/test/unit/server/services/slack.test.js index be3a019254..7b9b45f5ee 100644 --- a/ghost/core/test/unit/server/services/slack.test.js +++ b/ghost/core/test/unit/server/services/slack.test.js @@ -38,10 +38,22 @@ describe('Slack', function () { it('listener() calls ping() with toJSONified model', function () { const testPost = _.clone(testUtils.DataGenerator.Content.posts[2]); + const testAuthor = _.clone(testUtils.DataGenerator.Content.users[0]); const testModel = { toJSON: function () { return testPost; + }, + related: function (relation) { + return { + toJSON: function () { + if (relation === 'authors') { + return [testAuthor]; + } + + return []; + } + }; } }; @@ -52,7 +64,10 @@ describe('Slack', function () { listener(testModel); pingStub.calledOnce.should.be.true(); - pingStub.calledWith(testPost).should.be.true(); + pingStub.calledWith({ + ...testPost, + authors: [testAuthor] + }).should.be.true(); // Reset slack ping method resetSlack(); @@ -121,7 +136,10 @@ describe('Slack', function () { let requestUrl; let requestData; - const post = testUtils.DataGenerator.forKnex.createPost({slug: 'webhook-test'}); + const post = testUtils.DataGenerator.forKnex.createPost({ + slug: 'webhook-test', + html: `

Hello World!

This is a test post.

This is members only content.

` + }); urlService.getUrlByResourceId.withArgs(post.id, {absolute: true}).returns('http://myblog.com/' + post.slug + '/'); settingsCacheStub.withArgs('slack_url').returns(slackURL); @@ -140,7 +158,7 @@ describe('Slack', function () { requestUrl.should.equal(slackURL); requestData.attachments[0].title.should.equal(post.title); requestData.attachments[0].title_link.should.equal('http://myblog.com/webhook-test/'); - requestData.attachments[0].fields[0].value.should.equal('## markdown.'); + requestData.attachments[0].fields[0].value.should.equal('Hello World!This is a test post.'); requestData.attachments[0].should.not.have.property('author_name'); requestData.icon_url.should.equal('http://myblog.com/favicon.ico');