diff --git a/src/socket.io/posts/helpers.js b/src/socket.io/posts/helpers.js index 657ab0f64a..4c04f82afc 100644 --- a/src/socket.io/posts/helpers.js +++ b/src/socket.io/posts/helpers.js @@ -13,59 +13,64 @@ helpers.postCommand = function (socket, command, eventName, notification, data, if (!socket.uid) { return callback(new Error('[[error:not-logged-in]]')); } + if (!data || !data.pid || !data.room_id) { return callback(new Error('[[error:invalid-data]]')); } - async.parallel({ - exists: function (next) { - posts.exists(data.pid, next); - }, - deleted: function (next) { - posts.getPostField(data.pid, 'deleted', next); - } - }, function (err, results) { - if (err || !results.exists) { - return callback(err || new Error('[[error:invalid-pid]]')); - } - if (parseInt(results.deleted, 10) === 1) { - return callback(new Error('[[error:post-deleted]]')); - } + async.waterfall([ + function (next) { + async.parallel({ + exists: function (next) { + posts.exists(data.pid, next); + }, + deleted: function (next) { + posts.getPostField(data.pid, 'deleted', next); + } + }, next); + }, + function (results, next) { + if (!results.exists) { + return next(new Error('[[error:invalid-pid]]')); + } - /* - hooks: - filter:post.upvote - filter:post.downvote - filter:post.unvote - filter:post.bookmark - filter:post.unbookmark - */ - plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function (err, filteredData) { - if (err) { - return callback(err); + if (parseInt(results.deleted, 10) === 1) { + return next(new Error('[[error:post-deleted]]')); } - executeCommand(socket, command, eventName, notification, filteredData.data, callback); - }); - }); + /* + hooks: + filter:post.upvote + filter:post.downvote + filter:post.unvote + filter:post.bookmark + filter:post.unbookmark + */ + plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, next); + }, + function (filteredData, next) { + executeCommand(socket, command, eventName, notification, filteredData.data, next); + } + ], callback); }; function executeCommand(socket, command, eventName, notification, data, callback) { - posts[command](data.pid, socket.uid, function (err, result) { - if (err) { - return callback(err); - } - - if (result && eventName) { - socket.emit('posts.' + command, result); - websockets.in(data.room_id).emit('event:' + eventName, result); - } + async.waterfall([ + function (next) { + posts[command](data.pid, socket.uid, next); + }, + function (result, next) { + if (result && eventName) { + websockets.in('uid_' + socket.uid).emit('posts.' + command, result); + websockets.in(data.room_id).emit('event:' + eventName, result); + } - if (result && notification) { - socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification); - } else if (result && command === 'unvote') { - socketHelpers.rescindUpvoteNotification(data.pid, socket.uid); + if (result && notification) { + socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification); + } else if (result && command === 'unvote') { + socketHelpers.rescindUpvoteNotification(data.pid, socket.uid); + } + next(null, result); } - callback(); - }); + ], callback); } \ No newline at end of file diff --git a/test/posts.js b/test/posts.js index 6f940eec12..207406fd5d 100644 --- a/test/posts.js +++ b/test/posts.js @@ -66,9 +66,9 @@ describe('Post\'s', function () { }); describe('voting', function () { - + var socketPosts = require('../src/socket.io/posts'); it('should upvote a post', function (done) { - posts.upvote(postData.pid, voterUid, function (err, result) { + socketPosts.upvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) { assert.ifError(err); assert.equal(result.post.upvotes, 1); assert.equal(result.post.downvotes, 0); @@ -83,8 +83,28 @@ describe('Post\'s', function () { }); }); + it('should get voters', function (done) { + socketPosts.getVoters({uid: globalModUid}, {pid: postData.pid, cid: cid}, function (err, data) { + assert.ifError(err); + assert.equal(data.upvoteCount, 1); + assert.equal(data.downvoteCount, 0); + assert(Array.isArray(data.upvoters)); + assert.equal(data.upvoters[0].username, 'upvoter'); + done(); + }); + }); + + it('should get upvoters', function (done) { + socketPosts.getUpvoters({uid: globalModUid}, [postData.pid], function (err, data) { + assert.ifError(err); + assert.equal(data[0].otherCount, 0); + assert.equal(data[0].usernames, 'upvoter'); + done(); + }); + }); + it('should unvote a post', function (done) { - posts.unvote(postData.pid, voterUid, function (err, result) { + socketPosts.unvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) { assert.ifError(err); assert.equal(result.post.upvotes, 0); assert.equal(result.post.downvotes, 0); @@ -100,7 +120,7 @@ describe('Post\'s', function () { }); it('should downvote a post', function (done) { - posts.downvote(postData.pid, voterUid, function (err, result) { + socketPosts.downvote({uid: voterUid}, {pid: postData.pid, room_id: 'topic_1'}, function (err, result) { assert.ifError(err); assert.equal(result.post.upvotes, 0); assert.equal(result.post.downvotes, 1);