2017-11-21 16:28:05 +03:00
|
|
|
'use strict';
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
// # Post Model
|
2017-11-21 16:28:05 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
uuid = require('uuid'),
|
|
|
|
moment = require('moment'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
ObjectId = require('bson-objectid'),
|
|
|
|
sequence = require('../utils/sequence'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
htmlToText = require('html-to-text'),
|
|
|
|
ghostBookshelf = require('./base'),
|
|
|
|
events = require('../events'),
|
|
|
|
config = require('../config'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
i18n = require('../i18n'),
|
2014-02-19 17:57:26 +04:00
|
|
|
Post,
|
2014-02-22 01:54:56 +04:00
|
|
|
Posts;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Post = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
tableName: 'posts',
|
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
relationships: ['tags'],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base model keeps only the columns, which are defined in the schema.
|
|
|
|
* We have to add the relations on top, otherwise bookshelf-relations
|
|
|
|
* has no access to the nested relations, which should be updated.
|
|
|
|
*/
|
|
|
|
permittedAttributes: function permittedAttributes() {
|
|
|
|
let filteredKeys = ghostBookshelf.Model.prototype.permittedAttributes.apply(this, arguments);
|
|
|
|
|
|
|
|
this.relationships.forEach((key) => {
|
|
|
|
filteredKeys.push(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return filteredKeys;
|
|
|
|
},
|
|
|
|
|
2017-05-22 11:24:59 +03:00
|
|
|
emitChange: function emitChange(event, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
var resourceType = this.get('page') ? 'page' : 'post';
|
2017-05-22 11:24:59 +03:00
|
|
|
|
|
|
|
if (options.usePreviousResourceType) {
|
2015-03-24 23:23:23 +03:00
|
|
|
resourceType = this.updated('page') ? 'page' : 'post';
|
|
|
|
}
|
2017-01-25 16:47:49 +03:00
|
|
|
|
2017-05-22 11:24:59 +03:00
|
|
|
events.emit(resourceType + '.' + event, this, options);
|
2015-03-24 23:23:23 +03:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
defaults: function defaults() {
|
2013-06-25 15:43:15 +04:00
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 23:01:46 +04:00
|
|
|
status: 'draft'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-04-19 16:53:23 +03:00
|
|
|
/**
|
|
|
|
* We update the tags after the Post was inserted.
|
|
|
|
* We update the tags before the Post was updated, see `onSaving` event.
|
|
|
|
* `onCreated` is called before `onSaved`.
|
|
|
|
*/
|
|
|
|
onCreated: function onCreated(model, response, options) {
|
2016-10-14 15:37:01 +03:00
|
|
|
var status = model.get('status');
|
2017-04-19 16:53:23 +03:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
model.emitChange('added');
|
2014-04-27 20:58:34 +04:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
if (['published', 'scheduled'].indexOf(status) !== -1) {
|
2017-05-22 11:24:59 +03:00
|
|
|
model.emitChange(status, {importing: options.importing});
|
2016-10-14 15:37:01 +03:00
|
|
|
}
|
2017-04-19 16:53:23 +03:00
|
|
|
},
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
onUpdated: function onUpdated(model) {
|
|
|
|
model.statusChanging = model.get('status') !== model.updated('status');
|
|
|
|
model.isPublished = model.get('status') === 'published';
|
|
|
|
model.isScheduled = model.get('status') === 'scheduled';
|
|
|
|
model.wasPublished = model.updated('status') === 'published';
|
|
|
|
model.wasScheduled = model.updated('status') === 'scheduled';
|
|
|
|
model.resourceTypeChanging = model.get('page') !== model.updated('page');
|
|
|
|
model.publishedAtHasChanged = model.hasDateChanged('published_at');
|
|
|
|
model.needsReschedule = model.publishedAtHasChanged && model.isScheduled;
|
|
|
|
|
|
|
|
// Handle added and deleted for post -> page or page -> post
|
|
|
|
if (model.resourceTypeChanging) {
|
|
|
|
if (model.wasPublished) {
|
2017-05-22 11:24:59 +03:00
|
|
|
model.emitChange('unpublished', {usePreviousResourceType: true});
|
2016-10-14 15:37:01 +03:00
|
|
|
}
|
2016-04-14 14:22:38 +03:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
if (model.wasScheduled) {
|
2017-05-22 11:24:59 +03:00
|
|
|
model.emitChange('unscheduled', {usePreviousResourceType: true});
|
2016-10-14 15:37:01 +03:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:24:59 +03:00
|
|
|
model.emitChange('deleted', {usePreviousResourceType: true});
|
2015-03-24 23:23:23 +03:00
|
|
|
model.emitChange('added');
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
if (model.isPublished) {
|
|
|
|
model.emitChange('published');
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
if (model.isScheduled) {
|
|
|
|
model.emitChange('scheduled');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (model.statusChanging) {
|
|
|
|
// CASE: was published before and is now e.q. draft or scheduled
|
|
|
|
if (model.wasPublished) {
|
|
|
|
model.emitChange('unpublished');
|
2016-04-14 14:22:38 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
// CASE: was draft or scheduled before and is now e.q. published
|
2015-03-24 23:23:23 +03:00
|
|
|
if (model.isPublished) {
|
|
|
|
model.emitChange('published');
|
|
|
|
}
|
2016-04-14 14:22:38 +03:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
// CASE: was draft or published before and is now e.q. scheduled
|
2016-04-14 14:22:38 +03:00
|
|
|
if (model.isScheduled) {
|
|
|
|
model.emitChange('scheduled');
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
// CASE: from scheduled to something
|
|
|
|
if (model.wasScheduled && !model.isScheduled && !model.isPublished) {
|
|
|
|
model.emitChange('unscheduled');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (model.isPublished) {
|
|
|
|
model.emitChange('published.edited');
|
2015-03-24 23:23:23 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
if (model.needsReschedule) {
|
|
|
|
model.emitChange('rescheduled');
|
|
|
|
}
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
// Fire edited if this wasn't a change between resourceType
|
|
|
|
model.emitChange('edited');
|
|
|
|
}
|
|
|
|
},
|
2016-09-19 16:45:36 +03:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
onDestroying: function onDestroying(model) {
|
|
|
|
if (model.previous('status') === 'published') {
|
|
|
|
model.emitChange('unpublished');
|
|
|
|
}
|
2016-10-14 15:37:01 +03:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
model.emitChange('deleted');
|
2013-08-20 22:52:44 +04:00
|
|
|
},
|
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
onSaving: function onSaving(model, attr, options) {
|
2016-04-14 14:22:38 +03:00
|
|
|
options = options || {};
|
|
|
|
|
2014-03-24 22:08:06 +04:00
|
|
|
var self = this,
|
2015-10-27 14:15:09 +03:00
|
|
|
title,
|
2016-03-23 16:17:01 +03:00
|
|
|
i,
|
|
|
|
// Variables to make the slug checking more readable
|
2017-11-21 16:28:05 +03:00
|
|
|
newTitle = this.get('title'),
|
|
|
|
newStatus = this.get('status'),
|
2016-05-19 14:49:22 +03:00
|
|
|
olderStatus = this.previous('status'),
|
2017-11-21 16:28:05 +03:00
|
|
|
prevTitle = this._previousAttributes.title,
|
|
|
|
prevSlug = this._previousAttributes.slug,
|
2016-06-02 20:11:54 +03:00
|
|
|
publishedAt = this.get('published_at'),
|
2017-04-06 19:49:59 +03:00
|
|
|
publishedAtHasChanged = this.hasDateChanged('published_at', {beforeWrite: true}),
|
2017-11-21 16:28:05 +03:00
|
|
|
mobiledoc = this.get('mobiledoc'),
|
|
|
|
tagsToSave,
|
|
|
|
ops = [];
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
// CASE: disallow published -> scheduled
|
|
|
|
// @TODO: remove when we have versioning based on updated_at
|
|
|
|
if (newStatus !== olderStatus && newStatus === 'scheduled' && olderStatus === 'published') {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.models.post.isAlreadyPublished', {key: 'status'})
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: both page and post can get scheduled
|
2016-04-14 14:22:38 +03:00
|
|
|
if (newStatus === 'scheduled') {
|
|
|
|
if (!publishedAt) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.models.post.valueCannotBeBlank', {key: 'published_at'})
|
|
|
|
}));
|
2016-04-14 14:22:38 +03:00
|
|
|
} else if (!moment(publishedAt).isValid()) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.models.post.valueCannotBeBlank', {key: 'published_at'})
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
// CASE: to schedule/reschedule a post, a minimum diff of x minutes is needed (default configured is 2minutes)
|
2017-05-12 15:56:40 +03:00
|
|
|
} else if (
|
|
|
|
publishedAtHasChanged &&
|
|
|
|
moment(publishedAt).isBefore(moment().add(config.get('times').cannotScheduleAPostBeforeInMinutes, 'minutes')) &&
|
|
|
|
!options.importing
|
|
|
|
) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.ValidationError({
|
|
|
|
message: i18n.t('errors.models.post.expectedPublishedAtInFuture', {
|
2016-09-13 18:41:14 +03:00
|
|
|
cannotScheduleAPostBeforeInMinutes: config.get('times').cannotScheduleAPostBeforeInMinutes
|
2016-05-19 14:49:22 +03:00
|
|
|
})
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2016-04-14 14:22:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
// CASE: detect lowercase/uppercase tag slugs
|
|
|
|
if (!_.isUndefined(this.get('tags')) && !_.isNull(this.get('tags'))) {
|
|
|
|
tagsToSave = [];
|
|
|
|
|
2016-06-02 20:11:54 +03:00
|
|
|
// and deduplicate upper/lowercase tags
|
2017-11-21 16:28:05 +03:00
|
|
|
_.each(this.get('tags'), function each(item) {
|
|
|
|
for (i = 0; i < tagsToSave.length; i = i + 1) {
|
|
|
|
if (tagsToSave[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
|
2016-06-02 20:11:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-03-24 22:08:06 +04:00
|
|
|
}
|
2014-05-23 23:32:14 +04:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
tagsToSave.push(item);
|
2016-06-02 20:11:54 +03:00
|
|
|
});
|
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
this.set('tags', tagsToSave);
|
2016-06-02 20:11:54 +03:00
|
|
|
}
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
ghostBookshelf.Model.prototype.onSaving.call(this, model, attr, options);
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2016-09-26 16:23:49 +03:00
|
|
|
if (mobiledoc) {
|
2017-08-31 13:09:02 +03:00
|
|
|
this.set('html', utils.mobiledocConverter.render(JSON.parse(mobiledoc)));
|
2016-09-26 16:23:49 +03:00
|
|
|
}
|
2017-04-11 11:55:36 +03:00
|
|
|
|
replace custom showdown fork with markdown-it (#8451)
refs https://github.com/TryGhost/Ghost-Admin/pull/690, closes #1501, closes #2093, closes #4592, closes #4627, closes #4659, closes #5039, closes #5237, closes #5587, closes #5625, closes #5632, closes #5822, closes #5939, closes #6840, closes #7183, closes #7536
- replace custom showdown fork with markdown-it
- swaps showdown for markdown-it when rendering markdown
- match existing header ID behaviour
- allow headers without a space after the #s
- add duplicate header ID handling
- remove legacy markdown spec
- move markdown-it setup into markdown-converter util
- update mobiledoc specs to match markdown-it newline behaviour
- update data-generator HTML to match markdown-it newline behaviour
- fix Post "converts html to plaintext" test
- update rss spec to match markdown-it newline behaviour
- close almost all related showdown bugs
2017-05-15 19:48:14 +03:00
|
|
|
if (this.hasChanged('html') || !this.get('plaintext')) {
|
2017-04-11 11:55:36 +03:00
|
|
|
this.set('plaintext', htmlToText.fromString(this.get('html'), {
|
|
|
|
wordwrap: 80,
|
|
|
|
ignoreImage: true,
|
|
|
|
hideLinkHrefIfSameAsText: true,
|
|
|
|
preserveNewlines: true,
|
|
|
|
returnDomByDefault: true,
|
|
|
|
uppercaseHeadings: false
|
|
|
|
}));
|
|
|
|
}
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-01-07 00:17:20 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
2017-05-23 19:18:13 +03:00
|
|
|
if (!options.importing) {
|
|
|
|
title = this.get('title') || i18n.t('errors.models.post.untitled');
|
|
|
|
this.set('title', _.toString(title).trim());
|
|
|
|
}
|
2013-08-30 08:18:55 +04:00
|
|
|
|
2015-09-25 19:11:22 +03:00
|
|
|
// ### Business logic for published_at and published_by
|
|
|
|
// If the current status is 'published' and published_at is not set, set it to now
|
2016-04-14 14:22:38 +03:00
|
|
|
if (newStatus === 'published' && !publishedAt) {
|
2015-09-25 19:11:22 +03:00
|
|
|
this.set('published_at', new Date());
|
|
|
|
}
|
2015-02-22 00:04:00 +03:00
|
|
|
|
2015-09-25 19:11:22 +03:00
|
|
|
// If the current status is 'published' and the status has just changed ensure published_by is set correctly
|
2016-04-14 14:22:38 +03:00
|
|
|
if (newStatus === 'published' && this.hasChanged('status')) {
|
2015-02-22 00:04:00 +03:00
|
|
|
// unless published_by is set and we're importing, set published_by to contextUser
|
|
|
|
if (!(this.get('published_by') && options.importing)) {
|
2014-08-09 23:16:54 +04:00
|
|
|
this.set('published_by', this.contextUser(options));
|
|
|
|
}
|
2015-09-25 19:11:22 +03:00
|
|
|
} else {
|
|
|
|
// In any other case (except import), `published_by` should not be changed
|
|
|
|
if (this.hasChanged('published_by') && !options.importing) {
|
|
|
|
this.set('published_by', this.previous('published_by'));
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-15 20:11:15 +04:00
|
|
|
|
2016-03-23 16:17:01 +03:00
|
|
|
// If a title is set, not the same as the old title, a draft post, and has never been published
|
2016-04-14 14:22:38 +03:00
|
|
|
if (prevTitle !== undefined && newTitle !== prevTitle && newStatus === 'draft' && !publishedAt) {
|
2017-04-19 16:53:23 +03:00
|
|
|
ops.push(function updateSlug() {
|
|
|
|
// Pass the new slug through the generator to strip illegal characters, detect duplicates
|
|
|
|
return ghostBookshelf.Model.generateSlug(Post, self.get('title'),
|
2015-09-23 13:54:56 +03:00
|
|
|
{status: 'all', transacting: options.transacting, importing: options.importing})
|
2017-04-19 16:53:23 +03:00
|
|
|
.then(function then(slug) {
|
|
|
|
// After the new slug is found, do another generate for the old title to compare it to the old slug
|
|
|
|
return ghostBookshelf.Model.generateSlug(Post, prevTitle,
|
|
|
|
{status: 'all', transacting: options.transacting, importing: options.importing}
|
|
|
|
).then(function then(prevTitleSlug) {
|
|
|
|
// If the old slug is the same as the slug that was generated from the old title
|
|
|
|
// then set a new slug. If it is not the same, means was set by the user
|
|
|
|
if (prevTitleSlug === prevSlug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
}
|
|
|
|
});
|
2016-03-23 16:17:01 +03:00
|
|
|
});
|
2017-04-19 16:53:23 +03:00
|
|
|
});
|
2016-03-23 16:17:01 +03:00
|
|
|
} else {
|
2017-04-19 16:53:23 +03:00
|
|
|
ops.push(function updateSlug() {
|
|
|
|
// If any of the attributes above were false, set initial slug and check to see if slug was changed by the user
|
|
|
|
if (self.hasChanged('slug') || !self.get('slug')) {
|
|
|
|
// Pass the new slug through the generator to strip illegal characters, detect duplicates
|
|
|
|
return ghostBookshelf.Model.generateSlug(Post, self.get('slug') || self.get('title'),
|
2016-03-23 16:17:01 +03:00
|
|
|
{status: 'all', transacting: options.transacting, importing: options.importing})
|
2017-04-19 16:53:23 +03:00
|
|
|
.then(function then(slug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2013-09-14 23:01:46 +04:00
|
|
|
}
|
2017-04-19 16:53:23 +03:00
|
|
|
|
|
|
|
return sequence(ops);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
onCreating: function onCreating(model, attr, options) {
|
2014-04-03 17:03:09 +04:00
|
|
|
options = options || {};
|
2013-06-18 02:59:08 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
// set any dynamic default properties
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!this.get('author_id')) {
|
2014-07-15 15:03:12 +04:00
|
|
|
this.set('author_id', this.contextUser(options));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-26 05:42:51 +04:00
|
|
|
|
2016-10-14 15:37:01 +03:00
|
|
|
ghostBookshelf.Model.prototype.onCreating.call(this, model, attr, options);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
// Relations
|
2015-06-24 12:00:24 +03:00
|
|
|
author: function author() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'author_id');
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
created_by: function createdBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'created_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
updated_by: function updatedBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'updated_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
published_by: function publishedBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'published_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
tags: function tags() {
|
2015-09-01 03:51:56 +03:00
|
|
|
return this.belongsToMany('Tag').withPivot('sort_order').query('orderBy', 'sort_order', 'ASC');
|
2014-02-25 00:28:18 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
fields: function fields() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.morphMany('AppField', 'relatable');
|
2014-04-22 05:08:11 +04:00
|
|
|
},
|
|
|
|
|
2016-07-18 23:21:47 +03:00
|
|
|
defaultColumnsToFetch: function defaultColumnsToFetch() {
|
|
|
|
return ['id', 'published_at', 'slug', 'author_id'];
|
|
|
|
},
|
2017-05-30 13:40:39 +03:00
|
|
|
/**
|
|
|
|
* If the `formats` option is not used, we return `html` be default.
|
|
|
|
* Otherwise we return what is requested e.g. `?formats=mobiledoc,plaintext`
|
|
|
|
*/
|
|
|
|
formatsToJSON: function formatsToJSON(attrs, options) {
|
|
|
|
var defaultFormats = ['html'],
|
|
|
|
formatsToKeep = options.formats || defaultFormats;
|
|
|
|
|
|
|
|
// Iterate over all known formats, and if they are not in the keep list, remove them
|
|
|
|
_.each(Post.allowedFormats, function (format) {
|
|
|
|
if (formatsToKeep.indexOf(format) === -1) {
|
|
|
|
delete attrs[format];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
2016-07-18 23:21:47 +03:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
toJSON: function toJSON(options) {
|
2015-07-04 21:27:23 +03:00
|
|
|
options = options || {};
|
|
|
|
|
2017-07-27 10:55:23 +03:00
|
|
|
var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options),
|
|
|
|
oldPostId = attrs.amp,
|
|
|
|
commentId;
|
2014-04-22 05:08:11 +04:00
|
|
|
|
2017-05-30 13:40:39 +03:00
|
|
|
attrs = this.formatsToJSON(attrs, options);
|
|
|
|
|
2015-07-04 21:27:23 +03:00
|
|
|
if (!options.columns || (options.columns && options.columns.indexOf('author') > -1)) {
|
|
|
|
attrs.author = attrs.author || attrs.author_id;
|
|
|
|
delete attrs.author_id;
|
|
|
|
}
|
2017-07-31 12:00:03 +03:00
|
|
|
// If the current column settings allow it...
|
|
|
|
if (!options.columns || (options.columns && options.columns.indexOf('primary_tag') > -1)) {
|
2017-08-24 15:07:19 +03:00
|
|
|
// ... attach a computed property of primary_tag which is the first tag if it is public, else null
|
|
|
|
if (attrs.tags && attrs.tags.length > 0 && attrs.tags[0].visibility === 'public') {
|
|
|
|
attrs.primary_tag = attrs.tags[0];
|
|
|
|
} else {
|
|
|
|
attrs.primary_tag = null;
|
|
|
|
}
|
2017-07-31 12:00:03 +03:00
|
|
|
}
|
2015-07-04 21:27:23 +03:00
|
|
|
|
|
|
|
if (!options.columns || (options.columns && options.columns.indexOf('url') > -1)) {
|
2016-09-09 13:23:47 +03:00
|
|
|
attrs.url = utils.url.urlPathForPost(attrs);
|
2015-07-04 21:27:23 +03:00
|
|
|
}
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
2017-07-27 10:55:23 +03:00
|
|
|
if (oldPostId) {
|
2017-09-12 18:03:47 +03:00
|
|
|
// CASE: You create a new post on 1.X, you enable disqus. You export your content, you import your content on a different instance.
|
|
|
|
// This time, the importer remembers your old post id in the amp field as ObjectId.
|
|
|
|
if (ObjectId.isValid(oldPostId)) {
|
2017-07-27 10:55:23 +03:00
|
|
|
commentId = oldPostId;
|
2017-09-12 18:03:47 +03:00
|
|
|
} else {
|
|
|
|
oldPostId = Number(oldPostId);
|
|
|
|
|
|
|
|
// CASE: You import an old post id from your LTS blog. Stored in the amp field.
|
|
|
|
if (!isNaN(oldPostId)) {
|
|
|
|
commentId = oldPostId.toString();
|
|
|
|
} else {
|
|
|
|
commentId = attrs.id;
|
|
|
|
}
|
2017-07-27 10:55:23 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commentId = attrs.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we remember the old post id because of disqus
|
|
|
|
attrs.comment_id = commentId;
|
|
|
|
|
2014-04-22 05:08:11 +04:00
|
|
|
return attrs;
|
2015-11-11 20:52:44 +03:00
|
|
|
},
|
|
|
|
enforcedFilters: function enforcedFilters() {
|
|
|
|
return this.isPublicContext() ? 'status:published' : null;
|
|
|
|
},
|
|
|
|
defaultFilters: function defaultFilters() {
|
2016-04-14 18:54:49 +03:00
|
|
|
if (this.isInternalContext()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-11-11 20:52:44 +03:00
|
|
|
return this.isPublicContext() ? 'page:false' : 'page:false+status:published';
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
}, {
|
2017-05-31 17:46:29 +03:00
|
|
|
allowedFormats: ['mobiledoc', 'html', 'plaintext', 'amp'],
|
2017-05-30 13:40:39 +03:00
|
|
|
|
2015-06-17 16:55:39 +03:00
|
|
|
orderDefaultOptions: function orderDefaultOptions() {
|
|
|
|
return {
|
|
|
|
status: 'ASC',
|
|
|
|
published_at: 'DESC',
|
|
|
|
updated_at: 'DESC',
|
|
|
|
id: 'DESC'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-07-15 13:04:10 +03:00
|
|
|
orderDefaultRaw: function () {
|
|
|
|
return '' +
|
|
|
|
'CASE WHEN posts.status = \'scheduled\' THEN 1 ' +
|
|
|
|
'WHEN posts.status = \'draft\' THEN 2 ' +
|
|
|
|
'ELSE 3 END ASC,' +
|
|
|
|
'posts.published_at DESC,' +
|
|
|
|
'posts.updated_at DESC,' +
|
|
|
|
'posts.id DESC';
|
|
|
|
},
|
|
|
|
|
2015-11-11 22:31:52 +03:00
|
|
|
/**
|
|
|
|
* @deprecated in favour of filter
|
|
|
|
*/
|
|
|
|
processOptions: function processOptions(options) {
|
|
|
|
if (!options.staticPages && !options.status) {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the only place that 'options.where' is set now
|
|
|
|
options.where = {statements: []};
|
|
|
|
|
2015-06-17 16:55:39 +03:00
|
|
|
// Step 4: Setup filters (where clauses)
|
2015-11-11 20:52:44 +03:00
|
|
|
if (options.staticPages && options.staticPages !== 'all') {
|
2015-06-17 16:55:39 +03:00
|
|
|
// convert string true/false to boolean
|
|
|
|
if (!_.isBoolean(options.staticPages)) {
|
2016-06-11 21:23:27 +03:00
|
|
|
options.staticPages = _.includes(['true', '1'], options.staticPages);
|
2015-06-17 16:55:39 +03:00
|
|
|
}
|
2015-11-11 22:31:52 +03:00
|
|
|
options.where.statements.push({prop: 'page', op: '=', value: options.staticPages});
|
|
|
|
delete options.staticPages;
|
|
|
|
} else if (options.staticPages === 'all') {
|
|
|
|
options.where.statements.push({prop: 'page', op: 'IN', value: [true, false]});
|
|
|
|
delete options.staticPages;
|
2015-06-17 16:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unless `all` is passed as an option, filter on
|
|
|
|
// the status provided.
|
2015-11-11 20:52:44 +03:00
|
|
|
if (options.status && options.status !== 'all') {
|
2015-06-17 16:55:39 +03:00
|
|
|
// make sure that status is valid
|
2016-05-19 14:49:22 +03:00
|
|
|
options.status = _.includes(['published', 'draft', 'scheduled'], options.status) ? options.status : 'published';
|
2015-11-11 22:31:52 +03:00
|
|
|
options.where.statements.push({prop: 'status', op: '=', value: options.status});
|
|
|
|
delete options.status;
|
2015-11-11 20:52:44 +03:00
|
|
|
} else if (options.status === 'all') {
|
2016-05-19 14:49:22 +03:00
|
|
|
options.where.statements.push({prop: 'status', op: 'IN', value: ['published', 'draft', 'scheduled']});
|
2015-11-11 22:31:52 +03:00
|
|
|
delete options.status;
|
2015-06-17 16:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
permittedOptions: function permittedOptions(methodName) {
|
2014-05-06 05:45:08 +04:00
|
|
|
var options = ghostBookshelf.Model.permittedOptions(),
|
|
|
|
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
|
|
validOptions = {
|
2017-11-14 02:18:59 +03:00
|
|
|
findOne: ['columns', 'importing', 'withRelated', 'require'],
|
2015-10-22 15:49:15 +03:00
|
|
|
findPage: ['page', 'limit', 'columns', 'filter', 'order', 'status', 'staticPages'],
|
2017-11-14 02:18:59 +03:00
|
|
|
findAll: ['columns', 'filter']
|
2014-05-06 05:45:08 +04:00
|
|
|
};
|
|
|
|
|
2017-05-30 13:40:39 +03:00
|
|
|
// The post model additionally supports having a formats option
|
|
|
|
options.push('formats');
|
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-04-19 11:59:09 +03:00
|
|
|
* Manually add 'tags' attribute since it's not in the schema and call parent.
|
|
|
|
*
|
2014-05-06 05:45:08 +04:00
|
|
|
* @param {Object} data Has keys representing the model's attributes/fields in the database.
|
|
|
|
* @return {Object} The filtered results of the passed in data, containing only what's allowed in the schema.
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
filterData: function filterData(data) {
|
2017-04-19 11:59:09 +03:00
|
|
|
var filteredData = ghostBookshelf.Model.filterData.apply(this, arguments),
|
2017-11-21 16:28:05 +03:00
|
|
|
extraData = _.pick(data, this.prototype.relationships);
|
2014-05-06 05:45:08 +04:00
|
|
|
|
2017-04-19 11:59:09 +03:00
|
|
|
_.merge(filteredData, extraData);
|
2014-05-06 05:45:08 +04:00
|
|
|
return filteredData;
|
|
|
|
},
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// ## Model Data Functions
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find One
|
|
|
|
* @extends ghostBookshelf.Model.findOne to handle post status
|
|
|
|
* **See:** [ghostBookshelf.Model.findOne](base.js.html#Find%20One)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
findOne: function findOne(data, options) {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
options = options || {};
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2015-06-25 21:56:27 +03:00
|
|
|
data = _.defaults(data || {}, {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
status: 'published'
|
2015-06-25 21:56:27 +03:00
|
|
|
});
|
2013-06-09 03:39:24 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (data.status === 'all') {
|
|
|
|
delete data.status;
|
2013-06-01 18:47:41 +04:00
|
|
|
}
|
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
options.withRelated = _.union(options.withRelated, options.include);
|
2015-01-07 20:25:32 +03:00
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
return ghostBookshelf.Model.findOne.call(this, data, options);
|
2013-09-13 17:29:59 +04:00
|
|
|
},
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Edit
|
2017-04-19 16:53:23 +03:00
|
|
|
* Fetches and saves to Post. See model.Base.edit
|
|
|
|
*
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @extends ghostBookshelf.Model.edit to handle returning the full object and manage _updatedAttributes
|
|
|
|
* **See:** [ghostBookshelf.Model.edit](base.js.html#edit)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
edit: function edit(data, options) {
|
2017-11-21 16:28:05 +03:00
|
|
|
let opts = _.cloneDeep(options || {});
|
|
|
|
|
|
|
|
const editPost = () => {
|
|
|
|
opts.forUpdate = true;
|
2017-04-19 16:53:23 +03:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
return ghostBookshelf.Model.edit.call(this, data, opts)
|
|
|
|
.then((post) => {
|
|
|
|
return this.findOne({status: 'all', id: opts.id}, opts)
|
|
|
|
.then((found) => {
|
2017-04-19 16:53:23 +03:00
|
|
|
if (found) {
|
|
|
|
// Pass along the updated attributes for checking status changes
|
|
|
|
found._updatedAttributes = post._updatedAttributes;
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-11-21 16:28:05 +03:00
|
|
|
};
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
if (!opts.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
opts.transacting = transacting;
|
|
|
|
return editPost();
|
|
|
|
});
|
2017-04-19 16:53:23 +03:00
|
|
|
}
|
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
return editPost();
|
2013-11-03 21:13:19 +04:00
|
|
|
},
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add
|
|
|
|
* @extends ghostBookshelf.Model.add to handle returning the full object
|
|
|
|
* **See:** [ghostBookshelf.Model.add](base.js.html#add)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
add: function add(data, options) {
|
2017-11-21 16:28:05 +03:00
|
|
|
let opts = _.cloneDeep(options || {});
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
const addPost = (() => {
|
|
|
|
return ghostBookshelf.Model.add.call(this, data, opts)
|
|
|
|
.then((post) => {
|
|
|
|
return this.findOne({status: 'all', id: post.id}, opts);
|
|
|
|
});
|
2013-09-13 17:29:59 +04:00
|
|
|
});
|
2017-11-21 16:28:05 +03:00
|
|
|
|
|
|
|
if (!opts.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
opts.transacting = transacting;
|
|
|
|
|
|
|
|
return addPost();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return addPost();
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function destroy(options) {
|
|
|
|
let opts = _.cloneDeep(options || {});
|
|
|
|
|
|
|
|
const destroyPost = () => {
|
|
|
|
return ghostBookshelf.Model.destroy.call(this, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!opts.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
opts.transacting = transacting;
|
|
|
|
return destroyPost();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return destroyPost();
|
2013-09-27 14:56:20 +04:00
|
|
|
},
|
2014-04-03 17:03:09 +04:00
|
|
|
|
2014-07-23 12:32:27 +04:00
|
|
|
/**
|
|
|
|
* ### destroyByAuthor
|
|
|
|
* @param {[type]} options has context and id. Context is the user doing the destroy, id is the user to destroy
|
|
|
|
*/
|
2017-11-21 16:28:05 +03:00
|
|
|
destroyByAuthor: function destroyByAuthor(options) {
|
|
|
|
let opts = _.cloneDeep(options || {});
|
|
|
|
|
|
|
|
let postCollection = Posts.forge(),
|
|
|
|
authorId = opts.id;
|
2014-07-23 12:32:27 +04:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
opts = this.filterOptions(opts, 'destroyByAuthor');
|
2016-03-02 07:42:01 +03:00
|
|
|
|
|
|
|
if (!authorId) {
|
2017-11-21 16:28:05 +03:00
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.models.post.noUserFound')
|
|
|
|
});
|
2014-07-23 12:32:27 +04:00
|
|
|
}
|
2016-03-02 07:42:01 +03:00
|
|
|
|
2017-11-21 16:28:05 +03:00
|
|
|
const destroyPost = (() => {
|
|
|
|
return postCollection
|
|
|
|
.query('where', 'author_id', '=', authorId)
|
|
|
|
.fetch(opts)
|
|
|
|
.call('invokeThen', 'destroy', opts)
|
|
|
|
.catch((err) => {
|
|
|
|
throw new errors.GhostError({err: err});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!opts.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
opts.transacting = transacting;
|
|
|
|
return destroyPost();
|
2016-03-02 07:42:01 +03:00
|
|
|
});
|
2017-11-21 16:28:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return destroyPost();
|
|
|
|
},
|
2014-07-23 12:32:27 +04:00
|
|
|
|
2017-09-26 19:06:14 +03:00
|
|
|
permissible: function permissible(postModelOrId, action, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasAppPermission) {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
var self = this,
|
2014-05-14 05:49:07 +04:00
|
|
|
postModel = postModelOrId,
|
|
|
|
origArgs;
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
|
|
|
// If we passed in an id instead of a model, get the model
|
|
|
|
// then check the permissions
|
|
|
|
if (_.isNumber(postModelOrId) || _.isString(postModelOrId)) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Grab the original args without the first one
|
|
|
|
origArgs = _.toArray(arguments).slice(1);
|
2016-04-14 18:54:49 +03:00
|
|
|
|
2014-05-14 05:49:07 +04:00
|
|
|
// Get the actual post model
|
2015-06-14 18:58:49 +03:00
|
|
|
return this.findOne({id: postModelOrId, status: 'all'}).then(function then(foundPostModel) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Build up the original args but substitute with actual model
|
|
|
|
var newArgs = [foundPostModel].concat(origArgs);
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
return self.permissible.apply(self, newArgs);
|
2016-10-04 18:33:43 +03:00
|
|
|
});
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
}
|
|
|
|
|
2017-09-27 14:12:53 +03:00
|
|
|
function isChanging(attr) {
|
|
|
|
return unsafeAttrs[attr] && unsafeAttrs[attr] !== postModel.get(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function actorIsAuthor(loadedPermissions) {
|
|
|
|
return loadedPermissions.user && _.some(loadedPermissions.user.roles, {name: 'Author'});
|
|
|
|
}
|
|
|
|
|
|
|
|
function isOwner() {
|
|
|
|
return unsafeAttrs.author_id && unsafeAttrs.author_id === context.user;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actorIsAuthor(loadedPermissions) && action === 'edit' && isChanging('author_id')) {
|
|
|
|
hasUserPermission = false;
|
|
|
|
} else if (actorIsAuthor(loadedPermissions) && action === 'add') {
|
|
|
|
hasUserPermission = isOwner();
|
|
|
|
} else if (postModel) {
|
2014-05-14 05:49:07 +04:00
|
|
|
hasUserPermission = hasUserPermission || context.user === postModel.get('author_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUserPermission && hasAppPermission) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve();
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.models.post.notEnoughPermission')}));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Posts = ghostBookshelf.Collection.extend({
|
2015-12-02 13:06:44 +03:00
|
|
|
model: Post
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
2014-07-13 15:17:18 +04:00
|
|
|
Post: ghostBookshelf.model('Post', Post),
|
|
|
|
Posts: ghostBookshelf.collection('Posts', Posts)
|
2014-02-25 14:18:33 +04:00
|
|
|
};
|