Squashed commit of the following:

commit 7bd46afad7033a466626826d3e29610f41328510
Author: Julian Lam <julian@nodebb.org>
Date:   Thu Mar 15 15:41:36 2018 -0400

    fixes #6363

commit 4b755d5801b2f6d70cea10516f88392708c72f61
Author: Julian Lam <julian@nodebb.org>
Date:   Thu Mar 15 15:24:12 2018 -0400

    fixes #6362

commit 6035e75453a08aee0fef7ff59d57dd5c1e8f4ac9
Author: Julian Lam <julian@nodebb.org>
Date:   Thu Mar 15 15:07:23 2018 -0400

    Fixes #6361
This commit is contained in:
Julian Lam
2018-03-15 15:42:15 -04:00
parent 8492a1586f
commit 5d2e6f0e8e
5 changed files with 95 additions and 14 deletions

View File

@@ -12,21 +12,44 @@ module.exports = function (Posts) {
Posts.diffs = {};
Posts.diffs.exists = function (pid, callback) {
db.sortedSetCard('post:' + pid + ':diffs', function (err, numDiffs) {
return callback(err, numDiffs > 0);
db.listLength('post:' + pid + ':diffs', function (err, numDiffs) {
return callback(err, !!numDiffs);
});
};
Posts.diffs.get = function (pid, since, callback) {
async.waterfall([
async.apply(db.getListRange.bind(db), 'post:' + pid + ':diffs', 0, -1),
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);
};
Posts.diffs.list = function (pid, callback) {
db.getSortedSetRangeWithScores('post:' + pid + ':diffs', 0, -1, function (err, diffs) {
callback(err, diffs ? diffs.map(function (diffObj) {
return diffObj.score;
}).reverse() : null);
});
db.getListRange('post:' + pid + ':diffs', 0, -1, callback);
};
Posts.diffs.save = function (pid, oldContent, newContent, callback) {
db.sortedSetAdd('post:' + pid + ':diffs', Date.now(), diff.createPatch('', newContent, oldContent), 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);
});
};
Posts.diffs.load = function (pid, since, uid, callback) {
@@ -41,7 +64,7 @@ module.exports = function (Posts) {
post: async.apply(Posts.getPostSummaryByPids, [pid], uid, {
parse: false,
}),
diffs: async.apply(db.getSortedSetRangeByScore.bind(db), 'post:' + pid + ':diffs', 0, -1, since, Date.now()),
diffs: async.apply(Posts.diffs.get, pid, since),
}, function (err, data) {
if (err) {
return callback(err);
@@ -51,8 +74,8 @@ module.exports = function (Posts) {
data.post.content = validator.unescape(data.post.content);
// Replace content with re-constructed content from that point in time
data.post.content = data.diffs.reverse().reduce(function (content, diffString) {
return diff.applyPatch(content, diffString, {
data.post.content = data.diffs.reduce(function (content, currentDiff) {
return diff.applyPatch(content, currentDiff.patch, {
fuzzFactor: 1,
});
}, data.post.content);