From d05920ec784653cd630f7d666841aa337527fe77 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 24 Apr 2014 20:05:05 -0400 Subject: [PATCH] refactor of thread tools --- src/postTools.js | 2 +- src/socket.io/topics.js | 2 +- src/threadTools.js | 154 ++++++++-------------------------------- src/topics.js | 3 +- src/topics/create.js | 3 +- src/topics/follow.js | 62 ++++++++++++++++ src/topics/posts.js | 30 ++++++++ 7 files changed, 127 insertions(+), 129 deletions(-) create mode 100644 src/topics/follow.js diff --git a/src/postTools.js b/src/postTools.js index 3cb73e2282..0bba2135ef 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -212,7 +212,7 @@ var winston = require('winston'), } function updateTopicTimestamp(tid, callback) { - threadTools.getLatestUndeletedPid(tid, function(err, pid) { + topics.getLatestUndeletedPid(tid, function(err, pid) { if(err || !pid) { return callback(err); } diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index af1d835c73..283448a25a 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -300,7 +300,7 @@ SocketTopics.moveAll = function(socket, data, callback) { }; SocketTopics.followCheck = function(socket, tid, callback) { - threadTools.isFollowing(tid, socket.uid, callback); + topics.isFollowing(tid, socket.uid, callback); }; SocketTopics.follow = function(socket, tid, callback) { diff --git a/src/threadTools.js b/src/threadTools.js index 63a29cf95d..0ffbea46d4 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -74,6 +74,12 @@ var winston = require('winston'), } topics[isDelete ? 'delete' : 'restore'](tid, function(err) { + function emitTo(room) { + websockets.in(room).emit(isDelete ? 'event:topic_deleted' : 'event:topic_restored', { + tid: tid, + isDelete: isDelete + }); + } if(err) { return callback(err); } @@ -86,15 +92,8 @@ var winston = require('winston'), websockets.emitTopicPostStats(); - websockets.in('topic_' + tid).emit(isDelete ? 'event:topic_deleted' : 'event:topic_restored', { - tid: tid, - isDelete: isDelete - }); - - websockets.in('category_' + topicData.cid).emit(isDelete ? 'event:topic_deleted' : 'event:topic_restored', { - tid: tid, - isDelete: isDelete - }); + emitTo('topic_' + tid); + emitTo('category_' + topicData.cid); callback(null, { tid: tid @@ -113,21 +112,21 @@ var winston = require('winston'), function toggleLock(tid, uid, lock, callback) { topics.getTopicField(tid, 'cid', function(err, cid) { + function emitTo(room) { + websockets.in(room).emit(lock ? 'event:topic_locked' : 'event:topic_unlocked', { + tid: tid, + isLocked: lock + }); + } + if (err) { return callback(err); } topics.setTopicField(tid, 'locked', lock ? 1 : 0); - websockets.in('topic_' + tid).emit(lock ? 'event:topic_locked' : 'event:topic_unlocked', { - tid: tid, - isLocked: lock - }); - - websockets.in('category_' + cid).emit(lock ? 'event:topic_locked' : 'event:topic_unlocked', { - tid: tid, - isLocked: lock - }); + emitTo('topic_' + tid); + emitTo('category_' + cid); if (typeof callback === 'function') { callback(null, { @@ -148,6 +147,13 @@ var winston = require('winston'), function togglePin(tid, uid, pin, callback) { topics.getTopicField(tid, 'cid', function(err, cid) { + function emitTo(room) { + websockets.in(room).emit(pin ? 'event:topic_pinned' : 'event:topic_unpinned', { + tid: tid, + isPinned: pin + }); + } + if (err) { return callback(err); } @@ -157,15 +163,8 @@ var winston = require('winston'), db.sortedSetAdd('categories:' + topicData.cid + ':tid', pin ? Math.pow(2, 53) : topicData.lastposttime, tid); }); - websockets.in('topic_' + tid).emit(pin ? 'event:topic_pinned' : 'event:topic_unpinned', { - tid: tid, - isPinned: pin - }); - - websockets.in('category_' + cid).emit(pin ? 'event:topic_pinned' : 'event:topic_unpinned', { - tid: tid, - isPinned: pin - }); + emitTo('topic_' + tid); + emitTo('category_' + cid); if (typeof callback === 'function') { callback(null, { @@ -207,17 +206,15 @@ var winston = require('winston'), }); }; - ThreadTools.isFollowing = function(tid, uid, callback) { - db.isSetMember('tid:' + tid + ':followers', uid, callback); - }; + ThreadTools.toggleFollow = function(tid, uid, callback) { - ThreadTools.isFollowing(tid, uid, function(err, following) { + topics.isFollowing(tid, uid, function(err, following) { if(err) { return callback(err); } - db[following?'setRemove':'setAdd']('tid:' + tid + ':followers', uid, function(err, success) { + db[following ? 'setRemove' : 'setAdd']('tid:' + tid + ':followers', uid, function(err, success) { if (callback) { if(err) { return callback(err); @@ -229,97 +226,4 @@ var winston = require('winston'), }); }; - ThreadTools.getFollowers = function(tid, callback) { - db.getSetMembers('tid:' + tid + ':followers', function(err, followers) { - if(err) { - return callback(err); - } - - if(followers) { - followers = followers.map(function(follower) { - return parseInt(follower, 10); - }); - } - callback(null, followers); - }); - }; - - ThreadTools.notifyFollowers = function(tid, pid, exceptUid) { - async.parallel([ - function(next) { - topics.getTopicFields(tid, ['title', 'slug'], function(err, topicData) { - if(err) { - return next(err); - } - - user.getUserField(exceptUid, 'username', function(err, username) { - if(err) { - return next(err); - } - - notifications.create({ - text: '[[notifications:user_posted_to, ' + username + ', ' + topicData.title + ']]', - path: nconf.get('relative_path') + '/topic/' + topicData.slug + '#' + pid, - uniqueId: 'topic:' + tid, - from: exceptUid - }, function(nid) { - next(null, nid); - }); - }); - }); - }, - function(next) { - ThreadTools.getFollowers(tid, function(err, followers) { - if(err) { - return next(err); - } - - exceptUid = parseInt(exceptUid, 10); - if (followers.indexOf(exceptUid) !== -1) { - followers.splice(followers.indexOf(exceptUid), 1); - } - - next(null, followers); - }); - } - ], function(err, results) { - if (!err && results[1].length) { - notifications.push(results[0], results[1]); - } - }); - }; - - ThreadTools.getLatestUndeletedPost = function(tid, callback) { - ThreadTools.getLatestUndeletedPid(tid, function(err, pid) { - if(err) { - return callback(err); - } - - posts.getPostData(pid, callback); - }); - }; - - ThreadTools.getLatestUndeletedPid = function(tid, callback) { - db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, function(err, pids) { - if(err) { - return callback(err); - } - - if (!pids.length) { - return callback(null, null); - } - - async.detectSeries(pids, function(pid, next) { - posts.getPostField(pid, 'deleted', function(err, deleted) { - next(parseInt(deleted, 10) === 0); - }); - }, function(pid) { - if (pid) { - callback(null, pid); - } else { - callback(null, null); - } - }); - }); - }; }(exports)); diff --git a/src/topics.js b/src/topics.js index 7ff3d6e68f..43dd9dadf2 100644 --- a/src/topics.js +++ b/src/topics.js @@ -20,6 +20,7 @@ var async = require('async'), require('./topics/recent')(Topics); require('./topics/fork')(Topics); require('./topics/posts')(Topics); + require('./topics/follow')(Topics); Topics.getTopicData = function(tid, callback) { @@ -304,7 +305,7 @@ var async = require('async'), }; Topics.getTeaser = function(tid, callback) { - threadTools.getLatestUndeletedPid(tid, function(err, pid) { + Topics.getLatestUndeletedPid(tid, function(err, pid) { if (err) { return callback(err); } diff --git a/src/topics/create.js b/src/topics/create.js index f597b53d7d..c9d6482df8 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -180,7 +180,8 @@ module.exports = function(Topics) { }, function(data, next) { postData = data; - threadTools.notifyFollowers(tid, postData.pid, uid); + + Topics.notifyFollowers(tid, postData.pid, uid); user.notifications.sendPostNotificationToFollowers(uid, tid, postData.pid); diff --git a/src/topics/follow.js b/src/topics/follow.js new file mode 100644 index 0000000000..7e86d9a019 --- /dev/null +++ b/src/topics/follow.js @@ -0,0 +1,62 @@ + +'use strict'; + +var async = require('async'), + nconf = require('nconf'), + + db = require('../database'), + user = require('../user'), + notifications = require('../notifications'); + +module.exports = function(Topics) { + + + Topics.isFollowing = function(tid, uid, callback) { + db.isSetMember('tid:' + tid + ':followers', uid, callback); + }; + + Topics.getFollowers = function(tid, callback) { + db.getSetMembers('tid:' + tid + ':followers', callback); + }; + + Topics.notifyFollowers = function(tid, pid, exceptUid) { + async.parallel({ + nid: function(next) { + Topics.getTopicFields(tid, ['title', 'slug'], function(err, topicData) { + if(err) { + return next(err); + } + + user.getUserField(exceptUid, 'username', function(err, username) { + if(err) { + return next(err); + } + + notifications.create({ + text: '[[notifications:user_posted_to, ' + username + ', ' + topicData.title + ']]', + path: nconf.get('relative_path') + '/topic/' + topicData.slug + '#' + pid, + uniqueId: 'topic:' + tid, + from: exceptUid + }, function(nid) { + next(null, nid); + }); + }); + }); + }, + followers: function(next) { + Topics.getFollowers(tid, next); + } + }, function(err, results) { + if (!err && results.followers.length) { + + var index = results.followers.indexOf(exceptUid.toString()); + if (index !== -1) { + results.followers.splice(index, 1); + } + + notifications.push(results.nid, results.followers); + } + }); + }; + +}; \ No newline at end of file diff --git a/src/topics/posts.js b/src/topics/posts.js index 3062c5dfa9..53ff57f5a4 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -80,6 +80,36 @@ module.exports = function(Topics) { }); }; + Topics.getLatestUndeletedPost = function(tid, callback) { + Topics.getLatestUndeletedPid(tid, function(err, pid) { + if(err) { + return callback(err); + } + + posts.getPostData(pid, callback); + }); + }; + + Topics.getLatestUndeletedPid = function(tid, callback) { + db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, function(err, pids) { + if(err) { + return callback(err); + } + + if (!pids || !pids.length) { + return callback(null, null); + } + + async.detectSeries(pids, function(pid, next) { + posts.getPostField(pid, 'deleted', function(err, deleted) { + next(parseInt(deleted, 10) === 0); + }); + }, function(pid) { + callback(null, pid ? pid : null); + }); + }); + }; + Topics.addPostToTopic = function(tid, pid, timestamp, callback) { db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback); };