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

Cleaned up multiple implementations of getAction

refs https://github.com/TryGhost/Toolbox/issues/356

- we had a function called `getAction` in every model where we were
  collecting CRUD actions to store in the DB
- this function has the same boilerplate code - make sure it's not
  internal and then construct the object to return
- as we add more actions to more models, we probably want to pull this
  out and just configure the things specific to the model
- this commit pulls out the function into the actions plugin and adds a
  couple of keys to the models to indicate we'd like to store CRUD
  actions, along with the model name
This commit is contained in:
Daniel Lockyer 2022-08-17 10:26:21 +02:00
parent 6c2aebbf0c
commit 7aecae11cc
No known key found for this signature in database
GPG key ID: D21186F0B47295AD
6 changed files with 41 additions and 90 deletions

View file

@ -6,6 +6,9 @@ const {Role} = require('./role');
const ApiKey = ghostBookshelf.Model.extend({
tableName: 'api_keys',
actionsCollectCRUD: true,
actionsResourceType: 'api_key',
defaults() {
const secret = security.secret.create(this.get('type'));
@ -53,24 +56,6 @@ const ApiKey = ghostBookshelf.Model.extend({
if (this.previous('secret') !== this.get('secret')) {
this.addAction(model, 'refreshed', options);
}
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'api_key',
actor_id: actor.id,
actor_type: actor.type
};
}
}, {
refreshSecret(data, options) {

View file

@ -7,6 +7,32 @@ const logging = require('@tryghost/logging');
*/
module.exports = function (Bookshelf) {
Bookshelf.Model = Bookshelf.Model.extend({
/**
* Constructs data to be stored in the database with info
* on particular actions
*/
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
if (!this.actionsCollectCRUD) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: this.actionsResourceType,
actor_id: actor.id,
actor_type: actor.type
};
},
/**
* @NOTE:
*

View file

@ -13,6 +13,9 @@ Label = ghostBookshelf.Model.extend({
tableName: 'labels',
actionsCollectCRUD: true,
actionsResourceType: 'label',
emitChange: function emitChange(event, options) {
const eventToTrigger = 'label' + '.' + event;
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
@ -62,24 +65,6 @@ Label = ghostBookshelf.Model.extend({
const attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
return attrs;
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'label',
actor_id: actor.id,
actor_type: actor.type
};
}
}, {
orderDefaultOptions: function orderDefaultOptions() {

View file

@ -38,6 +38,9 @@ Post = ghostBookshelf.Model.extend({
tableName: 'posts',
actionsCollectCRUD: true,
actionsResourceType: 'post',
/**
* @NOTE
*
@ -962,24 +965,6 @@ Post = ghostBookshelf.Model.extend({
delete options.status;
return filter;
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'post',
actor_id: actor.id,
actor_type: actor.type
};
}
}, {
allowedFormats: ['mobiledoc', 'html', 'plaintext'],

View file

@ -14,6 +14,9 @@ Tag = ghostBookshelf.Model.extend({
tableName: 'tags',
actionsCollectCRUD: true,
actionsResourceType: 'tag',
defaults: function defaults() {
return {
visibility: 'public'
@ -138,24 +141,6 @@ Tag = ghostBookshelf.Model.extend({
return attrs;
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'tag',
actor_id: actor.id,
actor_type: actor.type
};
},
defaultColumnsToFetch() {
return ['id'];
}

View file

@ -57,6 +57,9 @@ User = ghostBookshelf.Model.extend({
tableName: 'users',
actionsCollectCRUD: true,
actionsResourceType: 'user',
defaults: function defaults() {
return {
password: security.identifier.uid(50),
@ -363,24 +366,6 @@ User = ghostBookshelf.Model.extend({
delete options.status;
return filter;
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'user',
actor_id: actor.id,
actor_type: actor.type
};
}
}, {
orderDefaultOptions: function orderDefaultOptions() {