mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 09:22:49 +03:00
80fbfd7a85
no issue - the column addition/removal can be too slow for large sites - will be added back in 3.0 --- Revert "Fixed canary api for page/type column" This reverts commita5a7e7e919
. Revert "Updated frontend canary url config for page/type" This reverts commit19100ec5e6
. Revert "Updated canary api to handle type column correctly (#11006)" This reverts commitc3e8ba0523
. Revert "Ensured `page` filter works in routes.yaml" This reverts commit9037c19e50
. Revert "Replaced usage of mongo util with nql-map-key-values" This reverts commit8c5f1d0ef0
. Revert "Added shared nql-map-key-values module" This reverts commitef4fd4b8ef
. Revert "Ensured page prop is present on content api response" This reverts commitcfa0a0862b
. Revert "Fixed failing regression tests" This reverts commit9c2bb3811f
. Revert "Updated xmlrpc and slack service to use type column" This reverts commit44a02c7d36
. Revert "Updated v0.1 posts api to work with type column" This reverts commit2c81d7c914
. Revert "Removed updates to v0.1 specific code" This reverts commit08d83c1f53
. Revert "Added missing context from ValidationError" This reverts commitcd45ab4f54
. Revert "Renamed page->type in the page&posts serializers" This reverts commitdf99e724e3
. Revert "Added mongo helper to input serializers" This reverts commitfb8eadb4a8
. Revert "Passed mongoTransformer through to NQL" This reverts commit0ae3f0fdfc
. Revert "Permitted mongoTransformer option for read methods" This reverts commita89376bf26
. Revert "Updated the count plugin to reference the type column" This reverts commita52f15d3d3
. Revert "Updated hashes for db integrity check" This reverts commitbb6b337be3
. Revert "Remove page column and remaining references" This reverts commit9d7190d692
. Revert "Added type column to data generator" This reverts commite59806cb45
. Revert "Removed references to page column in rss tests" This reverts commit04d0f855de
. Revert "Removed page column references in validation tests" This reverts commitf0afbc5cc0
. Revert "Updated the post model to use the `type` column" This reverts commit1189bc823a
. Revert "Updated url service to use type column" This reverts commit61612ba8fd
. Revert "Updated the v2 api to deal with type column" This reverts commit57afb2de2b
. Revert "Added type property to post model defaults" This reverts commitdc3345b1c5
. Revert "Added type property to the default post fixtures" This reverts commit82d8c38033
. Revert "Added type column to posts table" This reverts commit9b85fc6a69
.
271 lines
9.1 KiB
JavaScript
271 lines
9.1 KiB
JavaScript
// # Posts API
|
|
// RESTful API for the Post resource
|
|
const Promise = require('bluebird'),
|
|
{omit, defaults} = require('lodash'),
|
|
pipeline = require('../../lib/promise/pipeline'),
|
|
localUtils = require('./utils'),
|
|
models = require('../../models'),
|
|
common = require('../../lib/common'),
|
|
{urlsForPost} = require('./decorators/urls'),
|
|
docName = 'posts',
|
|
/**
|
|
* @deprecated: `author`, will be removed in Ghost 3.0
|
|
*/
|
|
allowedIncludes = [
|
|
'created_by', 'updated_by', 'published_by', 'author', 'tags', 'fields', 'authors', 'authors.roles'
|
|
],
|
|
unsafeAttrs = ['author_id', 'status', 'authors'];
|
|
|
|
let posts;
|
|
|
|
/**
|
|
* ### Posts API Methods
|
|
*
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
|
*/
|
|
|
|
posts = {
|
|
/**
|
|
* ## Browse
|
|
* Find a paginated set of posts
|
|
*
|
|
* Will only return published posts unless we have an authenticated user and an alternative status
|
|
* parameter.
|
|
*
|
|
* Will return without static pages unless told otherwise
|
|
*
|
|
*
|
|
* @public
|
|
* @param {{context, page, limit, status, staticPages, tag, featured}} options (optional)
|
|
* @returns {Promise<Posts>} Posts Collection with Meta
|
|
*/
|
|
browse(options) {
|
|
const extraOptions = ['status', 'formats', 'absolute_urls'];
|
|
let permittedOptions,
|
|
tasks;
|
|
|
|
// Workaround to remove static pages from results
|
|
// TODO: rework after https://github.com/TryGhost/Ghost/issues/5151
|
|
if (options && options.context && (options.context.user || options.context.internal)) {
|
|
extraOptions.push('staticPages');
|
|
}
|
|
permittedOptions = localUtils.browseDefaultOptions.concat(extraOptions);
|
|
|
|
/**
|
|
* ### Model Query
|
|
* Make the call to the Model layer
|
|
* @param {Object} options
|
|
* @returns {Object} options
|
|
*/
|
|
function modelQuery(options) {
|
|
return models.Post.findPage(options)
|
|
.then(({data, meta}) => {
|
|
return {
|
|
posts: data.map(model => urlsForPost(model.id, model.toJSON(options), options)),
|
|
meta: meta
|
|
};
|
|
});
|
|
}
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
tasks = [
|
|
localUtils.validate(docName, {opts: permittedOptions}),
|
|
localUtils.convertOptions(allowedIncludes, models.Post.allowedFormats),
|
|
localUtils.handlePublicPermissions(docName, 'browse', unsafeAttrs),
|
|
modelQuery
|
|
];
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
return pipeline(tasks, options);
|
|
},
|
|
|
|
/**
|
|
* ## Read
|
|
* Find a post, by ID, UUID, or Slug
|
|
*
|
|
* @public
|
|
* @param {Object} options
|
|
* @return {Promise<Post>} Post
|
|
*/
|
|
read(options) {
|
|
const attrs = ['id', 'slug', 'status', 'uuid'],
|
|
// NOTE: the scheduler API uses the post API and forwards custom options
|
|
extraAllowedOptions = options.opts || ['formats', 'absolute_urls'];
|
|
|
|
let tasks;
|
|
|
|
/**
|
|
* ### Model Query
|
|
* Make the call to the Model layer
|
|
* @param {Object} options
|
|
* @returns {Object} options
|
|
*/
|
|
function modelQuery(options) {
|
|
return models.Post.findOne(options.data, omit(options, ['data']))
|
|
.then((model) => {
|
|
if (!model) {
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
}));
|
|
}
|
|
|
|
return {
|
|
posts: [urlsForPost(model.id, model.toJSON(options), options)]
|
|
};
|
|
});
|
|
}
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
tasks = [
|
|
localUtils.validate(docName, {attrs: attrs, opts: extraAllowedOptions}),
|
|
localUtils.convertOptions(allowedIncludes, models.Post.allowedFormats),
|
|
localUtils.handlePublicPermissions(docName, 'read', unsafeAttrs),
|
|
modelQuery
|
|
];
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
return pipeline(tasks, options);
|
|
},
|
|
|
|
/**
|
|
* ## Edit
|
|
* Update properties of a post
|
|
*
|
|
* @public
|
|
* @param {Post} object Post or specific properties to update
|
|
* @param {{id (required), context, include,...}} options
|
|
* @return {Promise(Post)} Edited Post
|
|
*/
|
|
edit(object, options) {
|
|
let tasks;
|
|
// NOTE: the scheduler API uses the post API and forwards custom options
|
|
const extraAllowedOptions = options.opts || [];
|
|
|
|
/**
|
|
* ### Model Query
|
|
* Make the call to the Model layer
|
|
* @param {Object} options
|
|
* @returns {Object} options
|
|
*/
|
|
function modelQuery(options) {
|
|
return models.Post.edit(options.data.posts[0], omit(options, ['data']))
|
|
.then((model) => {
|
|
if (!model) {
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
}));
|
|
}
|
|
|
|
const post = urlsForPost(model.id, model.toJSON(options), options);
|
|
|
|
// If previously was not published and now is (or vice versa), signal the change
|
|
// @TODO: `statusChanged` get's added for the API headers only. Reconsider this.
|
|
post.statusChanged = false;
|
|
if (model.previous('status') !== model.get('status')) {
|
|
post.statusChanged = true;
|
|
}
|
|
|
|
return {
|
|
posts: [post]
|
|
};
|
|
});
|
|
}
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
tasks = [
|
|
localUtils.validate(docName, {opts: localUtils.idDefaultOptions.concat(extraAllowedOptions)}),
|
|
localUtils.convertOptions(allowedIncludes),
|
|
localUtils.handlePermissions(docName, 'edit', unsafeAttrs),
|
|
modelQuery
|
|
];
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
return pipeline(tasks, object, options);
|
|
},
|
|
|
|
/**
|
|
* ## Add
|
|
* Create a new post along with any tags
|
|
*
|
|
* @public
|
|
* @param {Post} object
|
|
* @param {{context, include,...}} options
|
|
* @return {Promise(Post)} Created Post
|
|
*/
|
|
add(object, options) {
|
|
let tasks;
|
|
|
|
/**
|
|
* ### Model Query
|
|
* Make the call to the Model layer
|
|
* @param {Object} options
|
|
* @returns {Object} options
|
|
*/
|
|
function modelQuery(options) {
|
|
return models.Post.add(options.data.posts[0], omit(options, ['data']))
|
|
.then((model) => {
|
|
const post = urlsForPost(model.id, model.toJSON(options), options);
|
|
|
|
if (post.status === 'published') {
|
|
// When creating a new post that is published right now, signal the change
|
|
post.statusChanged = true;
|
|
}
|
|
|
|
return {posts: [post]};
|
|
});
|
|
}
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
tasks = [
|
|
localUtils.validate(docName),
|
|
localUtils.convertOptions(allowedIncludes),
|
|
localUtils.handlePermissions(docName, 'add', unsafeAttrs),
|
|
modelQuery
|
|
];
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
return pipeline(tasks, object, options);
|
|
},
|
|
|
|
/**
|
|
* ## Destroy
|
|
* Delete a post, cleans up tag relations, but not unused tags.
|
|
* You can only delete a post by `id`.
|
|
*
|
|
* @public
|
|
* @param {{id (required), context,...}} options
|
|
* @return {Promise}
|
|
*/
|
|
destroy(options) {
|
|
let tasks;
|
|
|
|
/**
|
|
* @function deletePost
|
|
* @param {Object} options
|
|
*/
|
|
function deletePost(options) {
|
|
const opts = defaults({require: true}, options);
|
|
|
|
return models.Post.destroy(opts).return(null)
|
|
.catch(models.Post.NotFoundError, () => {
|
|
throw new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
});
|
|
});
|
|
}
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
tasks = [
|
|
localUtils.validate(docName, {opts: localUtils.idDefaultOptions}),
|
|
localUtils.convertOptions(allowedIncludes),
|
|
localUtils.handlePermissions(docName, 'destroy', unsafeAttrs),
|
|
deletePost
|
|
];
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
return pipeline(tasks, options);
|
|
}
|
|
};
|
|
|
|
module.exports = posts;
|