From 8efc33d6a1ed06480fcebf6a859d4feef0bfcd24 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 17 Mar 2014 17:27:42 -0400 Subject: [PATCH] closes #976 --- src/categories.js | 16 +++++++++++----- src/posts.js | 5 ++--- src/topics.js | 13 ++++++++----- src/user.js | 13 ++++++++----- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/categories.js b/src/categories.js index e75ea055af..46673d0873 100644 --- a/src/categories.js +++ b/src/categories.js @@ -10,6 +10,7 @@ var db = require('./database'), plugins = require('./plugins'), CategoryTools = require('./categoryTools'), meta = require('./meta'), + emitter = require('./emitter'), async = require('async'), winston = require('winston'), @@ -299,19 +300,24 @@ var db = require('./database'), }); }; - Categories.onNewPostMade = function(uid, tid, pid, timestamp) { - topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { + Categories.onNewPostMade = function(postData) { + topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) { + if (err) { + winston.error(err.message); + } var cid = topicData.cid; - db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid); + db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid); if(parseInt(topicData.pinned, 10) === 0) { - db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); + db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid); } - Categories.addActiveUser(cid, uid, timestamp); + Categories.addActiveUser(cid, postData.uid, postData.timestamp); }); }; + emitter.on('newpost', Categories.onNewPostMade); + }(exports)); \ No newline at end of file diff --git a/src/posts.js b/src/posts.js index dff6af855d..ce28617cda 100644 --- a/src/posts.js +++ b/src/posts.js @@ -9,6 +9,7 @@ var db = require('./database'), categories = require('./categories'), plugins = require('./plugins'), meta = require('./meta'), + emitter = require('./emitter'), async = require('async'), path = require('path'), @@ -68,9 +69,7 @@ var db = require('./database'), db.incrObjectField('global', 'postCount'); - topics.onNewPostMade(tid, postData.pid, timestamp); - categories.onNewPostMade(uid, tid, postData.pid, timestamp); - user.onNewPostMade(uid, tid, postData.pid, timestamp); + emitter.emit('newpost', postData); plugins.fireHook('filter:post.get', postData, next); }, diff --git a/src/topics.js b/src/topics.js index 032e3ce440..f34a6dde94 100644 --- a/src/topics.js +++ b/src/topics.js @@ -21,7 +21,8 @@ var async = require('async'), notifications = require('./notifications'), favourites = require('./favourites'), meta = require('./meta'), - Plugins = require('./plugins'); + Plugins = require('./plugins'), + emitter = require('./emitter'); (function(Topics) { @@ -1015,12 +1016,14 @@ var async = require('async'), Topics.setTopicField(tid, 'lastposttime', timestamp); }; - Topics.onNewPostMade = function(tid, pid, timestamp, callback) { - Topics.increasePostCount(tid); - Topics.updateTimestamp(tid, timestamp); - Topics.addPostToTopic(tid, pid, timestamp, callback); + Topics.onNewPostMade = function(postData) { + Topics.increasePostCount(postData.tid); + Topics.updateTimestamp(postData.tid, postData.timestamp); + Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp); }; + emitter.on('newpost', Topics.onNewPostMade); + Topics.addPostToTopic = function(tid, pid, timestamp, callback) { db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback); }; diff --git a/src/user.js b/src/user.js index 89e88f490b..d0e32f4486 100644 --- a/src/user.js +++ b/src/user.js @@ -15,6 +15,7 @@ var bcrypt = require('bcryptjs'), groups = require('./groups'), topics = require('./topics'), events = require('./events'), + emitter = require('./emitter'), Emailer = require('./emailer'); (function(User) { @@ -339,16 +340,18 @@ var bcrypt = require('bcryptjs'), }); }; - User.onNewPostMade = function(uid, tid, pid, timestamp) { - User.addPostIdToUser(uid, pid, timestamp); + User.onNewPostMade = function(postData) { + User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp); - User.incrementUserFieldBy(uid, 'postcount', 1, function(err, newpostcount) { - db.sortedSetAdd('users:postcount', newpostcount, uid); + User.incrementUserFieldBy(postData.uid, 'postcount', 1, function(err, newpostcount) { + db.sortedSetAdd('users:postcount', newpostcount, postData.uid); }); - User.setUserField(uid, 'lastposttime', timestamp); + User.setUserField(postData.uid, 'lastposttime', postData.timestamp); }; + emitter.on('newpost', User.onNewPostMade); + User.addPostIdToUser = function(uid, pid, timestamp) { db.sortedSetAdd('uid:' + uid + ':posts', timestamp, pid); };