2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Simplfied action trigger in base model

refs #10431

- the model layer triggers a couple of events on resource update
  e.g. post to page -> post.deleted, post.added
- the resource_type must be always "post", because "page" is not an official model (Bookshelf won't be able to resolve the resource anymore)
- the action streams looks very confusion if you see deleted and added actions when toggling the post to a static page
- therefor the easiest approach for now is to only store actions for: added, edited, deleted
- and we will add the context information asap
  - e.g. you will see that status was changed from "draft" to "published"
- we can also introduce extra published actions if we want
- relying on the internal event system right now makes things just more complicated and we want to keep it simple
This commit is contained in:
kirrg001 2019-02-06 22:05:03 +01:00
parent dbd3832967
commit cca3317a11
2 changed files with 70 additions and 42 deletions

View file

@ -87,6 +87,58 @@ ghostBookshelf.plugin('bookshelf-relations', {
// 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) => {
// CASE: model does not support actions at all
if (!model.getAction) {
return;
}
const action = model.getAction(event, options);
// CASE: model does not support action for target event
if (!action) {
return;
}
const insert = (action) => {
ghostBookshelf.model('Action')
.add(action)
.catch((err) => {
if (_.isArray(err)) {
err = err[0];
}
common.logging.error(new common.errors.InternalServerError({
err
}));
});
};
if (options.transacting) {
options.transacting.once('committed', (committed) => {
if (!committed) {
return;
}
insert(action);
});
} else {
insert(action);
}
};
// ## ghostBookshelf.Model
// The Base Model which other Ghost objects will inherit from,
// including some convenience functions as static properties on the model.
@ -127,40 +179,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
// @NOTE: Internal Ghost events. These are very granular e.g. post.published
common.events.emit(ghostEvent, model, _.omit(options, 'transacting'));
// CASE: model does not support actions at all
if (!model.getAction) {
return;
}
const action = model.getAction(ghostEvent, options);
// CASE: model does not support action for target event
if (!action) {
return;
}
/**
* @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.
*/
ghostBookshelf.model('Action')
.add(action)
.catch((err) => {
if (_.isArray(err)) {
err = err[0];
}
common.logging.error(new common.errors.InternalServerError({
err
}));
});
};
if (!options.transacting) {
@ -367,6 +385,18 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return Promise.resolve(this.onValidate(model, attr, options));
},
onCreated(model, attrs, options) {
addAction(model, 'added', options);
},
onUpdated(model, attrs, options) {
addAction(model, 'edited', options);
},
onDestroyed(model, options) {
addAction(model, 'deleted', options);
},
/**
* before we insert dates into the database, we have to normalize
* date format is now in each db the same

View file

@ -109,6 +109,8 @@ Post = ghostBookshelf.Model.extend({
},
onUpdated: function onUpdated(model, attrs, options) {
ghostBookshelf.Model.prototype.onUpdated.call(this, model, attrs, options);
model.statusChanging = model.get('status') !== model.previous('status');
model.isPublished = model.get('status') === 'published';
model.isScheduled = model.get('status') === 'scheduled';
@ -179,6 +181,8 @@ Post = ghostBookshelf.Model.extend({
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.call(this, model, options);
if (model.previous('status') === 'published') {
model.emitChange('unpublished', Object.assign({usePreviousAttribute: true}, options));
}
@ -627,13 +631,7 @@ Post = ghostBookshelf.Model.extend({
return filter;
},
getAction(ghostEvent, options) {
const supported = ['post.edited', 'post.deleted', 'post.added'];
if (!supported.includes(ghostEvent)) {
return;
}
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
@ -643,7 +641,7 @@ Post = ghostBookshelf.Model.extend({
// @TODO: implement context
return {
event: ghostEvent.replace(/^\w+\./, ''),
event: event,
resource_id: this.id || this.previous('id'),
resource_type: this.tableName.replace(/s$/, ''),
actor_id: actor.id,