diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 764ec7cd0a..c3e58d3db9 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -10,6 +10,8 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator PostTools.init = function(tid) { topicName = ajaxify.data.title; + renderMenu(); + addPostHandlers(tid); share.addShareHandlers(topicName); @@ -19,6 +21,30 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator PostTools.updatePostCount(ajaxify.data.postcount); }; + function renderMenu() { + $('[component="topic"]').on('show.bs.dropdown', '.moderator-tools', function() { + var $this = $(this); + var dropdownMenu = $this.find('.dropdown-menu'); + if (dropdownMenu.html()) { + return; + } + var postEl = $this.parents('[data-pid]'); + var pid = postEl.attr('data-pid'); + var index = parseInt(postEl.attr('data-index'), 10); + socket.emit('posts.loadPostTools', {pid: pid, cid: ajaxify.data.cid}, function(err, data) { + if (err) { + return app.alertError(err); + } + data.posts.display_move_tools = data.posts.display_move_tools && index !== 0; + templates.parse('partials/topic/post-menu-list', data, function(html) { + translator.translate(html, function(html) { + dropdownMenu.html(html); + }); + }); + }); + }); + } + PostTools.toggle = function(pid, isDeleted) { var postEl = components.get('post', 'pid', pid); diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js index e5e04b8f5a..fc4491ea44 100644 --- a/src/socket.io/posts/tools.js +++ b/src/socket.io/posts/tools.js @@ -6,10 +6,44 @@ var posts = require('../../posts'); var events = require('../../events'); var websockets = require('../index'); var socketTopics = require('../topics'); +var privileges = require('../../privileges'); +var favourites = require('../../favourites'); module.exports = function(SocketPosts) { + SocketPosts.loadPostTools = function(socket, data, callback) { + if (!socket.uid) { + return; + } + if (!data) { + return callback(new Error('[[error:invalid-data]]')) + } + + async.parallel({ + posts: function(next) { + posts.getPostFields(data.pid, ['deleted', 'reputation', 'uid'], next); + }, + isAdminOrMod: function(next) { + privileges.categories.isAdminOrMod(data.cid, socket.uid, next); + }, + favourited: function(next) { + favourites.getFavouritesByPostIDs([data.pid], socket.uid, next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + results.posts.tools = []; // TODO: add filter for this + results.posts.deleted = parseInt(results.posts.deleted, 10) === 1; + results.posts.favourited = results.favourited[0]; + results.posts.selfPost = socket.uid === parseInt(results.posts.uid, 10); + results.posts.display_moderator_tools = results.isAdminOrMod || results.posts.selfPost; + results.posts.display_move_tools = results.isAdminOrMod; + callback(null, results); + }); + }; + SocketPosts.delete = function(socket, data, callback) { doPostAction('delete', 'event:post_deleted', socket, data, callback); };