wrapping up post history work

This commit is contained in:
Julian Lam
2018-02-16 21:22:26 -05:00
parent c1fef4561b
commit a86d91a552
5 changed files with 91 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
var async = require('async');
var validator = require('validator');
var db = require('../database');
var diff = require('diff');
@@ -18,7 +19,7 @@ module.exports = function (Posts) {
db.getSortedSetRangeWithScores('post:' + pid + ':diffs', 0, -1, function (err, diffs) {
callback(err, diffs ? diffs.map(function (diffObj) {
return diffObj.score;
}) : null);
}).reverse() : null);
});
};
@@ -26,7 +27,7 @@ module.exports = function (Posts) {
db.sortedSetAdd('post:' + pid + ':diffs', Date.now(), diff.createPatch('', newContent, oldContent), callback);
};
Posts.diffs.load = function (pid, since, callback) {
Posts.diffs.load = function (pid, since, uid, callback) {
// Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since`
since = parseInt(since, 10);
@@ -35,13 +36,18 @@ module.exports = function (Posts) {
}
async.parallel({
post: async.apply(Posts.getPostData, pid),
post: async.apply(Posts.getPostSummaryByPids, [pid], uid, {
parse: false,
}),
diffs: async.apply(db.getSortedSetRangeByScore.bind(db), 'post:' + pid + ':diffs', 0, -1, since, Date.now()),
}, function (err, data) {
if (err) {
return callback(err);
}
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.reverse().reduce(function (content, diffString) {
return diff.applyPatch(content, diffString);
@@ -51,7 +57,9 @@ module.exports = function (Posts) {
delete data.post.edited;
data.post.editor = null;
return callback(null, data.post);
Posts.parsePost(data.post, function (err, post) {
callback(err, post);
});
});
};
};