2018-02-16 16:41:06 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
const validator = require('validator');
|
|
|
|
|
const diff = require('diff');
|
2018-02-16 16:41:06 -05:00
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const translator = require('../translator');
|
2018-02-16 16:41:06 -05:00
|
|
|
|
|
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
module.exports = function (Posts) {
|
|
|
|
|
const Diffs = {};
|
|
|
|
|
Posts.diffs = Diffs;
|
|
|
|
|
Diffs.exists = async function (pid) {
|
|
|
|
|
if (meta.config.enablePostHistory !== 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-03-15 16:03:24 -04:00
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
const numDiffs = await db.listLength('post:' + pid + ':diffs');
|
|
|
|
|
return !!numDiffs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Diffs.get = async function (pid, since) {
|
|
|
|
|
const timestamps = await Diffs.list(pid);
|
|
|
|
|
// Pass those made after `since`, and create keys
|
|
|
|
|
const keys = timestamps.filter(t => (parseInt(t, 10) || 0) >= since)
|
|
|
|
|
.map(t => 'diff:' + pid + '.' + t);
|
|
|
|
|
return await db.getObjects(keys);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Diffs.list = async function (pid) {
|
|
|
|
|
return await db.getListRange('post:' + pid + ':diffs', 0, -1);
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-15 11:02:56 -04:00
|
|
|
Diffs.save = async function (data) {
|
|
|
|
|
const { pid, uid, oldContent, newContent } = data;
|
2019-07-17 13:38:40 -04:00
|
|
|
const now = Date.now();
|
|
|
|
|
const patch = diff.createPatch('', newContent, oldContent);
|
|
|
|
|
await Promise.all([
|
|
|
|
|
db.listPrepend('post:' + pid + ':diffs', now),
|
|
|
|
|
db.setObject('diff:' + pid + '.' + now, {
|
2020-06-15 11:02:56 -04:00
|
|
|
uid: uid,
|
2019-07-17 13:38:40 -04:00
|
|
|
pid: pid,
|
|
|
|
|
patch: patch,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Diffs.load = async function (pid, since, uid) {
|
2020-06-15 11:02:56 -04:00
|
|
|
const post = await postDiffLoad(pid, since, uid);
|
|
|
|
|
|
|
|
|
|
// Clear editor data (as it is outdated for this content)
|
|
|
|
|
delete post.edited;
|
|
|
|
|
post.editor = null;
|
|
|
|
|
|
|
|
|
|
post.content = String(post.content || '');
|
|
|
|
|
|
|
|
|
|
const result = await plugins.fireHook('filter:parse.post', { postData: post });
|
|
|
|
|
result.postData.content = translator.escape(result.postData.content);
|
|
|
|
|
return result.postData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Diffs.restore = async function (pid, since, uid, req) {
|
|
|
|
|
const post = await postDiffLoad(pid, since, uid);
|
|
|
|
|
|
|
|
|
|
return await Posts.edit({
|
|
|
|
|
uid: uid,
|
|
|
|
|
pid: pid,
|
|
|
|
|
content: post.content,
|
|
|
|
|
req: req,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function postDiffLoad(pid, since, uid) {
|
2019-07-17 13:38:40 -04:00
|
|
|
// Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since`
|
|
|
|
|
since = parseInt(since, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(since) || since > Date.now()) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
2018-03-15 16:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
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
|
2020-06-15 11:02:56 -04:00
|
|
|
post[0].content = diffs.reduce(function (content, currentDiff) {
|
2019-07-17 13:38:40 -04:00
|
|
|
const result = diff.applyPatch(content, currentDiff.patch, {
|
|
|
|
|
fuzzFactor: 1,
|
|
|
|
|
});
|
2018-02-16 18:20:27 -05:00
|
|
|
|
2019-07-17 13:38:40 -04:00
|
|
|
return typeof result === 'string' ? result : content;
|
2020-06-15 11:02:56 -04:00
|
|
|
}, validator.unescape(post[0].content));
|
2018-03-15 16:03:24 -04:00
|
|
|
|
2020-06-15 11:02:56 -04:00
|
|
|
return post[0];
|
2019-07-17 13:38:40 -04:00
|
|
|
}
|
2018-02-16 16:41:06 -05:00
|
|
|
};
|