From 2c81d7c914ac0a2c3b7f1d6d0385479e61d15f18 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 1 Aug 2019 14:57:38 +0800 Subject: [PATCH] Updated v0.1 posts api to work with type column refs #10922 --- core/server/api/v0.1/posts.js | 67 +++++++++++++++++++++++--- core/test/regression/api/v0.1/utils.js | 2 + 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/core/server/api/v0.1/posts.js b/core/server/api/v0.1/posts.js index e7ba18f41c..45712d9797 100644 --- a/core/server/api/v0.1/posts.js +++ b/core/server/api/v0.1/posts.js @@ -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) + }; }); } diff --git a/core/test/regression/api/v0.1/utils.js b/core/test/regression/api/v0.1/utils.js index d6bbde81b8..3652a12d51 100644 --- a/core/test/regression/api/v0.1/utils.js +++ b/core/test/regression/api/v0.1/utils.js @@ -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')