mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-27 18:52:14 +03:00
parent
08d83c1f53
commit
2c81d7c914
@ -16,6 +16,55 @@ const Promise = require('bluebird'),
|
||||
],
|
||||
unsafeAttrs = ['author_id', 'status', 'authors'];
|
||||
|
||||
const mongo = require('../v2/utils/serializers/input/utils/mongo.js');
|
||||
|
||||
/*
|
||||
* Replaces references of "page" in filters
|
||||
* with the correct column "type"
|
||||
*/
|
||||
function replacePageWithType(mongoJSON) {
|
||||
return mongo.mapKeysAndValues(mongoJSON, {
|
||||
key: {
|
||||
from: 'page',
|
||||
to: 'type'
|
||||
},
|
||||
values: [{
|
||||
from: false,
|
||||
to: 'post'
|
||||
}, {
|
||||
from: true,
|
||||
to: 'page'
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
function convertTypeToPage(model) {
|
||||
// Respect include param
|
||||
if (!Object.hasOwnProperty.call(model, 'type')) {
|
||||
return model;
|
||||
}
|
||||
model.page = model.type === 'page';
|
||||
delete model.type;
|
||||
return model;
|
||||
}
|
||||
|
||||
function convertPageToType(model) {
|
||||
if (!Object.hasOwnProperty.call(model, 'page')) {
|
||||
return model;
|
||||
}
|
||||
|
||||
if (model.page === true) {
|
||||
model.type = 'page';
|
||||
} else if (model.page === false) {
|
||||
model.type = 'post';
|
||||
} else {
|
||||
// This is to ensure that invalid page props generate a ValidationError
|
||||
model.type = 'UNKNOWN_PAGE_OPTION';
|
||||
}
|
||||
delete model.page;
|
||||
return model;
|
||||
}
|
||||
|
||||
let posts;
|
||||
|
||||
/**
|
||||
@ -58,10 +107,12 @@ posts = {
|
||||
* @returns {Object} options
|
||||
*/
|
||||
function modelQuery(options) {
|
||||
options.mongoTransformer = replacePageWithType;
|
||||
|
||||
return models.Post.findPage(options)
|
||||
.then(({data, meta}) => {
|
||||
return {
|
||||
posts: data.map(model => urlsForPost(model.id, model.toJSON(options), options)),
|
||||
posts: data.map(model => urlsForPost(model.id, model.toJSON(options), options)).map(convertTypeToPage),
|
||||
meta: meta
|
||||
};
|
||||
});
|
||||
@ -101,6 +152,8 @@ posts = {
|
||||
* @returns {Object} options
|
||||
*/
|
||||
function modelQuery(options) {
|
||||
options.mongoTransformer = replacePageWithType;
|
||||
|
||||
return models.Post.findOne(options.data, omit(options, ['data']))
|
||||
.then((model) => {
|
||||
if (!model) {
|
||||
@ -110,7 +163,7 @@ posts = {
|
||||
}
|
||||
|
||||
return {
|
||||
posts: [urlsForPost(model.id, model.toJSON(options), options)]
|
||||
posts: [urlsForPost(model.id, model.toJSON(options), options)].map(convertTypeToPage)
|
||||
};
|
||||
});
|
||||
}
|
||||
@ -148,7 +201,7 @@ posts = {
|
||||
* @returns {Object} options
|
||||
*/
|
||||
function modelQuery(options) {
|
||||
return models.Post.edit(options.data.posts[0], omit(options, ['data']))
|
||||
return models.Post.edit(options.data.posts.map(convertPageToType)[0], omit(options, ['data']))
|
||||
.then((model) => {
|
||||
if (!model) {
|
||||
return Promise.reject(new common.errors.NotFoundError({
|
||||
@ -166,7 +219,7 @@ posts = {
|
||||
}
|
||||
|
||||
return {
|
||||
posts: [post]
|
||||
posts: [post].map(convertTypeToPage)
|
||||
};
|
||||
});
|
||||
}
|
||||
@ -202,7 +255,7 @@ posts = {
|
||||
* @returns {Object} options
|
||||
*/
|
||||
function modelQuery(options) {
|
||||
return models.Post.add(options.data.posts[0], omit(options, ['data']))
|
||||
return models.Post.add(options.data.posts.map(convertPageToType)[0], omit(options, ['data']))
|
||||
.then((model) => {
|
||||
const post = urlsForPost(model.id, model.toJSON(options), options);
|
||||
|
||||
@ -211,7 +264,9 @@ posts = {
|
||||
post.statusChanged = true;
|
||||
}
|
||||
|
||||
return {posts: [post]};
|
||||
return {
|
||||
posts: [post].map(convertTypeToPage)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,8 @@ const expectedProperties = {
|
||||
.keys()
|
||||
// by default we only return html
|
||||
.without('mobiledoc', 'plaintext')
|
||||
.without('type')
|
||||
.concat('page')
|
||||
// swaps author_id to author, and always returns computed properties: url, comment_id, primary_tag, primary_author
|
||||
.without('author_id').concat('author', 'url', 'primary_tag', 'primary_author')
|
||||
.without('canonical_url')
|
||||
|
Loading…
Reference in New Issue
Block a user