diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 18402c1f58..9d8e878477 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -195,7 +195,21 @@ define('forum/topic', [ function addParentHandler() { components.get('topic').on('click', '[component="post/parent"]', function() { - navigator.scrollToPost(parseInt(this.getAttribute('data-index'), 10), true); + var toPid = $(this).attr('data-topid'); + var content = $(this).parents('[component="post"]').find('[component="post/parent/content"]'); + if (!content.hasClass('hidden')) { + return content.addClass('hidden'); + } else if (content.html().length) { + return content.removeClass('hidden'); + } + + socket.emit('posts.getPost', toPid, function(err, post) { + if (err) { + return app.alertError(err.message); + } + + content.html(post.content).removeClass('hidden'); + }); }); } diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 9e81c09993..c268f133fd 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -145,6 +145,29 @@ SocketPosts.getRawPost = function(socket, pid, callback) { ], callback); }; +SocketPosts.getPost = function(socket, pid, callback) { + async.waterfall([ + function(next) { + privileges.posts.can('read', pid, socket.uid, next); + }, + function(canRead, next) { + if (!canRead) { + return next(new Error('[[error:no-privileges]]')); + } + posts.getPostsByPids([pid], socket.uid, next); + }, + function(postData, next) { + if (!postData.length) { + return next(new Error('[[error:no-post]]')); + } + postData = postData[0]; + if (parseInt(postData.deleted, 10) === 1) { + return next(new Error('[[error:no-post]]')); + } + next(null, postData); + } + ], callback); +} SocketPosts.getPrivileges = function(socket, pids, callback) { privileges.posts.get(pids, socket.uid, function(err, privileges) { diff --git a/src/topics/posts.js b/src/topics/posts.js index ba2a5eba13..60f2dbe8ce 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -115,42 +115,36 @@ module.exports = function(Topics) { }, parents: function(next) { var parentPids = postData.map(function(postObj) { - return postObj.hasOwnProperty('toPid') ? parseInt(postObj.toPid, 10) : null - }).filter(Boolean), - parentUids; - - if (parentPids) { - async.waterfall([ - async.apply(posts.getPostsFields, parentPids, ['pid', 'tid', 'uid']), - function(postsArr, next) { - // To use Posts.getUserInfoForPosts would be overkill here... - parentUids = postsArr.map(function(postObj) { return parseInt(postObj.uid, 10); }).filter(function(uid, idx, users) { - return users.indexOf(uid) === idx; - }); - - user.getUsersFields(parentUids, ['username'], function(err, userDataArr) { - var userData = {}; - userDataArr.forEach(function(user) { - userData[user.uid] = user; - }); - next(err, postsArr, userData); - }); - }, - function(postsArr, userData, next) { - var returnData = {}; - posts.getPostIndices(postsArr, uid, function(err, indices) { - postsArr.forEach(function(post, idx) { - var pid = parseInt(post.pid, 10); - returnData[pid] = _.clone(userData[parseInt(post.uid, 10)]); - returnData[pid].index = indices[idx]+0; - }); - next(err, returnData); - }); - } - ], next); - } else { - next(); + return postObj.hasOwnProperty('toPid') ? parseInt(postObj.toPid, 10) : null; + }).filter(Boolean); + var parentUids; + + if (!parentPids.length) { + return next(null, []); } + var parentPosts; + async.waterfall([ + async.apply(posts.getPostsFields, parentPids, ['pid', 'uid']), + function(_parentPosts, next) { + parentPosts = _parentPosts; + parentUids = parentPosts.map(function(postObj) { return parseInt(postObj.uid, 10); }).filter(function(uid, idx, users) { + return users.indexOf(uid) === idx; + }); + + user.getUsersFields(parentUids, ['username'], next); + }, + function (userData, next) { + var usersMap = {}; + userData.forEach(function(user) { + usersMap[user.uid] = user.username; + }); + var parents = {}; + parentPosts.forEach(function(post, i) { + parents[parentPids[i]] = {username: usersMap[post.uid]}; + }); + next(null, parents); + } + ], next); } }, function(err, results) { if (err) {