wait for callback

v1.18.x
barisusakli 11 years ago
parent aee8b87385
commit f3f4711a60

@ -9,7 +9,6 @@ var db = require('./database'),
topics = require('./topics'), topics = require('./topics'),
plugins = require('./plugins'), plugins = require('./plugins'),
meta = require('./meta'), meta = require('./meta'),
emitter = require('./emitter'),
validator = require('validator'), validator = require('validator'),
privileges = require('./privileges'), privileges = require('./privileges'),
@ -418,27 +417,35 @@ var db = require('./database'),
], callback); ], callback);
}; };
Categories.onNewPostMade = function(postData) { Categories.onNewPostMade = function(postData, callback) {
topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) { topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) {
if (err) { if (err) {
return winston.error(err.message); return callback(err);
} }
if (!topicData) { if (!topicData) {
return; return callback();
} }
var cid = topicData.cid; var cid = topicData.cid;
db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid); async.parallel([
db.incrObjectField('category:' + cid, 'post_count'); 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) { if(parseInt(topicData.pinned, 10) === 0) {
db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid); db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid, next);
} else {
next();
} }
}
], callback);
}); });
}; };
emitter.on('event:newpost', Categories.onNewPostMade);
}(exports)); }(exports));

@ -211,7 +211,7 @@ var winston = require('winston'),
} }
if (timestamp) { if (timestamp) {
topics.updateTimestamp(tid, timestamp); return topics.updateTimestamp(tid, timestamp, callback);
} }
callback(); callback();
}); });

@ -70,8 +70,16 @@ var async = require('async'),
db.setObject('post:' + postData.pid, postData, next); db.setObject('post:' + postData.pid, postData, next);
}, },
function(next) { function(next) {
emitter.emit('event:newpost', postData);
async.parallel([ async.parallel([
function(next) {
user.onNewPostMade(postData, next);
},
function(next) {
topics.onNewPostMade(postData, next);
},
function(next) {
categories.onNewPostMade(postData, next);
},
function(next) { function(next) {
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
}, },

@ -48,9 +48,13 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
Topics.updateTimestamp(tid, Date.now()); Topics.updateTimestamp(tid, Date.now(), function(err) {
if (err) {
return callback(err);
}
Topics.getTopicData(tid, callback); Topics.getTopicData(tid, callback);
}); });
});
function move(pid, next) { function move(pid, next) {
privileges.posts.canEdit(pid, uid, function(err, canEdit) { privileges.posts.canEdit(pid, uid, function(err, canEdit) {

@ -7,20 +7,26 @@ var async = require('async'),
db = require('../database'), db = require('../database'),
user = require('../user'), user = require('../user'),
emitter = require('../emitter'),
favourites = require('../favourites'), favourites = require('../favourites'),
posts = require('../posts'), posts = require('../posts'),
privileges = require('../privileges'); privileges = require('../privileges');
module.exports = function(Topics) { module.exports = function(Topics) {
Topics.onNewPostMade = function(postData) { Topics.onNewPostMade = function(postData, callback) {
Topics.increasePostCount(postData.tid); async.parallel([
Topics.updateTimestamp(postData.tid, postData.timestamp); function(next) {
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0); 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) { Topics.getTopicPosts = function(tid, set, start, end, uid, reverse, callback) {
callback = callback || function() {}; callback = callback || function() {};

@ -2,7 +2,10 @@
'use strict'; 'use strict';
var db = require('./../database'); var async = require('async'),
db = require('../database');
module.exports = function(Topics) { module.exports = function(Topics) {
var terms = { var terms = {
@ -33,9 +36,15 @@ module.exports = function(Topics) {
db.getSortedSetRevRangeByScore('topics:recent', start, count, Infinity, Date.now() - since, callback); db.getSortedSetRevRangeByScore('topics:recent', start, count, Infinity, Date.now() - since, callback);
}; };
Topics.updateTimestamp = function(tid, timestamp) { Topics.updateTimestamp = function(tid, timestamp, callback) {
Topics.updateRecent(tid, timestamp); async.parallel([
Topics.setTopicField(tid, 'lastposttime', timestamp); function(next) {
Topics.updateRecent(tid, timestamp, next);
},
function(next) {
Topics.setTopicField(tid, 'lastposttime', timestamp, next);
}
], callback);
}; };
Topics.updateRecent = function(tid, timestamp, callback) { Topics.updateRecent = function(tid, timestamp, callback) {

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var var async = require('async'),
async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
gravatar = require('gravatar'), gravatar = require('gravatar'),
@ -9,7 +8,6 @@ var
db = require('./database'), db = require('./database'),
meta = require('./meta'), meta = require('./meta'),
groups = require('./groups'), groups = require('./groups'),
emitter = require('./emitter'),
Password = require('./password'); Password = require('./password');
(function(User) { (function(User) {
@ -298,16 +296,20 @@ var
Password.hash(nconf.get('bcrypt_rounds'), password, callback); Password.hash(nconf.get('bcrypt_rounds'), password, callback);
}; };
User.onNewPostMade = function(postData) { User.onNewPostMade = function(postData, callback) {
User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp); async.parallel([
function(next) {
User.incrementUserPostCountBy(postData.uid, 1); User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp, next);
},
User.setUserField(postData.uid, 'lastposttime', postData.timestamp); 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) { User.incrementUserPostCountBy = function(uid, value, callback) {
callback = callback || function() {}; callback = callback || function() {};
User.incrementUserFieldBy(uid, 'postcount', value, function(err, newpostcount) { User.incrementUserFieldBy(uid, 'postcount', value, function(err, newpostcount) {

Loading…
Cancel
Save