0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Added tiers data for posts with non tiers visibility

refs https://github.com/TryGhost/Team/issues/1004

The `tiers` column for a post/page only contained data if its visibility is set to `tiers`, otherwise its empty. This is because originally the purpose of `tiers` column on `post` was to capture specific tiers with access to post.
The best way to ensure a consistent behavior for `tiers` column data on post is to update it to always contain list of all `tiers` that have access to post, and not just when the visibility is `tiers`. This means the value is set to all tiers when visibility is one of public|members, and only paid tiers when visibility is `paid`.  This change also allows on frontend to get all relevant `tiers` information for a post locally within post context instead of relying on additional information from outside.

This change -

- updates the output serializer for post/page to add all desired tiers manually in case of visibility is not `tiers`
- updates tests
This commit is contained in:
Rishabh 2022-03-02 21:30:22 +05:30 committed by Rishabh Garg
parent df27b1993f
commit eac732f620
12 changed files with 398 additions and 16 deletions

View file

@ -1,9 +1,15 @@
const mapper = require('./utils/mapper');
const gating = require('./utils/post-gating');
const membersService = require('../../../../../services/members');
module.exports = {
async read(model, apiConfig, frame) {
const emailPost = await mapper.mapPost(model, frame);
const tiersModels = await membersService.api.productRepository.list({
withRelated: ['monthlyPrice', 'yearlyPrice']
});
const tiers = tiersModels.data && tiersModels.data.map(tierModel => tierModel.toJSON());
const emailPost = await mapper.mapPost(model, frame, {tiers});
gating.forPost(emailPost, frame);
frame.response = {

View file

@ -1,5 +1,6 @@
const debug = require('@tryghost/debug')('api:canary:utils:serializers:output:pages');
const mapper = require('./utils/mapper');
const membersService = require('../../../../../services/members');
module.exports = {
async all(models, apiConfig, frame) {
@ -10,9 +11,15 @@ module.exports = {
return;
}
let pages = [];
const tiersModels = await membersService.api.productRepository.list({
withRelated: ['monthlyPrice', 'yearlyPrice']
});
const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : [];
if (models.meta) {
for (let model of models.data) {
let page = await mapper.mapPage(model, frame);
let page = await mapper.mapPage(model, frame, {tiers});
pages.push(page);
}
frame.response = {
@ -22,7 +29,7 @@ module.exports = {
return;
}
let page = await mapper.mapPage(models, frame);
let page = await mapper.mapPage(models, frame, {tiers});
frame.response = {
pages: [page]
};

View file

@ -1,5 +1,6 @@
const debug = require('@tryghost/debug')('api:canary:utils:serializers:output:posts');
const mapper = require('./utils/mapper');
const membersService = require('../../../../../services/members');
module.exports = {
async all(models, apiConfig, frame) {
@ -10,9 +11,14 @@ module.exports = {
return;
}
let posts = [];
const tiersModels = await membersService.api.productRepository.list({
withRelated: ['monthlyPrice', 'yearlyPrice']
});
const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : [];
if (models.meta) {
for (let model of models.data) {
let post = await mapper.mapPost(model, frame);
let post = await mapper.mapPost(model, frame, {tiers});
posts.push(post);
}
frame.response = {
@ -22,7 +28,7 @@ module.exports = {
return;
}
let post = await mapper.mapPost(models, frame);
let post = await mapper.mapPost(models, frame, {tiers});
frame.response = {
posts: [post]
};

View file

@ -1,8 +1,14 @@
const mapper = require('./utils/mapper');
const membersService = require('../../../../../services/members');
module.exports = {
async all(model, apiConfig, frame) {
const data = await mapper.mapPost(model, frame);
const tiersModels = await membersService.api.productRepository.list({
withRelated: ['monthlyPrice', 'yearlyPrice']
});
const tiers = tiersModels.data ? tiersModels.data.map(tierModel => tierModel.toJSON()) : [];
const data = await mapper.mapPost(model, frame, {tiers});
frame.response = {
preview: [data]
};

View file

@ -31,7 +31,8 @@ const mapTag = (model, frame) => {
return jsonModel;
};
const mapPost = async (model, frame) => {
const mapPost = async (model, frame, options = {}) => {
const {tiers: tiersData} = options || {};
const extendedOptions = Object.assign(_.cloneDeep(frame.options), {
extraProperties: ['canonical_url']
});
@ -45,13 +46,22 @@ const mapPost = async (model, frame) => {
// Attach tiers to custom nql visibility filter
if (labsService.isSet('multipleProducts')
&& jsonModel.visibility
&& !['members', 'public', 'paid', 'tiers'].includes(jsonModel.visibility)
) {
if (['members', 'public'].includes(jsonModel.visibility) && jsonModel.tiers) {
jsonModel.tiers = tiersData || [];
}
if (jsonModel.visibility === 'paid' && jsonModel.tiers) {
jsonModel.tiers = tiersData ? tiersData.filter(t => t.type === 'paid') : [];
}
if (!['members', 'public', 'paid', 'tiers'].includes(jsonModel.visibility)) {
const tiers = await postsService.getProductsFromVisibilityFilter(jsonModel.visibility);
jsonModel.visibility = 'tiers';
jsonModel.tiers = tiers;
}
}
if (utils.isContentAPI(frame)) {
// Content api v2 still expects page prop
@ -103,8 +113,8 @@ const mapPost = async (model, frame) => {
return jsonModel;
};
const mapPage = async (model, frame) => {
const jsonModel = await mapPost(model, frame);
const mapPage = async (model, frame, options) => {
const jsonModel = await mapPost(model, frame, options);
delete jsonModel.email_subject;
delete jsonModel.email_recipient_filter;

View file

@ -1,5 +1,6 @@
const should = require('should');
const supertest = require('supertest');
const moment = require('moment');
const _ = require('lodash');
const testUtils = require('../../utils');
const config = require('../../../core/shared/config');
@ -75,6 +76,88 @@ describe('Pages API', function () {
modelJson.posts_meta.feature_image_caption.should.eql(page.feature_image_caption);
});
it('Can include free and paid tiers for public page', async function () {
const publicPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'free-to-see',
visibility: 'public',
published_at: moment().add(15, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(publicPost, {context: {internal: true}});
const publicPostRes = await request
.get(localUtils.API.getApiQuery(`pages/${publicPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const publicPostData = publicPostRes.body.pages[0];
publicPostData.tiers.length.should.eql(2);
});
it('Can include free and paid tiers for members only page', async function () {
const membersPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'thou-shalt-not-be-seen',
visibility: 'members',
published_at: moment().add(45, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(membersPost, {context: {internal: true}});
const membersPostRes = await request
.get(localUtils.API.getApiQuery(`pages/${membersPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const membersPostData = membersPostRes.body.pages[0];
membersPostData.tiers.length.should.eql(2);
});
it('Can include only paid tier for paid page', async function () {
const paidPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'thou-shalt-be-paid-for',
visibility: 'paid',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(paidPost, {context: {internal: true}});
const paidPostRes = await request
.get(localUtils.API.getApiQuery(`pages/${paidPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const paidPostData = paidPostRes.body.pages[0];
paidPostData.tiers.length.should.eql(1);
});
it('Can include specific tier for page with tiers visibility', async function () {
const res = await request.get(localUtils.API.getApiQuery('products/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
const jsonResponse = res.body;
const paidTier = jsonResponse.products.find(p => p.type === 'paid');
const tiersPage = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'thou-shalt-be-for-specific-tiers',
visibility: 'tiers',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
tiersPage.tiers = [paidTier];
await models.Post.add(tiersPage, {context: {internal: true}});
const tiersPageRes = await request
.get(localUtils.API.getApiQuery(`pages/${tiersPage.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const tiersPageData = tiersPageRes.body.pages[0];
tiersPageData.tiers.length.should.eql(1);
});
it('Can update a page', async function () {
const page = {
title: 'updated page',

View file

@ -250,6 +250,84 @@ describe('Posts API', function () {
modelJson.posts_meta.feature_image_caption.should.eql(post.feature_image_caption);
});
it('Can include free and paid tiers for public post', async function () {
const publicPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'free-to-see',
visibility: 'public',
published_at: moment().add(15, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(publicPost, {context: {internal: true}});
const publicPostRes = await request
.get(localUtils.API.getApiQuery(`posts/${publicPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const publicPostData = publicPostRes.body.posts[0];
publicPostData.tiers.length.should.eql(2);
});
it('Can include free and paid tiers for members only post', async function () {
const membersPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-not-be-seen',
visibility: 'members',
published_at: moment().add(45, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(membersPost, {context: {internal: true}});
const membersPostRes = await request
.get(localUtils.API.getApiQuery(`posts/${membersPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const membersPostData = membersPostRes.body.posts[0];
membersPostData.tiers.length.should.eql(2);
});
it('Can include only paid tier for paid post', async function () {
const paidPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-paid-for',
visibility: 'paid',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(paidPost, {context: {internal: true}});
const paidPostRes = await request
.get(localUtils.API.getApiQuery(`posts/${paidPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const paidPostData = paidPostRes.body.posts[0];
paidPostData.tiers.length.should.eql(1);
});
it('Can include specific tier for post with tiers visibility', async function () {
const res = await request.get(localUtils.API.getApiQuery('products/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
const jsonResponse = res.body;
const paidTier = jsonResponse.products.find(p => p.type === 'paid');
const tiersPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-for-specific-tiers',
visibility: 'tiers',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
tiersPost.tiers = [paidTier];
await models.Post.add(tiersPost, {context: {internal: true}});
const tiersPostRes = await request
.get(localUtils.API.getApiQuery(`posts/${tiersPost.id}/`))
.set('Origin', config.get('url'))
.expect(200);
const tiersPostData = tiersPostRes.body.posts[0];
tiersPostData.tiers.length.should.eql(1);
});
it('Can update draft', async function () {
const post = {
title: 'update draft'

View file

@ -1,5 +1,8 @@
const assert = require('assert');
const moment = require('moment');
const testUtils = require('../../utils');
const models = require('../../../core/server/models');
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyUuid, anyISODateTimeWithTZ} = matchers;
@ -54,4 +57,80 @@ describe('Pages Content API', function () {
assert.equal(urlParts.protocol, 'http:');
assert.equal(urlParts.host, '127.0.0.1:2369');
});
it('Can include free and paid tiers for public post', async function () {
const publicPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'free-to-see',
visibility: 'public',
published_at: moment().add(15, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(publicPost, {context: {internal: true}});
const publicPostRes = await agent
.get(`pages/${publicPost.id}/?include=tiers`)
.expectStatus(200);
const publicPostData = publicPostRes.body.pages[0];
publicPostData.tiers.length.should.eql(2);
});
it('Can include free and paid tiers for members only post', async function () {
const membersPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'thou-shalt-not-be-seen',
visibility: 'members',
published_at: moment().add(45, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(membersPost, {context: {internal: true}});
const membersPostRes = await agent
.get(`pages/${membersPost.id}/?include=tiers`)
.expectStatus(200);
const membersPostData = membersPostRes.body.pages[0];
membersPostData.tiers.length.should.eql(2);
});
it('Can include only paid tier for paid post', async function () {
const paidPost = testUtils.DataGenerator.forKnex.createPost({
type: 'page',
slug: 'thou-shalt-be-paid-for',
visibility: 'paid',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(paidPost, {context: {internal: true}});
const paidPostRes = await agent
.get(`pages/${paidPost.id}/?include=tiers`)
.expectStatus(200);
const paidPostData = paidPostRes.body.pages[0];
paidPostData.tiers.length.should.eql(1);
});
it('Can include specific tier for page with tiers visibility', async function () {
const res = await agent
.get(`products/`)
.expectStatus(200);
const jsonResponse = res.body;
const paidTier = jsonResponse.products.find(p => p.type === 'paid');
const tiersPage = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-for-specific-tiers',
type: 'page',
visibility: 'tiers',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
tiersPage.tiers = [paidTier];
await models.Post.add(tiersPage, {context: {internal: true}});
const tiersPostRes = await agent
.get(`pages/${tiersPage.id}/?include=tiers`)
.expectStatus(200);
const tiersPostData = tiersPostRes.body.pages[0];
tiersPostData.tiers.length.should.eql(1);
});
});

View file

@ -1,6 +1,8 @@
const assert = require('assert');
const cheerio = require('cheerio');
const moment = require('moment');
const testUtils = require('../../utils');
const models = require('../../../core/server/models');
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyArray, anyEtag, anyUuid, anyISODateTimeWithTZ} = matchers;
@ -230,4 +232,76 @@ describe('Posts Content API', function () {
.fill(postMatcher)
});
});
it('Can include free and paid tiers for public post', async function () {
const publicPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'free-to-see',
visibility: 'public',
published_at: moment().add(15, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(publicPost, {context: {internal: true}});
const publicPostRes = await agent
.get(`posts/${publicPost.id}/?include=tiers`)
.expectStatus(200);
const publicPostData = publicPostRes.body.posts[0];
publicPostData.tiers.length.should.eql(2);
});
it('Can include free and paid tiers for members only post', async function () {
const membersPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-not-be-seen',
visibility: 'members',
published_at: moment().add(45, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(membersPost, {context: {internal: true}});
const membersPostRes = await agent
.get(`posts/${membersPost.id}/?include=tiers`)
.expectStatus(200);
const membersPostData = membersPostRes.body.posts[0];
membersPostData.tiers.length.should.eql(2);
});
it('Can include only paid tier for paid post', async function () {
const paidPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-paid-for',
visibility: 'paid',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
await models.Post.add(paidPost, {context: {internal: true}});
const paidPostRes = await agent
.get(`posts/${paidPost.id}/?include=tiers`)
.expectStatus(200);
const paidPostData = paidPostRes.body.posts[0];
paidPostData.tiers.length.should.eql(1);
});
it('Can include specific tier for post with tiers visibility', async function () {
const res = await agent
.get(`products/`)
.expectStatus(200);
const jsonResponse = res.body;
const paidTier = jsonResponse.products.find(p => p.type === 'paid');
const tiersPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-for-specific-tiers',
visibility: 'tiers',
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
});
tiersPost.tiers = [paidTier];
await models.Post.add(tiersPost, {context: {internal: true}});
const tiersPostRes = await agent
.get(`posts/${tiersPost.id}/?include=tiers`)
.expectStatus(200);
const tiersPostData = tiersPostRes.body.posts[0];
tiersPostData.tiers.length.should.eql(1);
});
});

View file

@ -2,6 +2,7 @@ const should = require('should');
const sinon = require('sinon');
const testUtils = require('../../../../../../utils');
const mapper = require('../../../../../../../core/server/api/canary/utils/serializers/output/utils/mapper');
const membersService = require('../../../../../../../core/server/services/members');
const serializers = require('../../../../../../../core/server/api/canary/utils/serializers');
describe('Unit: canary/utils/serializers/output/pages', function () {
@ -12,6 +13,16 @@ describe('Unit: canary/utils/serializers/output/pages', function () {
return Object.assign(data, {toJSON: sinon.stub().returns(data)});
};
sinon.stub(membersService, 'api').get(() => {
return {
productRepository: {
list: () => {
return {data: null};
}
}
};
});
sinon.stub(mapper, 'mapPage').returns({});
});
@ -47,6 +58,6 @@ describe('Unit: canary/utils/serializers/output/pages', function () {
await serializers.output.pages.all(ctrlResponse, apiConfig, frame);
mapper.mapPage.callCount.should.equal(2);
mapper.mapPage.getCall(0).args.should.eql([ctrlResponse.data[0], frame]);
mapper.mapPage.getCall(0).args.should.eql([ctrlResponse.data[0], frame, {tiers: []}]);
});
});

View file

@ -2,6 +2,7 @@ const should = require('should');
const sinon = require('sinon');
const testUtils = require('../../../../../../utils');
const mapper = require('../../../../../../../core/server/api/canary/utils/serializers/output/utils/mapper');
const membersService = require('../../../../../../../core/server/services/members');
const serializers = require('../../../../../../../core/server/api/canary/utils/serializers');
describe('Unit: canary/utils/serializers/output/posts', function () {
@ -12,6 +13,16 @@ describe('Unit: canary/utils/serializers/output/posts', function () {
return Object.assign(data, {toJSON: sinon.stub().returns(data)});
};
sinon.stub(membersService, 'api').get(() => {
return {
productRepository: {
list: () => {
return {data: null};
}
}
};
});
sinon.stub(mapper, 'mapPost').returns({});
});
@ -41,6 +52,6 @@ describe('Unit: canary/utils/serializers/output/posts', function () {
await serializers.output.posts.all(ctrlResponse, apiConfig, frame);
mapper.mapPost.callCount.should.equal(2);
mapper.mapPost.getCall(0).args.should.eql([ctrlResponse.data[0], frame]);
mapper.mapPost.getCall(0).args.should.eql([ctrlResponse.data[0], frame, {tiers: []}]);
});
});

View file

@ -3,6 +3,7 @@ const sinon = require('sinon');
const testUtils = require('../../../../../../utils');
const mapper = require('../../../../../../../core/server/api/canary/utils/serializers/output/utils/mapper');
const serializers = require('../../../../../../../core/server/api/canary/utils/serializers');
const membersService = require('../../../../../../../core/server/services/members');
describe('Unit: canary/utils/serializers/output/preview', function () {
let pageModel;
@ -12,6 +13,16 @@ describe('Unit: canary/utils/serializers/output/preview', function () {
return Object.assign(data, {toJSON: sinon.stub().returns(data), get: key => (key === 'type' ? 'page' : '')});
};
sinon.stub(membersService, 'api').get(() => {
return {
productRepository: {
list: () => {
return {data: null};
}
}
};
});
sinon.stub(mapper, 'mapPost').returns({});
});
@ -38,7 +49,7 @@ describe('Unit: canary/utils/serializers/output/preview', function () {
await serializers.output.preview.all(ctrlResponse, apiConfig, frame);
mapper.mapPost.callCount.should.equal(1);
mapper.mapPost.getCall(0).args.should.eql([ctrlResponse, frame]);
mapper.mapPost.getCall(0).args.should.eql([ctrlResponse, frame, {tiers: []}]);
frame.response.preview[0].page.should.equal(true);
});