Skip to content

Commit b2201d4

Browse files
rshbhgrgallouis
authored andcommitted
Removed formats from private posts in content api (TryGhost#10154)
closes TryGhost#10118 All behind a members labs switch for now * Added filter for member only content * Updated frame context * Cleaned up members content check * Cleanup * Cleanup * Ensured members filtering works without include=tags * Protected against missing query * Fixed usage of include vs withRelated * Moved includeTags logic for members behind members flag to use tags * Cleanup * Update input serializer dependency Co-Authored-By: rishabhgrg <zrishabhgarg@gmail.com> * Added some explanations
1 parent 2f4b215 commit b2201d4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

core/server/api/v2/utils/serializers/input/posts.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const _ = require('lodash');
22
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts');
33
const url = require('./utils/url');
44
const utils = require('../../index');
5+
const labs = require('../../../../../services/labs');
56

67
function removeMobiledocFormat(frame) {
78
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
@@ -11,6 +12,14 @@ function removeMobiledocFormat(frame) {
1112
}
1213
}
1314

15+
function includeTags(frame) {
16+
if (!frame.options.withRelated) {
17+
frame.options.withRelated = ['tags'];
18+
} else if (!frame.options.withRelated.includes('tags')) {
19+
frame.options.withRelated.push('tags');
20+
}
21+
}
22+
1423
module.exports = {
1524
browse(apiConfig, frame) {
1625
debug('browse');
@@ -39,6 +48,10 @@ module.exports = {
3948
}
4049
// CASE: the content api endpoint for posts should not return mobiledoc
4150
removeMobiledocFormat(frame);
51+
if (labs.isSet('members')) {
52+
// CASE: Members needs to have the tags to check if its allowed access
53+
includeTags(frame);
54+
}
4255
}
4356

4457
debug(frame.options);
@@ -58,6 +71,10 @@ module.exports = {
5871
frame.data.page = false;
5972
// CASE: the content api endpoint for posts should not return mobiledoc
6073
removeMobiledocFormat(frame);
74+
if (labs.isSet('members')) {
75+
// CASE: Members needs to have the tags to check if its allowed access
76+
includeTags(frame);
77+
}
6178
}
6279

6380
debug(frame.options);

core/server/api/v2/utils/serializers/output/utils/mapper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const utils = require('../../../index');
22
const url = require('./url');
33
const date = require('./date');
4+
const members = require('./members');
45

56
const mapPost = (model, frame) => {
67
const jsonModel = model.toJSON(frame.options);
@@ -9,6 +10,7 @@ const mapPost = (model, frame) => {
910

1011
if (utils.isContentAPI(frame)) {
1112
date.forPost(jsonModel);
13+
members.forPost(jsonModel, frame);
1214
}
1315

1416
if (frame.options && frame.options.withRelated) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const labs = require('../../../../../../services/labs');
2+
const MEMBER_TAG = '#members';
3+
4+
// Checks if request should hide memnbers only content
5+
function hideMembersOnlyContent(attrs, frame) {
6+
let hasMemberTag = false;
7+
if (labs.isSet('members') && !frame.original.context.member && attrs.tags) {
8+
hasMemberTag = attrs.tags.find((tag) => {
9+
return (tag.name === MEMBER_TAG);
10+
});
11+
}
12+
return hasMemberTag;
13+
}
14+
15+
const forPost = (attrs, frame) => {
16+
const hideFormatsData = hideMembersOnlyContent(attrs, frame);
17+
if (hideFormatsData) {
18+
['plaintext', 'html'].forEach((field) => {
19+
attrs[field] = '';
20+
});
21+
}
22+
if (labs.isSet('members')) {
23+
// CASE: Members always adds tags, remove if the user didn't originally ask for them
24+
const origQuery = frame.original.query || {};
25+
if (!origQuery.include || !origQuery.include.includes('tags')) {
26+
delete attrs.tags;
27+
}
28+
}
29+
30+
return attrs;
31+
};
32+
33+
module.exports.forPost = forPost;

0 commit comments

Comments
 (0)