Merge branch 'post-history' into develop
commit
d8b86dc850
@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
define('forum/topic/diffs', ['benchpress', 'translator'], function (Benchpress, translator) {
|
||||
var Diffs = {};
|
||||
|
||||
Diffs.open = function (pid) {
|
||||
var localeStringOpts = { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric' };
|
||||
|
||||
socket.emit('posts.getDiffs', { pid: pid }, function (err, timestamps) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
Benchpress.parse('partials/modals/post_history', {
|
||||
diffs: timestamps.map(function (timestamp) {
|
||||
return {
|
||||
timestamp: timestamp,
|
||||
pretty: new Date(timestamp).toLocaleString(config.userLang.replace('_', '-'), localeStringOpts),
|
||||
};
|
||||
}),
|
||||
numDiffs: timestamps.length,
|
||||
}, function (html) {
|
||||
translator.translate(html, function (html) {
|
||||
var modal = bootbox.dialog({
|
||||
title: '[[topic:diffs.title]]',
|
||||
message: html,
|
||||
});
|
||||
|
||||
if (!timestamps.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
var selectEl = modal.find('select');
|
||||
var postContainer = modal.find('ul.posts-list');
|
||||
|
||||
selectEl.on('change', function () {
|
||||
Diffs.load(pid, this.value, postContainer);
|
||||
});
|
||||
|
||||
modal.on('shown.bs.modal', function () {
|
||||
Diffs.load(pid, selectEl.val(), postContainer);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Diffs.load = function (pid, since, postContainer) {
|
||||
socket.emit('posts.showPostAt', { pid: pid, since: since }, function (err, data) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
data.deleted = !!parseInt(data.deleted, 10);
|
||||
|
||||
app.parseAndTranslate('partials/posts_list', 'posts', {
|
||||
posts: [data],
|
||||
}, function (html) {
|
||||
postContainer.empty().append(html);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return Diffs;
|
||||
});
|
@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
|
||||
var db = require('../database');
|
||||
var diff = require('diff');
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
Posts.diffs.save = function (pid, oldContent, newContent, callback) {
|
||||
db.sortedSetAdd('post:' + pid + ':diffs', Date.now(), diff.createPatch('', newContent, oldContent), 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);
|
||||
|
||||
if (isNaN(since) || since > Date.now()) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
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);
|
||||
}, data.post.content);
|
||||
|
||||
// Clear editor data (as it is outdated for this content)
|
||||
delete data.post.edited;
|
||||
data.post.editor = null;
|
||||
|
||||
Posts.parsePost(data.post, function (err, post) {
|
||||
callback(err, post);
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
var posts = require('../../posts');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.getDiffs = function (socket, data, callback) {
|
||||
posts.diffs.list(data.pid, callback);
|
||||
};
|
||||
|
||||
SocketPosts.showPostAt = function (socket, data, callback) {
|
||||
posts.diffs.load(data.pid, data.since, socket.uid, callback);
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue