Persisted post revision author and title (#16653)

no issue

Persisted post revision author and title
This commit is contained in:
Michael Barrett 2023-04-18 14:15:26 +01:00 committed by GitHub
parent 7e349d0bf8
commit 9911e6be78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 14 deletions

View File

@ -908,6 +908,7 @@ Post = ghostBookshelf.Model.extend({
max_revisions: POST_REVISIONS_COUNT
}
});
const authorId = this.contextUser(options);
ops.push(async function updateRevisions() {
const revisionModels = await ghostBookshelf.model('PostRevision')
.findAll(Object.assign({
@ -919,12 +920,16 @@ Post = ghostBookshelf.Model.extend({
const previous = {
id: model.id,
lexical: model.previous('lexical'),
html: model.previous('html')
html: model.previous('html'),
author_id: model.previous('updated_by'),
title: model.previous('title')
};
const current = {
id: model.id,
lexical: model.get('lexical'),
html: model.get('html')
html: model.get('html'),
author_id: authorId,
title: model.get('title')
};
const newRevisions = await postRevisions.getRevisions(previous, current, revisions);

View File

@ -693,13 +693,13 @@ Object {
"og_title": null,
"post_revisions": Array [
Object {
"author_id": null,
"author_id": "1",
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"created_at_ts": Any<Number>,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"lexical": "{\\"root\\":{\\"children\\":[{\\"children\\":[{\\"detail\\":0,\\"format\\":0,\\"mode\\":\\"normal\\",\\"style\\":\\"\\",\\"text\\":\\"Testing post creation with lexical\\",\\"type\\":\\"text\\",\\"version\\":1}],\\"direction\\":\\"ltr\\",\\"format\\":\\"\\",\\"indent\\":0,\\"type\\":\\"paragraph\\",\\"version\\":1}],\\"direction\\":\\"ltr\\",\\"format\\":\\"\\",\\"indent\\":0,\\"type\\":\\"root\\",\\"version\\":1}}",
"post_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"title": null,
"title": "Post Revisions Test",
},
],
"published_at": null,
@ -722,7 +722,7 @@ exports[`Posts API Can read with post_revisions included 4: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "1480",
"content-length": "1496",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
@ -831,7 +831,7 @@ exports[`Posts API Create Can create a post with lexical 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "4565",
"content-length": "4574",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
@ -1261,7 +1261,7 @@ exports[`Posts API Update Can update a post with lexical 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "4495",
"content-length": "4511",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
@ -1371,7 +1371,7 @@ exports[`Posts API Update Can update a post with lexical 4: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "5012",
"content-length": "5044",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,

View File

@ -3,6 +3,8 @@
* @property {string} id
* @property {string} lexical
* @property {string} html
* @property {string} author_id
* @property {string} title
*/
/**
@ -10,6 +12,8 @@
* @property {string} post_id
* @property {string} lexical
* @property {number} created_at_ts
* @property {string} author_id
* @property {string} title
*/
class PostRevisions {
@ -35,7 +39,7 @@ class PostRevisions {
if (revisions.length === 0) {
return true;
}
return previous.html !== current.html;
return previous.html !== current.html || previous.title !== current.title;
}
/**
@ -57,7 +61,7 @@ class PostRevisions {
];
}
return [...revisions, currentRevision].slice(-this.config.max_revisions);
return [currentRevision, ...revisions].slice(0, this.config.max_revisions);
}
/**
@ -68,7 +72,9 @@ class PostRevisions {
return {
post_id: input.id,
lexical: input.lexical,
created_at_ts: Date.now() - offset
created_at_ts: Date.now() - offset,
author_id: input.author_id,
title: input.title
};
}
}

View File

@ -58,6 +58,25 @@ describe('PostRevisions', function () {
assert.equal(actual, expected);
});
it('should return true if the current and previous title values are different', function () {
const postRevisions = new PostRevisions({config});
const expected = true;
const actual = postRevisions.shouldGenerateRevision({
lexical: 'blah',
html: 'blah',
title: 'blah'
}, {
lexical: 'blah',
html: 'blah',
title: 'blah2'
}, [{
lexical: 'blah'
}]);
assert.equal(actual, expected);
});
});
describe('getRevisions', function () {
@ -93,21 +112,27 @@ describe('PostRevisions', function () {
assert.deepEqual(actual, expected);
});
it('returns one revisions when there are no existing revisions', async function () {
it('returns one revision when there are no existing revisions', async function () {
const postRevisions = new PostRevisions({config});
const actual = await postRevisions.getRevisions({
id: '1',
lexical: 'previous',
html: 'previous'
html: 'previous',
author_id: '123',
title: 'foo bar baz'
}, {
id: '1',
lexical: 'current',
html: 'current'
html: 'current',
author_id: '123',
title: 'foo bar baz'
}, []);
assert.equal(actual.length, 1);
assert.equal(actual[0].lexical, 'current');
assert.equal(actual[0].author_id, '123');
assert.equal(actual[0].title, 'foo bar baz');
});
it('limits the number of revisions to the max_revisions count', async function () {