feat: #7743 posts/diff, posts/edit
parent
acad245b4a
commit
c4bb467ea5
@ -1,121 +1,89 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
const diff = require('diff');
|
||||||
var diff = require('diff');
|
|
||||||
|
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var translator = require('../translator');
|
const translator = require('../translator');
|
||||||
|
|
||||||
var Diffs = {};
|
|
||||||
|
|
||||||
Diffs.exists = function (pid, callback) {
|
module.exports = function (Posts) {
|
||||||
if (meta.config.enablePostHistory !== 1) {
|
const Diffs = {};
|
||||||
return callback(null, 0);
|
Posts.diffs = Diffs;
|
||||||
}
|
Diffs.exists = async function (pid) {
|
||||||
|
if (meta.config.enablePostHistory !== 1) {
|
||||||
db.listLength('post:' + pid + ':diffs', function (err, numDiffs) {
|
return false;
|
||||||
return callback(err, !!numDiffs);
|
}
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Diffs.get = function (pid, since, callback) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
Diffs.list(pid, next);
|
|
||||||
},
|
|
||||||
function (timestamps, next) {
|
|
||||||
// Pass those made after `since`, and create keys
|
|
||||||
const keys = timestamps.filter(function (timestamp) {
|
|
||||||
return (parseInt(timestamp, 10) || 0) >= since;
|
|
||||||
}).map(function (timestamp) {
|
|
||||||
return 'diff:' + pid + '.' + timestamp;
|
|
||||||
});
|
|
||||||
|
|
||||||
db.getObjects(keys, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Diffs.list = function (pid, callback) {
|
|
||||||
db.getListRange('post:' + pid + ':diffs', 0, -1, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Diffs.save = function (pid, oldContent, newContent, callback) {
|
|
||||||
const now = Date.now();
|
|
||||||
const patch = diff.createPatch('', newContent, oldContent);
|
|
||||||
async.parallel([
|
|
||||||
async.apply(db.listPrepend.bind(db), 'post:' + pid + ':diffs', now),
|
|
||||||
async.apply(db.setObject.bind(db), 'diff:' + pid + '.' + now, {
|
|
||||||
pid: pid,
|
|
||||||
patch: patch,
|
|
||||||
}),
|
|
||||||
], function (err) {
|
|
||||||
// No return arguments passed back
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Diffs.load = function (pid, since, uid, callback) {
|
|
||||||
var Posts = require('../posts');
|
|
||||||
|
|
||||||
// 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()) {
|
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
async.parallel({
|
const numDiffs = await db.listLength('post:' + pid + ':diffs');
|
||||||
post: async.apply(Posts.getPostSummaryByPids, [pid], uid, {
|
return !!numDiffs;
|
||||||
parse: false,
|
};
|
||||||
}),
|
|
||||||
diffs: async.apply(Posts.diffs.get, pid, since),
|
Diffs.get = async function (pid, since) {
|
||||||
}, function (err, data) {
|
const timestamps = await Diffs.list(pid);
|
||||||
if (err) {
|
// Pass those made after `since`, and create keys
|
||||||
return callback(err);
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
Diffs.save = async function (pid, oldContent, newContent) {
|
||||||
|
const now = Date.now();
|
||||||
|
const patch = diff.createPatch('', newContent, oldContent);
|
||||||
|
await Promise.all([
|
||||||
|
db.listPrepend('post:' + pid + ':diffs', now),
|
||||||
|
db.setObject('diff:' + pid + '.' + now, {
|
||||||
|
pid: pid,
|
||||||
|
patch: patch,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
Diffs.load = async function (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);
|
||||||
|
|
||||||
|
if (isNaN(since) || since > Date.now()) {
|
||||||
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [post, diffs] = await Promise.all([
|
||||||
|
Posts.getPostSummaryByPids([pid], uid, { parse: false }),
|
||||||
|
Posts.diffs.get(pid, since),
|
||||||
|
]);
|
||||||
|
const data = {
|
||||||
|
post: post,
|
||||||
|
diffs: diffs,
|
||||||
|
};
|
||||||
postDiffLoad(data);
|
postDiffLoad(data);
|
||||||
|
const result = await plugins.fireHook('filter:parse.post', { postData: data.post });
|
||||||
|
result.postData.content = translator.escape(result.postData.content);
|
||||||
|
return result.postData;
|
||||||
|
};
|
||||||
|
|
||||||
|
function postDiffLoad(data) {
|
||||||
|
data.post = data.post[0];
|
||||||
|
data.post.content = validator.unescape(data.post.content);
|
||||||
|
|
||||||
|
// Replace content with re-constructed content from that point in time
|
||||||
|
data.post.content = data.diffs.reduce(function (content, currentDiff) {
|
||||||
|
const result = diff.applyPatch(content, currentDiff.patch, {
|
||||||
|
fuzzFactor: 1,
|
||||||
|
});
|
||||||
|
|
||||||
async.waterfall([
|
return typeof result === 'string' ? result : content;
|
||||||
function (next) {
|
}, data.post.content);
|
||||||
plugins.fireHook('filter:parse.post', { postData: data.post }, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
data.postData.content = translator.escape(data.postData.content);
|
|
||||||
next(null, data.postData);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function postDiffLoad(data) {
|
|
||||||
data.post = data.post[0];
|
|
||||||
data.post.content = validator.unescape(data.post.content);
|
|
||||||
|
|
||||||
// Replace content with re-constructed content from that point in time
|
|
||||||
data.post.content = data.diffs.reduce(function (content, currentDiff) {
|
|
||||||
const result = diff.applyPatch(content, currentDiff.patch, {
|
|
||||||
fuzzFactor: 1,
|
|
||||||
});
|
|
||||||
|
|
||||||
return typeof result === 'string' ? result : content;
|
|
||||||
}, data.post.content);
|
|
||||||
|
|
||||||
// Clear editor data (as it is outdated for this content)
|
|
||||||
delete data.post.edited;
|
|
||||||
data.post.editor = null;
|
|
||||||
|
|
||||||
data.post.content = String(data.post.content || '');
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
// Clear editor data (as it is outdated for this content)
|
||||||
Posts.diffs = {};
|
delete data.post.edited;
|
||||||
|
data.post.editor = null;
|
||||||
|
|
||||||
Object.keys(Diffs).forEach(function (property) {
|
data.post.content = String(data.post.content || '');
|
||||||
Posts.diffs[property] = Diffs[property];
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue