Skip to content

Node.js based forum software built for the modern web

waterfall for topics

barisusakli8f98d03239f2
diff --git a/src/topics.js b/src/topics.js
index 9da377f58d..fd9e19ca24 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -97,7 +97,7 @@ var async = require('async'),
user.getSettings(uid, next);
}
}, function(err, results) {
- if(err) {
+ if (err) {
return callback(err);
}
callback(null, Math.ceil((results.index + 1) / results.settings.topicsPerPage));
@@ -107,7 +107,7 @@ var async = require('async'),
Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
- callback(err);
+ return callback(err);
}
categories.getCategoryData(cid, callback);
@@ -144,43 +144,44 @@ var async = require('async'),
return callback(null, []);
}
- Topics.getTopicsData(tids, function(err, topics) {
- function mapFilter(array, field) {
- return array.map(function(topic) {
- return topic && topic[field] && topic[field].toString();
- }).filter(function(value, index, array) {
- return utils.isNumber(value) && array.indexOf(value) === index;
- });
- }
+ var uids, cids, topics;
- if (err) {
- return callback(err);
- }
-
- var uids = mapFilter(topics, 'uid');
- var cids = mapFilter(topics, 'cid');
-
- async.parallel({
- teasers: function(next) {
- Topics.getTeasers(topics, next);
- },
- users: function(next) {
- user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
- },
- categories: function(next) {
- categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
- },
- hasRead: function(next) {
- Topics.hasReadTopics(tids, uid, next);
- },
- tags: function(next) {
- Topics.getTopicsTagsObjects(tids, next);
- }
- }, function(err, results) {
- if (err) {
- return callback(err);
+ async.waterfall([
+ function(next) {
+ Topics.getTopicsData(tids, next);
+ },
+ function(_topics, next) {
+ function mapFilter(array, field) {
+ return array.map(function(topic) {
+ return topic && topic[field] && topic[field].toString();
+ }).filter(function(value, index, array) {
+ return utils.isNumber(value) && array.indexOf(value) === index;
+ });
}
+ topics = _topics;
+ uids = mapFilter(topics, 'uid');
+ cids = mapFilter(topics, 'cid');
+
+ async.parallel({
+ users: function(next) {
+ user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
+ },
+ categories: function(next) {
+ categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
+ },
+ hasRead: function(next) {
+ Topics.hasReadTopics(tids, uid, next);
+ },
+ teasers: function(next) {
+ Topics.getTeasers(topics, next);
+ },
+ tags: function(next) {
+ Topics.getTopicsTagsObjects(tids, next);
+ }
+ }, next);
+ },
+ function(results, next) {
var users = _.object(uids, results.users);
var categories = _.object(cids, results.categories);
@@ -204,31 +205,36 @@ var async = require('async'),
return topic && topic.category && !topic.category.disabled;
});
- plugins.fireHook('filter:topics.get', {topics: topics, uid: uid}, function(err, topicData) {
- callback(err, topicData.topics);
- });
- });
- });
+ plugins.fireHook('filter:topics.get', {topics: topics, uid: uid}, next);
+ },
+ function(data, next) {
+ next(null, data.topics);
+ }
+ ], callback);
};
Topics.getTopicWithPosts = function(tid, set, uid, start, stop, reverse, callback) {
- Topics.getTopicData(tid, function(err, topicData) {
- if (err || !topicData) {
- return callback(err || new Error('[[error:no-topic]]'));
- }
-
- async.parallel({
- posts: async.apply(getMainPostAndReplies, topicData, set, uid, start, stop, reverse),
- category: async.apply(Topics.getCategoryData, tid),
- threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
- tags: async.apply(Topics.getTopicTagsObjects, tid),
- isFollowing: async.apply(Topics.isFollowing, [tid], uid),
- bookmark: async.apply(Topics.getUserBookmark, tid, uid)
- }, function(err, results) {
- if (err) {
- return callback(err);
+ var topicData;
+ async.waterfall([
+ function(next) {
+ Topics.getTopicData(tid, next);
+ },
+ function(_topicData, next) {
+ topicData = _topicData;
+ if (!topicData) {
+ return callback(new Error('[[error:no-topic]]'));
}
+ async.parallel({
+ posts: async.apply(getMainPostAndReplies, topicData, set, uid, start, stop, reverse),
+ category: async.apply(Topics.getCategoryData, tid),
+ threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
+ tags: async.apply(Topics.getTopicTagsObjects, tid),
+ isFollowing: async.apply(Topics.isFollowing, [tid], uid),
+ bookmark: async.apply(Topics.getUserBookmark, tid, uid)
+ }, next);
+ },
+ function(results, next) {
topicData.posts = results.posts;
topicData.category = results.category;
topicData.thread_tools = results.threadTools.tools;
@@ -241,11 +247,12 @@ var async = require('async'),
topicData.locked = parseInt(topicData.locked, 10) === 1;
topicData.pinned = parseInt(topicData.pinned, 10) === 1;
- plugins.fireHook('filter:topic.get', {topic: topicData, uid: uid}, function(err, data) {
- callback(err, data ? data.topic : null);
- });
- });
- });
+ plugins.fireHook('filter:topic.get', {topic: topicData, uid: uid}, next);
+ },
+ function(data, next) {
+ next(null, data.topic);
+ }
+ ], callback);
};
function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback) {
diff --git a/src/topics/user.js b/src/topics/user.js
index 64651c2fd1..4db380efdf 100644
--- a/src/topics/user.js
+++ b/src/topics/user.js
@@ -2,7 +2,8 @@
'use strict';
-var db = require('../database'),
+var async = require('async'),
+ db = require('../database'),
posts = require('../posts');
@@ -19,24 +20,22 @@ module.exports = function(Topics) {
};
Topics.getUids = function(tid, callback) {
- Topics.getPids(tid, function(err, pids) {
- if (err) {
- return callback(err);
- }
-
- posts.getPostsFields(pids, ['uid'], function(err, postData) {
- if (err) {
- return callback(err);
- }
-
+ async.waterfall([
+ function(next) {
+ Topics.getPids(tid, next);
+ },
+ function(pids, next) {
+ posts.getPostsFields(pids, ['uid'], next);
+ },
+ function(postData, next) {
var uids = postData.map(function(post) {
return post && post.uid;
}).filter(function(uid, index, array) {
- return array.indexOf(uid) === index;
+ return uid && array.indexOf(uid) === index;
});
- callback(null, uids);
- });
- });
+ next(null, uids);
+ }
+ ], callback);
};
};
\ No newline at end of file