mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-03 05:09:48 +01:00
feat: #9109, ability to delete a post's diffs
This commit is contained in:
@@ -260,9 +260,14 @@ postsAPI.getDiffs = async (caller, data) => {
|
||||
let usernames = await user.getUsersFields(uids, ['username']);
|
||||
usernames = usernames.map(userObj => (userObj.uid ? userObj.username : null));
|
||||
|
||||
const cid = await posts.getCidByPid(data.pid);
|
||||
const isModerator = await privileges.users.isModerator(cid, caller.uid);
|
||||
|
||||
let canEdit = true;
|
||||
try {
|
||||
await user.isPrivilegedOrSelf(caller.uid, post.uid);
|
||||
if (!isModerator) {
|
||||
await user.isPrivilegedOrSelf(caller.uid, post.uid);
|
||||
}
|
||||
} catch (e) {
|
||||
canEdit = false;
|
||||
}
|
||||
@@ -276,6 +281,7 @@ postsAPI.getDiffs = async (caller, data) => {
|
||||
username: usernames[idx],
|
||||
})),
|
||||
editable: canEdit,
|
||||
deletable: isModerator,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const posts = require('../../posts');
|
||||
const privileges = require('../../privileges');
|
||||
|
||||
const api = require('../../api');
|
||||
const helpers = require('../helpers');
|
||||
@@ -94,3 +95,19 @@ Posts.restoreDiff = async (req, res) => {
|
||||
helpers.formatApiResponse(200, res, await api.posts.restoreDiff(req, { ...req.params }));
|
||||
};
|
||||
|
||||
Posts.deleteDiff = async (req, res) => {
|
||||
if (!parseInt(req.params.pid, 10)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
const cid = await posts.getCidByPid(req.params.pid);
|
||||
const isModerator = privileges.users.isModerator(cid, req.uid);
|
||||
|
||||
if (!isModerator) {
|
||||
return helpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
await posts.diffs.delete(req.params.pid, req.params.timestamp, req.uid);
|
||||
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
@@ -71,28 +71,79 @@ module.exports = function (Posts) {
|
||||
});
|
||||
};
|
||||
|
||||
async function postDiffLoad(pid, since, uid) {
|
||||
// Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since`
|
||||
since = parseInt(since, 10);
|
||||
Diffs.delete = async function (pid, timestamp, uid) {
|
||||
getValidatedTimestamp(timestamp);
|
||||
|
||||
if (isNaN(since) || since > Date.now()) {
|
||||
const [post, diffs, timestamps] = await Promise.all([
|
||||
Posts.getPostSummaryByPids([pid], uid, { parse: false }),
|
||||
Diffs.get(pid),
|
||||
Diffs.list(pid),
|
||||
]);
|
||||
|
||||
const timestampIndex = timestamps.indexOf(timestamp);
|
||||
const lastTimestampIndex = timestamps.length - 1;
|
||||
|
||||
if (timestamp === String(post[0].timestamp)) {
|
||||
return Promise.all([
|
||||
db.delete(`diff:${pid}.${timestamps[lastTimestampIndex]}`),
|
||||
db.listRemoveAll(`post:${pid}:diffs`, timestamps[lastTimestampIndex]),
|
||||
]);
|
||||
}
|
||||
if (timestampIndex === 0 || timestampIndex === -1) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
const postContent = validator.unescape(post[0].content);
|
||||
const versionContents = {};
|
||||
for (let i = 0, content = postContent; i < timestamps.length; ++i) {
|
||||
versionContents[timestamps[i]] = applyPatch(content, diffs[i]);
|
||||
content = versionContents[timestamps[i]];
|
||||
}
|
||||
|
||||
/* eslint-disable no-await-in-loop */
|
||||
for (let i = lastTimestampIndex; i >= timestampIndex; --i) {
|
||||
const newContentIndex = i === timestampIndex ? i - 2 : i - 1;
|
||||
const timestampToUpdate = newContentIndex + 1;
|
||||
const newContent = newContentIndex < 0 ? postContent : versionContents[timestamps[newContentIndex]];
|
||||
const patch = diff.createPatch('', newContent, versionContents[timestamps[i]]);
|
||||
await db.setObject('diff:' + pid + '.' + timestamps[timestampToUpdate], { patch });
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
db.delete(`diff:${pid}.${timestamp}`),
|
||||
db.listRemoveAll(`post:${pid}:diffs`, timestamp),
|
||||
]);
|
||||
};
|
||||
|
||||
async function postDiffLoad(pid, since, uid) {
|
||||
// Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since`
|
||||
since = getValidatedTimestamp(since);
|
||||
|
||||
const [post, diffs] = await Promise.all([
|
||||
Posts.getPostSummaryByPids([pid], uid, { parse: false }),
|
||||
Posts.diffs.get(pid, since),
|
||||
]);
|
||||
|
||||
// Replace content with re-constructed content from that point in time
|
||||
post[0].content = diffs.reduce(function (content, currentDiff) {
|
||||
const result = diff.applyPatch(content, currentDiff.patch, {
|
||||
fuzzFactor: 1,
|
||||
});
|
||||
|
||||
return typeof result === 'string' ? result : content;
|
||||
}, validator.unescape(post[0].content));
|
||||
post[0].content = diffs.reduce(applyPatch, validator.unescape(post[0].content));
|
||||
|
||||
return post[0];
|
||||
}
|
||||
|
||||
function getValidatedTimestamp(timestamp) {
|
||||
timestamp = parseInt(timestamp, 10);
|
||||
|
||||
if (isNaN(timestamp) || timestamp > Date.now()) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
function applyPatch(content, aDiff) {
|
||||
const result = diff.applyPatch(content, aDiff.patch, {
|
||||
fuzzFactor: 1,
|
||||
});
|
||||
return typeof result === 'string' ? result : content;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,6 +29,7 @@ module.exports = function () {
|
||||
setupApiRoute(router, 'get', '/:pid/diffs', [middleware.authenticateOrGuest, middleware.assert.post], controllers.write.posts.getDiffs);
|
||||
setupApiRoute(router, 'get', '/:pid/diffs/:since', [middleware.authenticateOrGuest, middleware.assert.post], controllers.write.posts.loadDiff);
|
||||
setupApiRoute(router, 'put', '/:pid/diffs/:since', [...middlewares, middleware.assert.post], controllers.write.posts.restoreDiff);
|
||||
setupApiRoute(router, 'delete', '/:pid/diffs/:timestamp', [...middlewares, middleware.assert.post], controllers.write.posts.deleteDiff);
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user