Added email_only property in Posts Admin API v4

https://github.com/TryGhost/Team/issues/893

- The property is only added to Admin API v4 and is invisible in all Content APIs as well as v2/v3 Posts APIs
This commit is contained in:
Naz 2021-08-04 15:00:32 +04:00 committed by naz
parent 9081877996
commit 838e94e535
9 changed files with 86 additions and 3 deletions

View File

@ -77,6 +77,7 @@ const post = (attrs, frame) => {
// @TODO: https://github.com/TryGhost/Ghost/issues/10335 // @TODO: https://github.com/TryGhost/Ghost/issues/10335
// delete attrs.page; // delete attrs.page;
delete attrs.status; delete attrs.status;
delete attrs.email_only;
// We are standardising on returning null from the Content API for any empty values // We are standardising on returning null from the Content API for any empty values
if (attrs.twitter_title === '') { if (attrs.twitter_title === '') {

View File

@ -96,6 +96,7 @@ const post = (attrs, frame) => {
// @TODO: https://github.com/TryGhost/Ghost/issues/10335 // @TODO: https://github.com/TryGhost/Ghost/issues/10335
// delete attrs.page; // delete attrs.page;
delete attrs.status; delete attrs.status;
delete attrs.email_only;
// We are standardising on returning null from the Content API for any empty values // We are standardising on returning null from the Content API for any empty values
if (attrs.twitter_title === '') { if (attrs.twitter_title === '') {

View File

@ -55,7 +55,9 @@ const mapPost = (model, frame) => {
_(metaAttrs).filter((k) => { _(metaAttrs).filter((k) => {
return (!frame.options.columns || (frame.options.columns && frame.options.columns.includes(k))); return (!frame.options.columns || (frame.options.columns && frame.options.columns.includes(k)));
}).each((attr) => { }).each((attr) => {
jsonModel[attr] = _.get(jsonModel.posts_meta, attr) || null; if (!(attr === 'email_only')) {
jsonModel[attr] = _.get(jsonModel.posts_meta, attr) || null;
}
}); });
delete jsonModel.posts_meta; delete jsonModel.posts_meta;

View File

@ -76,6 +76,7 @@ const post = (attrs, frame) => {
// @TODO: https://github.com/TryGhost/Ghost/issues/10335 // @TODO: https://github.com/TryGhost/Ghost/issues/10335
// delete attrs.page; // delete attrs.page;
delete attrs.status; delete attrs.status;
delete attrs.email_only;
// We are standardising on returning null from the Content API for any empty values // We are standardising on returning null from the Content API for any empty values
if (attrs.twitter_title === '') { if (attrs.twitter_title === '') {

View File

@ -60,7 +60,9 @@ const mapPost = (model, frame) => {
_(metaAttrs).filter((k) => { _(metaAttrs).filter((k) => {
return (!frame.options.columns || (frame.options.columns && frame.options.columns.includes(k))); return (!frame.options.columns || (frame.options.columns && frame.options.columns.includes(k)));
}).each((attr) => { }).each((attr) => {
jsonModel[attr] = _.get(jsonModel.posts_meta, attr) || null; if (!(attr === 'email_only')) {
jsonModel[attr] = _.get(jsonModel.posts_meta, attr) || null;
}
}); });
delete jsonModel.posts_meta; delete jsonModel.posts_meta;

View File

@ -600,6 +600,35 @@ describe('Posts API (canary)', function () {
}); });
}); });
it('can edit post_meta field that has default value and no previously created posts_meta relation', function () {
return request
.get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[0].id}/`))
.set('Origin', config.get('url'))
.expect(200)
.then((res) => {
should.equal(res.body.posts[0].email_only, false);
return request
.put(localUtils.API.getApiQuery('posts/' + testUtils.DataGenerator.Content.posts[1].id + '/'))
.set('Origin', config.get('url'))
.send({
posts: [{
email_only: true,
updated_at: res.body.posts[0].updated_at
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
})
.then((res) => {
should.exist(res.headers['x-cache-invalidate']);
should.exist(res.body.posts);
should.equal(res.body.posts[0].email_only, true);
});
});
it('saving post with no modbiledoc content doesn\t trigger cache invalidation', function () { it('saving post with no modbiledoc content doesn\t trigger cache invalidation', function () {
return request return request
.post(localUtils.API.getApiQuery('posts/')) .post(localUtils.API.getApiQuery('posts/'))

View File

@ -64,7 +64,8 @@ const expectedProperties = {
'meta_title', 'meta_title',
'meta_description', 'meta_description',
'email_subject', 'email_subject',
'frontmatter' 'frontmatter',
'email_only'
], ],
user: [ user: [
'id', 'id',

View File

@ -399,6 +399,29 @@ describe('Posts API (v2)', function () {
res.body.posts[0].visibility.should.equal('members'); res.body.posts[0].visibility.should.equal('members');
}); });
}); });
it('cannot edit post_meta field that was introduced in API v4', function () {
return request
.get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[0].id}/`))
.set('Origin', config.get('url'))
.expect(200)
.then((res) => {
should.equal(res.body.posts[0].email_only, undefined);
return request
.put(localUtils.API.getApiQuery('posts/' + testUtils.DataGenerator.Content.posts[1].id + '/'))
.set('Origin', config.get('url'))
.send({
posts: [{
email_only: true,
updated_at: res.body.posts[0].updated_at
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(422);
});
});
}); });
describe('Destroy', function () { describe('Destroy', function () {

View File

@ -592,6 +592,29 @@ describe('Posts API (v3)', function () {
}); });
}); });
it('cannot edit post_meta field that was introduced in API v4', function () {
return request
.get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[0].id}/`))
.set('Origin', config.get('url'))
.expect(200)
.then((res) => {
should.equal(res.body.posts[0].email_only, undefined);
return request
.put(localUtils.API.getApiQuery('posts/' + testUtils.DataGenerator.Content.posts[1].id + '/'))
.set('Origin', config.get('url'))
.send({
posts: [{
email_only: true,
updated_at: res.body.posts[0].updated_at
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(422);
});
});
it('saving post with no modbiledoc content doesn\t trigger cache invalidation', function () { it('saving post with no modbiledoc content doesn\t trigger cache invalidation', function () {
return request return request
.post(localUtils.API.getApiQuery('posts/')) .post(localUtils.API.getApiQuery('posts/'))