0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

🎨 do not ping slack if we import content (#8476)

closes #7275

- forward options for events (post model only for now)
This commit is contained in:
Katharina Irrgang 2017-05-22 10:24:59 +02:00 committed by Aileen Nowak
parent c7bbaffec3
commit b22151ac92
3 changed files with 35 additions and 16 deletions

View file

@ -9,7 +9,6 @@ var https = require('https'),
api = require('../../api/settings'), api = require('../../api/settings'),
i18n = require('../../i18n'), i18n = require('../../i18n'),
schema = require('../schema').checks, schema = require('../schema').checks,
options,
req, req,
slackData = {}; slackData = {};
@ -44,8 +43,15 @@ function makeRequest(reqOptions, reqPayload) {
req.end(); req.end();
} }
function ping(post) { function ping(post, options) {
var message; 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 this is a post, we want to send the link of the post
if (schema.isPost(post)) { if (schema.isPost(post)) {
@ -79,20 +85,20 @@ function ping(post) {
}; };
// fill the options for https request // fill the options for https request
options = url.parse(slackSettings.url); reqOptions = url.parse(slackSettings.url);
options.method = 'POST'; reqOptions.method = 'POST';
options.headers = {'Content-type': 'application/json'}; reqOptions.headers = {'Content-type': 'application/json'};
// with all the data we have, we're doing the request now // with all the data we have, we're doing the request now
makeRequest(options, slackData); makeRequest(reqOptions, slackData);
} else { } else {
return; return;
} }
}); });
} }
function listener(model) { function listener(model, options) {
ping(model.toJSON()); ping(model.toJSON(), options);
} }
function testPing() { function testPing() {

View file

@ -20,13 +20,16 @@ Post = ghostBookshelf.Model.extend({
tableName: 'posts', tableName: 'posts',
emitChange: function emitChange(event, usePreviousResourceType) { emitChange: function emitChange(event, options) {
options = options || {};
var resourceType = this.get('page') ? 'page' : 'post'; var resourceType = this.get('page') ? 'page' : 'post';
if (usePreviousResourceType) {
if (options.usePreviousResourceType) {
resourceType = this.updated('page') ? 'page' : 'post'; resourceType = this.updated('page') ? 'page' : 'post';
} }
events.emit(resourceType + '.' + event, this); events.emit(resourceType + '.' + event, this, options);
}, },
defaults: function defaults() { defaults: function defaults() {
@ -47,7 +50,7 @@ Post = ghostBookshelf.Model.extend({
model.emitChange('added'); model.emitChange('added');
if (['published', 'scheduled'].indexOf(status) !== -1) { if (['published', 'scheduled'].indexOf(status) !== -1) {
model.emitChange(status); model.emitChange(status, {importing: options.importing});
} }
return this.updateTags(model, response, options); return this.updateTags(model, response, options);
@ -87,14 +90,14 @@ Post = ghostBookshelf.Model.extend({
// Handle added and deleted for post -> page or page -> post // Handle added and deleted for post -> page or page -> post
if (model.resourceTypeChanging) { if (model.resourceTypeChanging) {
if (model.wasPublished) { if (model.wasPublished) {
model.emitChange('unpublished', true); model.emitChange('unpublished', {usePreviousResourceType: true});
} }
if (model.wasScheduled) { if (model.wasScheduled) {
model.emitChange('unscheduled', true); model.emitChange('unscheduled', {usePreviousResourceType: true});
} }
model.emitChange('deleted', true); model.emitChange('deleted', {usePreviousResourceType: true});
model.emitChange('added'); model.emitChange('added');
if (model.isPublished) { if (model.isPublished) {

View file

@ -294,5 +294,15 @@ describe('Slack', function () {
done(); 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);
});
}); });
}); });