From b22151ac92e995d2b23d9a263591fd86c3fb074b Mon Sep 17 00:00:00 2001 From: Katharina Irrgang Date: Mon, 22 May 2017 10:24:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=20do=20not=20ping=20slack=20if?= =?UTF-8?q?=20we=20import=20content=20(#8476)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #7275 - forward options for events (post model only for now) --- core/server/data/slack/index.js | 24 +++++++++++++++--------- core/server/models/post.js | 17 ++++++++++------- core/test/unit/slack_spec.js | 10 ++++++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/core/server/data/slack/index.js b/core/server/data/slack/index.js index 845e107f6b..dfe5ebecc3 100644 --- a/core/server/data/slack/index.js +++ b/core/server/data/slack/index.js @@ -9,7 +9,6 @@ var https = require('https'), api = require('../../api/settings'), i18n = require('../../i18n'), schema = require('../schema').checks, - options, req, slackData = {}; @@ -44,8 +43,15 @@ function makeRequest(reqOptions, reqPayload) { req.end(); } -function ping(post) { - var message; +function ping(post, options) { + options = options || {}; + + var message, reqOptions; + + // CASE: do not ping slack if we import a database + if (options.importing) { + return Promise.resolve(); + } // If this is a post, we want to send the link of the post if (schema.isPost(post)) { @@ -79,20 +85,20 @@ function ping(post) { }; // fill the options for https request - options = url.parse(slackSettings.url); - options.method = 'POST'; - options.headers = {'Content-type': 'application/json'}; + reqOptions = url.parse(slackSettings.url); + reqOptions.method = 'POST'; + reqOptions.headers = {'Content-type': 'application/json'}; // with all the data we have, we're doing the request now - makeRequest(options, slackData); + makeRequest(reqOptions, slackData); } else { return; } }); } -function listener(model) { - ping(model.toJSON()); +function listener(model, options) { + ping(model.toJSON(), options); } function testPing() { diff --git a/core/server/models/post.js b/core/server/models/post.js index 3c30ee963d..9d2941626d 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -20,13 +20,16 @@ Post = ghostBookshelf.Model.extend({ tableName: 'posts', - emitChange: function emitChange(event, usePreviousResourceType) { + emitChange: function emitChange(event, options) { + options = options || {}; + var resourceType = this.get('page') ? 'page' : 'post'; - if (usePreviousResourceType) { + + if (options.usePreviousResourceType) { resourceType = this.updated('page') ? 'page' : 'post'; } - events.emit(resourceType + '.' + event, this); + events.emit(resourceType + '.' + event, this, options); }, defaults: function defaults() { @@ -47,7 +50,7 @@ Post = ghostBookshelf.Model.extend({ model.emitChange('added'); if (['published', 'scheduled'].indexOf(status) !== -1) { - model.emitChange(status); + model.emitChange(status, {importing: options.importing}); } return this.updateTags(model, response, options); @@ -87,14 +90,14 @@ Post = ghostBookshelf.Model.extend({ // Handle added and deleted for post -> page or page -> post if (model.resourceTypeChanging) { if (model.wasPublished) { - model.emitChange('unpublished', true); + model.emitChange('unpublished', {usePreviousResourceType: true}); } if (model.wasScheduled) { - model.emitChange('unscheduled', true); + model.emitChange('unscheduled', {usePreviousResourceType: true}); } - model.emitChange('deleted', true); + model.emitChange('deleted', {usePreviousResourceType: true}); model.emitChange('added'); if (model.isPublished) { diff --git a/core/test/unit/slack_spec.js b/core/test/unit/slack_spec.js index 2a58701ec8..45bf863786 100644 --- a/core/test/unit/slack_spec.js +++ b/core/test/unit/slack_spec.js @@ -294,5 +294,15 @@ describe('Slack', function () { done(); }); }); + + it('do not ping if content is imported', function (done) { + ping({}, {importing: true}).then(function () { + isPostStub.called.should.be.false(); + urlForSpy.called.should.be.false(); + settingsAPIStub.called.should.be.false(); + makeRequestSpy.called.should.be.false(); + done(); + }).catch(done); + }); }); });