Skip to content

Node.js based forum software built for the modern web

closes #3689

barisusakli75e222898981
diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js
index e19e1f9fa4..36df0b3d1f 100644
--- a/public/src/client/topic/posts.js
+++ b/public/src/client/topic/posts.js
@@ -22,6 +22,12 @@ define('forum/topic/posts', [
return;
}
+ data.posts.forEach(function(post) {
+ post.selfPost = !!app.user.uid && parseInt(post.uid, 10) === parseInt(app.user.uid, 10);
+ post.display_moderator_tools = post.selfPost || ajaxify.data.isAdminOrMod;
+ post.display_move_tools = ajaxify.data.isAdminOrMod;
+ });
+
updatePostCounts(data.posts);
if (config.usePagination) {
@@ -154,50 +160,12 @@ define('forum/topic/posts', [
infinitescroll.removeExtra(components.get('post'), direction, 40);
- var pids = [];
- for(var i=0; i<data.posts.length; ++i) {
- pids.push(data.posts[i].pid);
- }
-
$(window).trigger('action:posts.loaded', {posts: data.posts});
- onNewPostsLoaded(html, pids);
- callback(html);
- });
- }
- function onNewPostsLoaded(html, pids) {
- if (app.user.uid) {
- socket.emit('posts.getPrivileges', pids, function(err, privileges) {
- if(err) {
- return app.alertError(err.message);
- }
+ Posts.processPage(html);
- for(var i=0; i<pids.length; ++i) {
- toggleModTools(pids[i], privileges[i]);
- }
- });
- } else {
- for(var i=0; i<pids.length; ++i) {
- toggleModTools(pids[i], {editable: false, move: false});
- }
- }
-
- Posts.processPage(html);
- }
-
- function toggleModTools(pid, privileges) {
- var postEl = components.get('post', 'pid', pid),
- isSelfPost = parseInt(postEl.attr('data-uid'), 10) === parseInt(app.user.uid, 10);
-
- if (!privileges.editable) {
- postEl.find('[component="post/edit"], [component="post/delete"], [component="post/purge"]').remove();
- }
-
- if (!privileges.move) {
- postEl.find('[component="post/move"]').remove();
- }
-
- postEl.find('[component="user/chat"], [component="post/flag"]').toggleClass('hidden', isSelfPost || !app.user.uid);
+ callback(html);
+ });
}
Posts.loadMorePosts = function(direction) {
diff --git a/src/privileges/topics.js b/src/privileges/topics.js
index 53a69c4552..ab0ee8fe74 100644
--- a/src/privileges/topics.js
+++ b/src/privileges/topics.js
@@ -51,6 +51,7 @@ module.exports = function(privileges) {
editable: editable,
deletable: deletable,
view_deleted: isAdminOrMod || results.isOwner,
+ isAdminOrMod: isAdminOrMod,
disabled: disabled,
tid: tid,
uid: uid
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index ee2dc44420..605fafd475 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -145,20 +145,6 @@ SocketPosts.getRawPost = function(socket, pid, callback) {
], callback);
};
-SocketPosts.getPrivileges = function(socket, pids, callback) {
- privileges.posts.get(pids, socket.uid, function(err, privileges) {
- if (err) {
- return callback(err);
- }
- if (!Array.isArray(privileges) || !privileges.length) {
- return callback(new Error('[[error:invalid-data]]'));
- }
-
- callback(null, privileges);
- });
-};
-
-
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback);
};
diff --git a/src/topics/create.js b/src/topics/create.js
index dd6e993c27..ae60a8c191 100644
--- a/src/topics/create.js
+++ b/src/topics/create.js
@@ -283,6 +283,9 @@ module.exports = function(Topics) {
topicInfo: function(next) {
Topics.getTopicFields(tid, ['tid', 'title', 'slug', 'cid', 'postcount'], next);
},
+ parents: function(next) {
+ Topics.addParentPosts([postData], next);
+ },
content: function(next) {
posts.parsePost(postData, next);
}
diff --git a/src/topics/posts.js b/src/topics/posts.js
index 60f2dbe8ce..521a101097 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -114,37 +114,7 @@ module.exports = function(Topics) {
privileges.posts.get(pids, uid, next);
},
parents: function(next) {
- var parentPids = postData.map(function(postObj) {
- 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);
+ Topics.addParentPosts(postData, next);
}
}, function(err, results) {
if (err) {
@@ -162,10 +132,9 @@ module.exports = function(Topics) {
postObj.votes = postObj.votes || 0;
postObj.display_moderator_tools = results.privileges[i].editable;
postObj.display_move_tools = results.privileges[i].move && postObj.index !== 0;
- postObj.selfPost = parseInt(uid, 10) === parseInt(postObj.uid, 10);
- postObj.parent = results.parents[parseInt(postObj.toPid, 10)];
+ postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
- if(postObj.deleted && !results.privileges[i].view_deleted) {
+ if (postObj.deleted && !results.privileges[i].view_deleted) {
postObj.content = '[[topic:post_is_deleted]]';
}
@@ -180,6 +149,44 @@ module.exports = function(Topics) {
});
};
+ Topics.addParentPosts = function(postData, callback) {
+ var parentPids = postData.map(function(postObj) {
+ return postObj && postObj.hasOwnProperty('toPid') ? parseInt(postObj.toPid, 10) : null;
+ }).filter(Boolean);
+
+ if (!parentPids.length) {
+ return callback();
+ }
+
+ var parentPosts;
+ async.waterfall([
+ async.apply(posts.getPostsFields, parentPids, ['uid']),
+ function(_parentPosts, next) {
+ parentPosts = _parentPosts;
+ var 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]};
+ });
+
+ postData.forEach(function(post) {
+ post.parent = parents[post.toPid];
+ });
+ next();
+ }
+ ], callback);
+ };
+
Topics.calculatePostIndices = function(posts, start, stop, postCount, reverse) {
posts.forEach(function(post, index) {
if (reverse) {