Node.js based forum software built for the modern web
feat: show editor in post diffs if available
Julian Lamf909ed254187
diff --git a/public/src/client/topic/diffs.js b/public/src/client/topic/diffs.jsindex 1eda09efb9..be6b0364df 100644--- a/public/src/client/topic/diffs.js+++ b/public/src/client/topic/diffs.js@@ -16,10 +16,11 @@ define('forum/topic/diffs', ['forum/topic/images', 'benchpress', 'translator'],}Benchpress.parse('partials/modals/post_history', {- diffs: data.timestamps.map(function (timestamp) {- timestamp = parseInt(timestamp, 10);+ diffs: data.revisions.map(function (revision) {+ var timestamp = parseInt(revision.timestamp, 10);return {+ username: revision.username,timestamp: timestamp,pretty: new Date(timestamp).toLocaleString(config.userLang.replace('_', '-'), localeStringOpts),};diff --git a/src/posts/diffs.js b/src/posts/diffs.jsindex f1635a4da4..eeb44927da 100644--- a/src/posts/diffs.js+++ b/src/posts/diffs.js@@ -23,6 +23,10 @@ module.exports = function (Posts) {Diffs.get = async function (pid, since) {const timestamps = await Diffs.list(pid);+ if (!since) {+ since = 0;+ }+// Pass those made after `since`, and create keysconst keys = timestamps.filter(t => (parseInt(t, 10) || 0) > since).map(t => 'diff:' + pid + '.' + t);@@ -49,11 +53,6 @@ module.exports = function (Posts) {Diffs.load = async function (pid, since, uid) {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 });diff --git a/src/socket.io/posts/diffs.js b/src/socket.io/posts/diffs.jsindex dc69063f14..8cfa3648c9 100644--- a/src/socket.io/posts/diffs.js+++ b/src/socket.io/posts/diffs.js@@ -1,6 +1,7 @@'use strict';const posts = require('../../posts');+const user = require('../../user');const privileges = require('../../privileges');const websockets = require('..');@@ -8,12 +9,24 @@ module.exports = function (SocketPosts) {SocketPosts.getDiffs = async function (socket, data) {await privilegeCheck(data.pid, socket.uid);const timestamps = await posts.diffs.list(data.pid);+ const post = await posts.getPostFields(data.pid, ['timestamp', 'uid']);++ const diffs = await posts.diffs.get(data.pid);+ const uids = diffs.map(diff => diff.uid || null);+ uids.push(post.uid);+ let usernames = await user.getUsersFields(uids, ['username']);+ usernames = usernames.map(userObj => (userObj.uid ? userObj.username : null));+const cid = await posts.getCidByPid(data.pid);const canEdit = await privileges.categories.can('edit', cid, socket.uid);- const postTime = await posts.getPostField(data.pid, 'timestamp');- timestamps.push(postTime);+ timestamps.push(post.timestamp);+return {timestamps: timestamps,+ revisions: timestamps.map((timestamp, idx) => ({+ timestamp: timestamp,+ username: usernames[idx],+ })),editable: canEdit,};};diff --git a/test/posts.js b/test/posts.jsindex 936c32dd9d..9bc9fa09ef 100644--- a/test/posts.js+++ b/test/posts.js@@ -593,10 +593,16 @@ describe('Post\'s', function () {it('should allow registered-users group to view diffs', function (done) {socketPosts.getDiffs({ uid: 1 }, { pid: 1 }, function (err, data) {assert.ifError(err);+assert.strictEqual('boolean', typeof data.editable);assert.strictEqual(false, data.editable);+assert.equal(true, Array.isArray(data.timestamps));assert.strictEqual(1, data.timestamps.length);++ assert.equal(true, Array.isArray(data.revisions));+ assert.strictEqual(data.timestamps.length, data.revisions.length);+ ['timestamp', 'username'].every(prop => Object.keys(data.revisions[0]).includes(prop));done();});});