diff --git a/ghost/core/core/server/models/api-key.js b/ghost/core/core/server/models/api-key.js index 7263a2f467..90f1b2725f 100644 --- a/ghost/core/core/server/models/api-key.js +++ b/ghost/core/core/server/models/api-key.js @@ -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) { diff --git a/ghost/core/core/server/models/base/plugins/actions.js b/ghost/core/core/server/models/base/plugins/actions.js index ef819be974..6b2113195c 100644 --- a/ghost/core/core/server/models/base/plugins/actions.js +++ b/ghost/core/core/server/models/base/plugins/actions.js @@ -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: * diff --git a/ghost/core/core/server/models/label.js b/ghost/core/core/server/models/label.js index 0e6c44c820..e5a0f6bc56 100644 --- a/ghost/core/core/server/models/label.js +++ b/ghost/core/core/server/models/label.js @@ -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() { diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index d737878111..c5a3a79673 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -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'], diff --git a/ghost/core/core/server/models/tag.js b/ghost/core/core/server/models/tag.js index 97378b16ee..c5fc15ba41 100644 --- a/ghost/core/core/server/models/tag.js +++ b/ghost/core/core/server/models/tag.js @@ -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']; } diff --git a/ghost/core/core/server/models/user.js b/ghost/core/core/server/models/user.js index c39659ea98..5045417f3e 100644 --- a/ghost/core/core/server/models/user.js +++ b/ghost/core/core/server/models/user.js @@ -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() {