diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 6405fc9a01..de652f0658 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -1,12 +1,11 @@ var topics = require('../topics'), threadTools = require('../threadTools'), - + index = require('./index'), SocketTopics = {}; -SocketTopics.post = function(data, callback, sessionData) { - var socket = sessionData.socket; +SocketTopics.post = function(socket, data, callback) { - if (sessionData.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) { + if (socket.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) { socket.emit('event:alert', { title: 'Post Unsuccessful', message: 'You don't seem to be logged in, so you cannot reply.', @@ -16,7 +15,7 @@ SocketTopics.post = function(data, callback, sessionData) { return; } - topics.post(sessionData.uid, data.title, data.content, data.category_id, function(err, result) { + topics.post(socket.uid, data.title, data.content, data.category_id, function(err, result) { if(err) { if (err.message === 'title-too-short') { module.parent.exports.emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.'); @@ -45,9 +44,9 @@ SocketTopics.post = function(data, callback, sessionData) { } if (result) { - sessionData.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData); - sessionData.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData); - sessionData.server.sockets.in('user/' + sessionData.uid).emit('event:new_post', { + index.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData); + index.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData); + index.server.sockets.in('user/' + socket.uid).emit('event:new_post', { posts: result.postData }); @@ -64,90 +63,131 @@ SocketTopics.post = function(data, callback, sessionData) { }); }; -SocketTopics.postcount = function(tid, callback) { +SocketTopics.postcount = function(socket, tid, callback) { topics.getTopicField(tid, 'postcount', callback); }; -SocketTopics.markAllRead = function(data, callback, sessionData) { - topics.markAllRead(sessionData.uid, function(err, success) { +SocketTopics.markAllRead = function(socket, data, callback) { + topics.markAllRead(socket.uid, function(err, success) { if (!err && success) { callback(true); - sessionData.server.sockets.in('uid_' + sessionData.uid).emit('event:unread.updateCount', 0); + index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', 0); } else { callback(false); } }); }; -SocketTopics.delete = function(data, callback, sessionData) { - threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.delete(data.tid, sessionData.uid, function(err) { - if (!err) { - module.parent.exports.emitTopicPostStats(); - if (callback) { - callback('topic.delete', { - status: 'ok', - tid: data.tid - }); - } - } - }); +SocketTopics.delete = function(socket, data, callback) { + threadTools.privileges(data.tid, socket.uid, function(err, privileges) { + if(err) { + return callback(err); } + + if(!privileges.editable) { + return callback(new Error('not-allowed')); + } + + threadTools.delete(data.tid, socket.uid, function(err) { + if(err) { + return callback(err); + } + + module.parent.exports.emitTopicPostStats(); + + + callback(null, 'topic.delete', { + status: 'ok', + tid: data.tid + }); + }); }); }; -SocketTopics.restore = function(data, callback, sessionData) { - threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.restore(data.tid, sessionData.uid, function(err) { - module.parent.exports.emitTopicPostStats(); - - if (callback) { - callback('topic.restore', { - status: 'ok', - tid: data.tid - }); - } - }); +SocketTopics.restore = function(socket, data, callback) { + threadTools.privileges(data.tid, socket.uid, function(err, privileges) { + if(err) { + return callback(err); + } + + if(!privileges.editable) { + return callback(new Error('not-allowed')); } + + threadTools.restore(data.tid, socket.uid, function(err) { + if(err) { + return callback(err); + } + + module.parent.exports.emitTopicPostStats(); + + callback(null, 'topic.restore', { + status: 'ok', + tid: data.tid + }); + }); + }); }; -SocketTopics.lock = function(data, callback, sessionData) { - threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.lock(data.tid, callback); +SocketTopics.lock = function(socket, data, callback) { + threadTools.privileges(data.tid, socket.uid, function(err, privileges) { + if(err) { + return callback(err); } + + if (!privileges.editable) { + return callback(new Error('not-allowed')); + } + + threadTools.lock(data.tid, callback); }); }; -SocketTopics.unlock = function(data, callback, sessionData) { - threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.unlock(data.tid, callback); +SocketTopics.unlock = function(socket, data, callback) { + threadTools.privileges(data.tid, socket.uid, function(err, privileges) { + if(err) { + return callback(err); } + + if (!privileges.editable) { + return callback(new Error('not-allowed')); + } + + threadTools.unlock(data.tid, callback); }); }; -SocketTopics.pin = function(data, callback, sessionData) { +SocketTopics.pin = function(socket, data, callback) { threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.pin(data.tid, callback); + if(err) { + return callback(err); } + + if (!privileges.editable) { + return callback(new Error('not-allowed')); + } + + threadTools.pin(data.tid, callback); }); }; -SocketTopics.unpin = function(data, callback, sessionData) { - threadTools.privileges(data.tid, sessionData.uid, function(err, privileges) { - if (!err && privileges.editable) { - threadTools.unpin(data.tid, callback); +SocketTopics.unpin = function(socket, data, callback) { + threadTools.privileges(data.tid, socket.uid, function(err, privileges) { + if(err) { + return callback(err); + } + + if (!privileges.editable) { + return callback(new Error('not-allowed')); } + + threadTools.unpin(data.tid, callback); }); }; -SocketTopics.createTopicFromPosts = function(data, callback, sessionData) { - if(!sessionData.uid) { +SocketTopics.createTopicFromPosts = function(socket, data, callback) { + if(!socket.uid) { socket.emit('event:alert', { title: 'Can't fork', message: 'Guests can't fork topics!', @@ -157,13 +197,13 @@ SocketTopics.createTopicFromPosts = function(data, callback, sessionData) { return; } - topics.createTopicFromPosts(sessionData.uid, data.title, data.pids, function(err, data) { - callback(err?{message:err.message}:null, data); + topics.createTopicFromPosts(socket.uid, data.title, data.pids, function(err, data) { + callback(err, data); }); }; -SocketTopics.movePost = function(data, callback, sessionData) { - if(!sessionData.uid) { +SocketTopics.movePost = function(socket, data, callback) { + if(!socket.uid) { socket.emit('event:alert', { title: 'Can't fork', message: 'Guests can't fork topics!', @@ -174,24 +214,34 @@ SocketTopics.movePost = function(data, callback, sessionData) { } topics.movePostToTopic(data.pid, data.tid, function(err, data) { - callback(err?{message:err.message}:null, data); + callback(err, data); }); }; -SocketTopics.move = function(data, callback, sessionData) { - threadTools.move(data.tid, data.cid, callback, sessionData); +SocketTopics.move = function(socket, data, callback) { + threadTools.move(data.tid, data.cid, function(err) { + if(err) { + return callback(err); + } + + index.server.sockets.in('topic_' + data.tid).emit('event:topic_moved', { + tid: tid + }); + }); }; -SocketTopics.followCheck = function(tid, callback, sessionData) { - threadTools.isFollowing(tid, sessionData.uid, function(following) { +SocketTopics.followCheck = function(socket, tid, callback) { + threadTools.isFollowing(tid, socket.uid, function(following) { callback(following); }); }; -SocketTopics.follow = function(tid, callback, sessionData) { - if (sessionData.uid && sessionData.uid > 0) { - threadTools.toggleFollow(tid, sessionData.uid, function(follow) { - if (follow.status === 'ok') callback(follow); +SocketTopics.follow = function(socket, tid, callback) { + if (socket.uid) { + threadTools.toggleFollow(tid, socket.uid, function(follow) { + if (follow.status === 'ok') { + callback(follow); + } }); } else { callback({ @@ -201,36 +251,40 @@ SocketTopics.follow = function(tid, callback, sessionData) { } }; -SocketTopics.loadMore = function(data, callback, sessionData) { +SocketTopics.loadMore = function(socket, data, callback) { var start = data.after, end = start + 9; - topics.getTopicPosts(data.tid, start, end, sessionData.uid, function(err, posts) { - callback({ + topics.getTopicPosts(data.tid, start, end, socket.uid, function(err, posts) { + if(err) { + return callback(err); + + } + callback(null, { posts: posts }); }); }; -SocketTopics.loadMoreRecentTopics = function(data, callback, sessionData) { +SocketTopics.loadMoreRecentTopics = function(socket, data, callback) { var start = data.after, end = start + 9; - topics.getLatestTopics(sessionData.uid, start, end, data.term, function(err, latestTopics) { - if (!err) { - callback(latestTopics); - } else { - winston.error('[socket topics.loadMoreRecentTopics] ' + err.message); + topics.getLatestTopics(socket.uid, start, end, data.term, function(err, latestTopics) { + if(err) { + return callback(err); } + + callback(null, latestTopics); }); }; -SocketTopics.loadMoreUnreadTopics = function(data, callback, sessionData) { +SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) { var start = data.after, end = start + 9; - topics.getUnreadTopics(sessionData.uid, start, end, function(unreadTopics) { - callback(unreadTopics); + topics.getUnreadTopics(socket.uid, start, end, function(unreadTopics) { + callback(null, unreadTopics); }); }; diff --git a/src/threadTools.js b/src/threadTools.js index 6d4fe41abe..aa1ce99dba 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -167,7 +167,7 @@ var winston = require('winston'), } } - ThreadTools.move = function(tid, cid, callback, sessionData) { + ThreadTools.move = function(tid, cid, callback) { topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) { var oldCid = topicData.cid; @@ -197,9 +197,6 @@ var winston = require('winston'), status: 'ok' }); - sessionData.server.sockets.in('topic_' + tid).emit('event:topic_moved', { - tid: tid - }); }); }); });