mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
parent
08d83c1f53
commit
2c81d7c914
2 changed files with 63 additions and 6 deletions
|
@ -16,6 +16,55 @@ const Promise = require('bluebird'),
|
||||||
],
|
],
|
||||||
unsafeAttrs = ['author_id', 'status', 'authors'];
|
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;
|
let posts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,10 +107,12 @@ posts = {
|
||||||
* @returns {Object} options
|
* @returns {Object} options
|
||||||
*/
|
*/
|
||||||
function modelQuery(options) {
|
function modelQuery(options) {
|
||||||
|
options.mongoTransformer = replacePageWithType;
|
||||||
|
|
||||||
return models.Post.findPage(options)
|
return models.Post.findPage(options)
|
||||||
.then(({data, meta}) => {
|
.then(({data, meta}) => {
|
||||||
return {
|
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
|
meta: meta
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -101,6 +152,8 @@ posts = {
|
||||||
* @returns {Object} options
|
* @returns {Object} options
|
||||||
*/
|
*/
|
||||||
function modelQuery(options) {
|
function modelQuery(options) {
|
||||||
|
options.mongoTransformer = replacePageWithType;
|
||||||
|
|
||||||
return models.Post.findOne(options.data, omit(options, ['data']))
|
return models.Post.findOne(options.data, omit(options, ['data']))
|
||||||
.then((model) => {
|
.then((model) => {
|
||||||
if (!model) {
|
if (!model) {
|
||||||
|
@ -110,7 +163,7 @@ posts = {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
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
|
* @returns {Object} options
|
||||||
*/
|
*/
|
||||||
function modelQuery(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) => {
|
.then((model) => {
|
||||||
if (!model) {
|
if (!model) {
|
||||||
return Promise.reject(new common.errors.NotFoundError({
|
return Promise.reject(new common.errors.NotFoundError({
|
||||||
|
@ -166,7 +219,7 @@ posts = {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
posts: [post]
|
posts: [post].map(convertTypeToPage)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -202,7 +255,7 @@ posts = {
|
||||||
* @returns {Object} options
|
* @returns {Object} options
|
||||||
*/
|
*/
|
||||||
function modelQuery(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) => {
|
.then((model) => {
|
||||||
const post = urlsForPost(model.id, model.toJSON(options), options);
|
const post = urlsForPost(model.id, model.toJSON(options), options);
|
||||||
|
|
||||||
|
@ -211,7 +264,9 @@ posts = {
|
||||||
post.statusChanged = true;
|
post.statusChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {posts: [post]};
|
return {
|
||||||
|
posts: [post].map(convertTypeToPage)
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,8 @@ const expectedProperties = {
|
||||||
.keys()
|
.keys()
|
||||||
// by default we only return html
|
// by default we only return html
|
||||||
.without('mobiledoc', 'plaintext')
|
.without('mobiledoc', 'plaintext')
|
||||||
|
.without('type')
|
||||||
|
.concat('page')
|
||||||
// swaps author_id to author, and always returns computed properties: url, comment_id, primary_tag, primary_author
|
// 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('author_id').concat('author', 'url', 'primary_tag', 'primary_author')
|
||||||
.without('canonical_url')
|
.without('canonical_url')
|
||||||
|
|
Loading…
Add table
Reference in a new issue