From f3f4711a607c4187a75a7d4a3bac20f0baa02b4c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 19 Sep 2014 15:54:13 -0400 Subject: [PATCH] wait for callback --- src/categories.js | 29 ++++++++++++++++++----------- src/postTools.js | 2 +- src/posts.js | 10 +++++++++- src/topics/fork.js | 8 ++++++-- src/topics/posts.js | 18 ++++++++++++------ src/topics/recent.js | 17 +++++++++++++---- src/user.js | 24 +++++++++++++----------- 7 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/categories.js b/src/categories.js index 1a0f159f2d..75ec29705b 100644 --- a/src/categories.js +++ b/src/categories.js @@ -9,7 +9,6 @@ var db = require('./database'), topics = require('./topics'), plugins = require('./plugins'), meta = require('./meta'), - emitter = require('./emitter'), validator = require('validator'), privileges = require('./privileges'), @@ -418,27 +417,35 @@ var db = require('./database'), ], callback); }; - Categories.onNewPostMade = function(postData) { + Categories.onNewPostMade = function(postData, callback) { topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) { if (err) { - return winston.error(err.message); + return callback(err); } if (!topicData) { - return; + return callback(); } var cid = topicData.cid; - db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid); - db.incrObjectField('category:' + cid, 'post_count'); - - if(parseInt(topicData.pinned, 10) === 0) { - db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid); - } + async.parallel([ + function(next) { + db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid, next); + }, + function(next) { + db.incrObjectField('category:' + cid, 'post_count', next); + }, + function(next) { + if(parseInt(topicData.pinned, 10) === 0) { + db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid, next); + } else { + next(); + } + } + ], callback); }); }; - emitter.on('event:newpost', Categories.onNewPostMade); }(exports)); diff --git a/src/postTools.js b/src/postTools.js index 595941fb09..cfbd9d504f 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -211,7 +211,7 @@ var winston = require('winston'), } if (timestamp) { - topics.updateTimestamp(tid, timestamp); + return topics.updateTimestamp(tid, timestamp, callback); } callback(); }); diff --git a/src/posts.js b/src/posts.js index ed3d5efa9b..4f9bbf9488 100644 --- a/src/posts.js +++ b/src/posts.js @@ -70,8 +70,16 @@ var async = require('async'), db.setObject('post:' + postData.pid, postData, next); }, function(next) { - emitter.emit('event:newpost', postData); async.parallel([ + function(next) { + user.onNewPostMade(postData, next); + }, + function(next) { + topics.onNewPostMade(postData, next); + }, + function(next) { + categories.onNewPostMade(postData, next); + }, function(next) { db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); }, diff --git a/src/topics/fork.js b/src/topics/fork.js index 4d276f16fb..4e562781bb 100644 --- a/src/topics/fork.js +++ b/src/topics/fork.js @@ -48,8 +48,12 @@ module.exports = function(Topics) { return callback(err); } - Topics.updateTimestamp(tid, Date.now()); - Topics.getTopicData(tid, callback); + Topics.updateTimestamp(tid, Date.now(), function(err) { + if (err) { + return callback(err); + } + Topics.getTopicData(tid, callback); + }); }); function move(pid, next) { diff --git a/src/topics/posts.js b/src/topics/posts.js index 0b29527939..ee769add0e 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -7,20 +7,26 @@ var async = require('async'), db = require('../database'), user = require('../user'), - emitter = require('../emitter'), favourites = require('../favourites'), posts = require('../posts'), privileges = require('../privileges'); module.exports = function(Topics) { - Topics.onNewPostMade = function(postData) { - Topics.increasePostCount(postData.tid); - Topics.updateTimestamp(postData.tid, postData.timestamp); - Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0); + Topics.onNewPostMade = function(postData, callback) { + async.parallel([ + function(next) { + Topics.increasePostCount(postData.tid, next); + }, + function(next) { + Topics.updateTimestamp(postData.tid, postData.timestamp, next); + }, + function(next) { + Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next); + } + ], callback); }; - emitter.on('event:newpost', Topics.onNewPostMade); Topics.getTopicPosts = function(tid, set, start, end, uid, reverse, callback) { callback = callback || function() {}; diff --git a/src/topics/recent.js b/src/topics/recent.js index 77d8023d32..e94dbe23a8 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -2,7 +2,10 @@ 'use strict'; -var db = require('./../database'); +var async = require('async'), + db = require('../database'); + + module.exports = function(Topics) { var terms = { @@ -33,9 +36,15 @@ module.exports = function(Topics) { db.getSortedSetRevRangeByScore('topics:recent', start, count, Infinity, Date.now() - since, callback); }; - Topics.updateTimestamp = function(tid, timestamp) { - Topics.updateRecent(tid, timestamp); - Topics.setTopicField(tid, 'lastposttime', timestamp); + Topics.updateTimestamp = function(tid, timestamp, callback) { + async.parallel([ + function(next) { + Topics.updateRecent(tid, timestamp, next); + }, + function(next) { + Topics.setTopicField(tid, 'lastposttime', timestamp, next); + } + ], callback); }; Topics.updateRecent = function(tid, timestamp, callback) { diff --git a/src/user.js b/src/user.js index 2271fc3c77..ad2d5852f9 100644 --- a/src/user.js +++ b/src/user.js @@ -1,7 +1,6 @@ 'use strict'; -var - async = require('async'), +var async = require('async'), nconf = require('nconf'), gravatar = require('gravatar'), @@ -9,7 +8,6 @@ var db = require('./database'), meta = require('./meta'), groups = require('./groups'), - emitter = require('./emitter'), Password = require('./password'); (function(User) { @@ -298,16 +296,20 @@ var Password.hash(nconf.get('bcrypt_rounds'), password, callback); }; - User.onNewPostMade = function(postData) { - User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp); - - User.incrementUserPostCountBy(postData.uid, 1); - - User.setUserField(postData.uid, 'lastposttime', postData.timestamp); + User.onNewPostMade = function(postData, callback) { + async.parallel([ + function(next) { + User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp, next); + }, + function(next) { + User.incrementUserPostCountBy(postData.uid, 1, next); + }, + function(next) { + User.setUserField(postData.uid, 'lastposttime', postData.timestamp, next); + } + ], callback); }; - emitter.on('event:newpost', User.onNewPostMade); - User.incrementUserPostCountBy = function(uid, value, callback) { callback = callback || function() {}; User.incrementUserFieldBy(uid, 'postcount', value, function(err, newpostcount) {