Skip to content

Node.js based forum software built for the modern web

test: post diff deletion tests

gasoved72b050b4a8d2
diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml
index 9510876c44..8b2592412d 100644
--- a/public/openapi/write.yaml
+++ b/public/openapi/write.yaml
@@ -116,6 +116,8 @@ paths:
$ref: 'write/posts/pid/diffs.yaml'
/posts/{pid}/diffs/{since}:
$ref: 'write/posts/pid/diffs/since.yaml'
+ /posts/{pid}/diffs/{timestamp}:
+ $ref: 'write/posts/pid/diffs/timestamp.yaml'
/admin/settings/{setting}:
$ref: 'write/admin/settings/setting.yaml'
/files/:
diff --git a/public/openapi/write/posts/pid/diffs.yaml b/public/openapi/write/posts/pid/diffs.yaml
index 40d4201185..5f0046781d 100644
--- a/public/openapi/write/posts/pid/diffs.yaml
+++ b/public/openapi/write/posts/pid/diffs.yaml
@@ -38,4 +38,6 @@ get:
username:
type: string
editable:
- type: boolean
\ No newline at end of file
+ type: boolean
+ deletable:
+ type: boolean
diff --git a/public/openapi/write/posts/pid/diffs/timestamp.yaml b/public/openapi/write/posts/pid/diffs/timestamp.yaml
new file mode 100644
index 0000000000..205c7e1e37
--- /dev/null
+++ b/public/openapi/write/posts/pid/diffs/timestamp.yaml
@@ -0,0 +1,37 @@
+delete:
+ tags:
+ - posts
+ summary: delete a post diff
+ description: This operation deletes a post diff from its history.
+ parameters:
+ - in: path
+ name: pid
+ schema:
+ type: string
+ required: true
+ description: a valid post id
+ example: 2
+ - in: path
+ name: timestamp
+ schema:
+ type: number
+ required: true
+ description: a valid UNIX timestamp
+ example: 1611850000000
+ responses:
+ '200':
+ description: Post diff successfully deleted
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ status:
+ $ref: ../../../../components/schemas/Status.yaml#/Status
+ response:
+ type: object
+ properties: {}
+ '400':
+ $ref: ../../../../components/responses/400.yaml#/400
+ '403':
+ $ref: ../../../../components/responses/403.yaml#/403
\ No newline at end of file
diff --git a/src/posts/diffs.js b/src/posts/diffs.js
index 2ca11b016b..5dc1de9ba5 100644
--- a/src/posts/diffs.js
+++ b/src/posts/diffs.js
@@ -84,6 +84,7 @@ module.exports = function (Posts) {
const lastTimestampIndex = timestamps.length - 1;
if (timestamp === String(post[0].timestamp)) {
+ // Deleting oldest diff, so history rewrite is not needed
return Promise.all([
db.delete(`diff:${pid}.${timestamps[lastTimestampIndex]}`),
db.listRemoveAll(`post:${pid}:diffs`, timestamps[lastTimestampIndex]),
@@ -102,6 +103,7 @@ module.exports = function (Posts) {
/* eslint-disable no-await-in-loop */
for (let i = lastTimestampIndex; i >= timestampIndex; --i) {
+ // Recreate older diffs with skipping the deleted diff
const newContentIndex = i === timestampIndex ? i - 2 : i - 1;
const timestampToUpdate = newContentIndex + 1;
const newContent = newContentIndex < 0 ? postContent : versionContents[timestamps[newContentIndex]];
diff --git a/test/posts.js b/test/posts.js
index b777230f93..9c32d90c89 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -647,6 +647,29 @@ describe('Post\'s', function () {
done();
});
});
+
+ it('should not delete first diff of a post', async function () {
+ const timestamps = await posts.diffs.list(replyPid);
+ await assert.rejects(async () => {
+ await posts.diffs.delete(replyPid, timestamps[0], voterUid);
+ }, {
+ message: '[[error:invalid-data]]',
+ });
+ });
+
+ it('should delete a post diff', async function () {
+ await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' });
+ await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' });
+ const timestamp = (await posts.diffs.list(replyPid)).pop();
+ await posts.diffs.delete(replyPid, timestamp, voterUid);
+ const differentTimestamp = (await posts.diffs.list(replyPid)).pop();
+ assert.notStrictEqual(timestamp, differentTimestamp);
+ });
+
+ it('should load (oldest) diff and reconstruct post correctly after a diff deletion', async function () {
+ const data = await posts.diffs.load(replyPid, 0, voterUid);
+ assert.strictEqual(data.content, 'A reply to edit');
+ });
});
describe('move', function () {