Wired up feature image alt and caption to DB and Admin

We no longer need a reference to the previous version, instead we can use the
latest revision, this makes it easier to compare "off table" data such as the
feature image caption stored in posts_meta.
This commit is contained in:
Fabien "egg" O'Carroll 2023-04-21 14:53:35 +01:00
parent bbdbcd02ef
commit 5feedadc80
6 changed files with 46 additions and 71 deletions

View File

@ -5,6 +5,8 @@ export default class PostRevisionModel extends Model {
@attr('string') lexical;
@attr('string') title;
@attr('string') featureImage;
@attr('string') featureImageAlt;
@attr('string') featureImageCaption;
@attr('string') reason;
@attr('moment-utc') createdAt;
@belongsTo('user') author;

View File

@ -9,9 +9,11 @@ export default class PostRevisionSerializer extends ApplicationSerializer.extend
lexical: {key: 'lexical'},
title: {key: 'title'},
createdAt: {key: 'created_at'},
postIdLocal: {key: 'post_id'},
postStatus: {key: 'post_status'},
reason: {key: 'reason'},
featureImage: {key: 'feature_image'}
featureImage: {key: 'feature_image'},
featureImageAlt: {key: 'feature_image_alt'},
featureImageCaption: {key: 'feature_image_caption'},
postIdLocal: {key: 'post_id'}
};
}

@ -1 +1 @@
Subproject commit 5c7fb39df91561a18b76fbfef88b9f58fcadc696
Subproject commit 657bb8f98653c5f1aea034a726e53f46ccf83b23

View File

@ -917,21 +917,15 @@ Post = ghostBookshelf.Model.extend({
}, _.pick(options, 'transacting')));
const revisions = revisionModels.toJSON();
const previous = {
id: model.id,
lexical: model.previous('lexical'),
html: model.previous('html'),
author_id: model.previous('updated_by'),
feature_image: model.previous('feature_image'),
title: model.previous('title'),
post_status: model.previous('status')
};
const current = {
id: model.id,
lexical: model.get('lexical'),
html: model.get('html'),
author_id: authorId,
feature_image: model.get('feature_image'),
feature_image_alt: model.get('posts_meta')?.feature_image_alt,
feature_image_caption: model.get('posts_meta')?.feature_image_caption,
title: model.get('title'),
post_status: model.get('status')
};
@ -941,7 +935,7 @@ Post = ghostBookshelf.Model.extend({
forceRevision: options.save_revision,
isPublished: newStatus === 'published'
};
const newRevisions = await postRevisions.getRevisions(previous, current, revisions, revisionOptions);
const newRevisions = await postRevisions.getRevisions(current, revisions, revisionOptions);
model.set('post_revisions', newRevisions);
});
}

View File

@ -5,6 +5,8 @@
* @property {string} html
* @property {string} author_id
* @property {string} feature_image
* @property {string} feature_image_alt
* @property {string} feature_image_caption
* @property {string} title
* @property {string} reason
* @property {string} post_status
@ -17,6 +19,8 @@
* @property {number} created_at_ts
* @property {string} author_id
* @property {string} feature_image
* @property {string} feature_image_alt
* @property {string} feature_image_caption
* @property {string} title
* @property {string} reason
* @property {string} post_status
@ -41,11 +45,8 @@ class PostRevisions {
* @param {object} options
* @returns {object}
*/
shouldGenerateRevision(previous, current, revisions, options) {
shouldGenerateRevision(current, revisions, options) {
const latestRevision = revisions[revisions.length - 1];
if (!previous) {
return {value: false};
}
// If there's no revisions for this post, we should always save a revision
if (revisions.length === 0) {
return {value: true, reason: 'initial_revision'};
@ -57,8 +58,9 @@ class PostRevisions {
const forceRevision = options && options.forceRevision;
const lexicalHasChangedSinceLatestRevision = latestRevision.lexical !== current.lexical;
const titleHasChanged = previous.title !== current.title;
if ((lexicalHasChangedSinceLatestRevision || titleHasChanged) && forceRevision) {
const titleHasChanged = latestRevision.title !== current.title;
const featuredImagedHasChanged = latestRevision.feature_image !== current.feature_image;
if ((lexicalHasChangedSinceLatestRevision || titleHasChanged || featuredImagedHasChanged) && forceRevision) {
return {value: true, reason: 'explicit_save'};
}
return {value: false};
@ -71,8 +73,8 @@ class PostRevisions {
* @param {object} options
* @returns {Promise<Revision[]>}
*/
async getRevisions(previous, current, revisions, options) {
const shouldGenerateRevision = this.shouldGenerateRevision(previous, current, revisions, options);
async getRevisions(current, revisions, options) {
const shouldGenerateRevision = this.shouldGenerateRevision(current, revisions, options);
if (!shouldGenerateRevision.value) {
return revisions;
}
@ -106,6 +108,8 @@ class PostRevisions {
created_at_ts: Date.now() - offset,
author_id: input.author_id,
feature_image: input.feature_image,
feature_image_alt: input.feature_image_alt,
feature_image_caption: input.feature_image_caption,
title: input.title,
post_status: input.post_status
};

View File

@ -8,20 +8,11 @@ const config = {
describe('PostRevisions', function () {
describe('shouldGenerateRevision', function () {
it('should return false if there is no previous', function () {
const postRevisions = new PostRevisions({config});
const expected = {value: false};
const actual = postRevisions.shouldGenerateRevision(null, {}, []);
assert.deepEqual(actual, expected);
});
it('should return true if there are no revisions', function () {
const postRevisions = new PostRevisions({config});
const expected = {value: true, reason: 'initial_revision'};
const actual = postRevisions.shouldGenerateRevision({}, {}, []);
const actual = postRevisions.shouldGenerateRevision({}, []);
assert.deepEqual(actual, expected);
});
@ -31,9 +22,6 @@ describe('PostRevisions', function () {
const expected = {value: false};
const actual = postRevisions.shouldGenerateRevision({
lexical: 'previous',
html: 'blah'
}, {
lexical: 'current',
html: 'blah'
}, [{
@ -48,9 +36,6 @@ describe('PostRevisions', function () {
const expected = {value: true, reason: 'explicit_save'};
const actual = postRevisions.shouldGenerateRevision({
lexical: 'blah',
html: 'blah'
}, {
lexical: 'blah',
html: 'blah2'
}, [{
@ -69,10 +54,6 @@ describe('PostRevisions', function () {
const expected = {value: true, reason: 'explicit_save'};
const actual = postRevisions.shouldGenerateRevision({
lexical: 'blah',
html: 'blah',
title: 'blah'
}, {
lexical: 'blah',
html: 'blah',
title: 'blah2'
@ -85,15 +66,32 @@ describe('PostRevisions', function () {
assert.deepEqual(actual, expected);
});
it('should return true if the current and previous feature_image values are different and forceRevision is true', function () {
const postRevisions = new PostRevisions({config});
const expected = {value: true, reason: 'explicit_save'};
const actual = postRevisions.shouldGenerateRevision({
lexical: 'blah',
html: 'blah',
title: 'blah',
feature_image: 'new'
}, [{
lexical: 'blah',
html: 'blah',
title: 'blah',
feature_image: null
}], {
forceRevision: true
});
assert.deepEqual(actual, expected);
});
it('should always return true if isPublished is true', function () {
const postRevisions = new PostRevisions({config});
const expected = {value: true, reason: 'published'};
const actual = postRevisions.shouldGenerateRevision({
lexical: 'blah',
html: 'blah',
title: 'blah'
}, {
lexical: 'blah',
html: 'blah',
title: 'blah2'
@ -114,7 +112,7 @@ describe('PostRevisions', function () {
const expected = [{
lexical: 'blah'
}];
const actual = await postRevisions.getRevisions(null, {}, [{
const actual = await postRevisions.getRevisions({}, [{
lexical: 'blah'
}]);
@ -130,9 +128,6 @@ describe('PostRevisions', function () {
const actual = await postRevisions.getRevisions({
lexical: 'blah',
html: 'blah'
}, {
lexical: 'blah',
html: 'blah'
}, [{
lexical: 'revision'
}]);
@ -144,12 +139,6 @@ describe('PostRevisions', function () {
const postRevisions = new PostRevisions({config});
const actual = await postRevisions.getRevisions({
id: '1',
lexical: 'previous',
html: 'previous',
author_id: '123',
title: 'foo bar baz'
}, {
id: '1',
lexical: 'current',
html: 'current',
@ -171,20 +160,12 @@ describe('PostRevisions', function () {
});
const revisions = await postRevisions.getRevisions({
id: '1',
lexical: 'previous',
html: 'previous'
}, {
id: '1',
lexical: 'current',
html: 'current'
}, []);
const actual = await postRevisions.getRevisions({
id: '1',
lexical: 'old',
html: 'old'
}, {
id: '1',
lexical: 'new',
html: 'new'
@ -203,20 +184,12 @@ describe('PostRevisions', function () {
});
const revisions = await postRevisions.getRevisions({
id: '1',
lexical: 'previous',
html: 'previous'
}, {
id: '1',
lexical: 'current',
html: 'current'
}, []);
const actual = await postRevisions.getRevisions({
id: '1',
lexical: 'old',
html: 'old'
}, {
id: '1',
lexical: 'new',
html: 'new'