mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Extracted Bookshelf actions code to plugin
no issue - this commit extracts code related to Actions from the Base model into a separate plugin - `api-key.js` contained the exact same helper function as the Base model so that has been de-duplicated
This commit is contained in:
parent
14ffd0b9d9
commit
93c00b1ab7
4 changed files with 69 additions and 108 deletions
|
@ -1,7 +1,4 @@
|
|||
const omit = require('lodash/omit');
|
||||
const logging = require('@tryghost/logging');
|
||||
const errors = require('@tryghost/errors');
|
||||
const _ = require('lodash');
|
||||
const crypto = require('crypto');
|
||||
const ghostBookshelf = require('./base');
|
||||
const {Role} = require('./role');
|
||||
|
@ -29,50 +26,6 @@ const createSecret = (type) => {
|
|||
return crypto.randomBytes(bytes).toString('hex');
|
||||
};
|
||||
|
||||
const addAction = (model, event, options) => {
|
||||
if (!model.wasChanged()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// CASE: model does not support actions at all
|
||||
if (!model.getAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingAction = model.getAction(event, options);
|
||||
|
||||
// CASE: model does not support action for target event
|
||||
if (!existingAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const insert = (action) => {
|
||||
ghostBookshelf.model('Action')
|
||||
.add(action)
|
||||
.catch((err) => {
|
||||
if (_.isArray(err)) {
|
||||
err = err[0];
|
||||
}
|
||||
|
||||
logging.error(new errors.InternalServerError({
|
||||
err
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
if (options.transacting) {
|
||||
options.transacting.once('committed', (committed) => {
|
||||
if (!committed) {
|
||||
return;
|
||||
}
|
||||
|
||||
insert(existingAction);
|
||||
});
|
||||
} else {
|
||||
insert(existingAction);
|
||||
}
|
||||
};
|
||||
|
||||
const ApiKey = ghostBookshelf.Model.extend({
|
||||
tableName: 'api_keys',
|
||||
|
||||
|
@ -121,7 +74,7 @@ const ApiKey = ghostBookshelf.Model.extend({
|
|||
},
|
||||
onUpdated(model, attrs, options) {
|
||||
if (this.previous('secret') !== this.get('secret')) {
|
||||
addAction(model, 'refreshed', options);
|
||||
this.addAction(model, 'refreshed', options);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
63
core/server/models/base/actions.js
Normal file
63
core/server/models/base/actions.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
const _ = require('lodash');
|
||||
const errors = require('@tryghost/errors');
|
||||
const logging = require('@tryghost/logging');
|
||||
|
||||
module.exports = function (Bookshelf) {
|
||||
Bookshelf.Model = Bookshelf.Model.extend({
|
||||
/**
|
||||
* @NOTE:
|
||||
*
|
||||
* We add actions step by step and define how they should look like.
|
||||
* Each post update triggers a couple of events, which we don't want to add actions for.
|
||||
*
|
||||
* e.g. transform post to page triggers a handful of events including `post.deleted` and `page.added`
|
||||
*
|
||||
* We protect adding too many and uncontrolled events.
|
||||
*
|
||||
* We could embedd adding actions more nicely in the future e.g. plugin.
|
||||
*/
|
||||
addAction: (model, event, options) => {
|
||||
if (!model.wasChanged()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// CASE: model does not support actions at all
|
||||
if (!model.getAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingAction = model.getAction(event, options);
|
||||
|
||||
// CASE: model does not support action for target event
|
||||
if (!existingAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const insert = (action) => {
|
||||
Bookshelf.model('Action')
|
||||
.add(action)
|
||||
.catch((err) => {
|
||||
if (_.isArray(err)) {
|
||||
err = err[0];
|
||||
}
|
||||
|
||||
logging.error(new errors.InternalServerError({
|
||||
err
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
if (options.transacting) {
|
||||
options.transacting.once('committed', (committed) => {
|
||||
if (!committed) {
|
||||
return;
|
||||
}
|
||||
|
||||
insert(existingAction);
|
||||
});
|
||||
} else {
|
||||
insert(existingAction);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
|
@ -44,6 +44,8 @@ ghostBookshelf.plugin(plugins.hasPosts);
|
|||
|
||||
ghostBookshelf.plugin(require('./crud'));
|
||||
|
||||
ghostBookshelf.plugin(require('./actions'));
|
||||
|
||||
// Manages nested updates (relationships)
|
||||
ghostBookshelf.plugin('bookshelf-relations', {
|
||||
allowedOptions: ['context', 'importing', 'migrating'],
|
||||
|
|
|
@ -14,7 +14,6 @@ const ObjectId = require('bson-objectid');
|
|||
const debug = require('@tryghost/debug')('models:base');
|
||||
const db = require('../../data/db');
|
||||
const events = require('../../lib/common/events');
|
||||
const logging = require('@tryghost/logging');
|
||||
const errors = require('@tryghost/errors');
|
||||
const security = require('@tryghost/security');
|
||||
const schema = require('../../data/schema');
|
||||
|
@ -35,62 +34,6 @@ let proto;
|
|||
// Cache an instance of the base model prototype
|
||||
proto = ghostBookshelf.Model.prototype;
|
||||
|
||||
/**
|
||||
* @NOTE:
|
||||
*
|
||||
* We add actions step by step and define how they should look like.
|
||||
* Each post update triggers a couple of events, which we don't want to add actions for.
|
||||
*
|
||||
* e.g. transform post to page triggers a handful of events including `post.deleted` and `page.added`
|
||||
*
|
||||
* We protect adding too many and uncontrolled events.
|
||||
*
|
||||
* We could embedd adding actions more nicely in the future e.g. plugin.
|
||||
*/
|
||||
const addAction = (model, event, options) => {
|
||||
if (!model.wasChanged()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// CASE: model does not support actions at all
|
||||
if (!model.getAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingAction = model.getAction(event, options);
|
||||
|
||||
// CASE: model does not support action for target event
|
||||
if (!existingAction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const insert = (action) => {
|
||||
ghostBookshelf.model('Action')
|
||||
.add(action)
|
||||
.catch((err) => {
|
||||
if (_.isArray(err)) {
|
||||
err = err[0];
|
||||
}
|
||||
|
||||
logging.error(new errors.InternalServerError({
|
||||
err
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
if (options.transacting) {
|
||||
options.transacting.once('committed', (committed) => {
|
||||
if (!committed) {
|
||||
return;
|
||||
}
|
||||
|
||||
insert(existingAction);
|
||||
});
|
||||
} else {
|
||||
insert(existingAction);
|
||||
}
|
||||
};
|
||||
|
||||
// ## ghostBookshelf.Model
|
||||
// The Base Model which other Ghost objects will inherit from,
|
||||
// including some convenience functions as static properties on the model.
|
||||
|
@ -265,7 +208,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
},
|
||||
|
||||
onCreated(model, attrs, options) {
|
||||
addAction(model, 'added', options);
|
||||
this.addAction(model, 'added', options);
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -326,7 +269,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
},
|
||||
|
||||
onUpdated(model, attrs, options) {
|
||||
addAction(model, 'edited', options);
|
||||
this.addAction(model, 'edited', options);
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -402,7 +345,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
// @NOTE: Bookshelf returns ".changed = {empty...}" on destroying (https://github.com/bookshelf/bookshelf/issues/1943)
|
||||
Object.assign(model._changed, _.cloneDeep(model.changed));
|
||||
|
||||
addAction(model, 'deleted', options);
|
||||
this.addAction(model, 'deleted', options);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue