waterfall for topics

v1.18.x
barisusakli 10 years ago
parent 0534da0ed1
commit 8f98d03239

@ -107,7 +107,7 @@ var async = require('async'),
Topics.getCategoryData = function(tid, callback) { Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) { Topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) { if (err) {
callback(err); return callback(err);
} }
categories.getCategoryData(cid, callback); categories.getCategoryData(cid, callback);
@ -144,7 +144,13 @@ var async = require('async'),
return callback(null, []); return callback(null, []);
} }
Topics.getTopicsData(tids, function(err, topics) { var uids, cids, topics;
async.waterfall([
function(next) {
Topics.getTopicsData(tids, next);
},
function(_topics, next) {
function mapFilter(array, field) { function mapFilter(array, field) {
return array.map(function(topic) { return array.map(function(topic) {
return topic && topic[field] && topic[field].toString(); return topic && topic[field] && topic[field].toString();
@ -153,17 +159,11 @@ var async = require('async'),
}); });
} }
if (err) { topics = _topics;
return callback(err); uids = mapFilter(topics, 'uid');
} cids = mapFilter(topics, 'cid');
var uids = mapFilter(topics, 'uid');
var cids = mapFilter(topics, 'cid');
async.parallel({ async.parallel({
teasers: function(next) {
Topics.getTeasers(topics, next);
},
users: function(next) { users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next); user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
}, },
@ -173,14 +173,15 @@ var async = require('async'),
hasRead: function(next) { hasRead: function(next) {
Topics.hasReadTopics(tids, uid, next); Topics.hasReadTopics(tids, uid, next);
}, },
teasers: function(next) {
Topics.getTeasers(topics, next);
},
tags: function(next) { tags: function(next) {
Topics.getTopicsTagsObjects(tids, next); Topics.getTopicsTagsObjects(tids, next);
} }
}, function(err, results) { }, next);
if (err) { },
return callback(err); function(results, next) {
}
var users = _.object(uids, results.users); var users = _.object(uids, results.users);
var categories = _.object(cids, results.categories); var categories = _.object(cids, results.categories);
@ -204,17 +205,24 @@ var async = require('async'),
return topic && topic.category && !topic.category.disabled; return topic && topic.category && !topic.category.disabled;
}); });
plugins.fireHook('filter:topics.get', {topics: topics, uid: uid}, function(err, topicData) { plugins.fireHook('filter:topics.get', {topics: topics, uid: uid}, next);
callback(err, topicData.topics); },
}); function(data, next) {
}); next(null, data.topics);
}); }
], callback);
}; };
Topics.getTopicWithPosts = function(tid, set, uid, start, stop, reverse, callback) { Topics.getTopicWithPosts = function(tid, set, uid, start, stop, reverse, callback) {
Topics.getTopicData(tid, function(err, topicData) { var topicData;
if (err || !topicData) { async.waterfall([
return callback(err || new Error('[[error:no-topic]]')); function(next) {
Topics.getTopicData(tid, next);
},
function(_topicData, next) {
topicData = _topicData;
if (!topicData) {
return callback(new Error('[[error:no-topic]]'));
} }
async.parallel({ async.parallel({
@ -224,11 +232,9 @@ var async = require('async'),
tags: async.apply(Topics.getTopicTagsObjects, tid), tags: async.apply(Topics.getTopicTagsObjects, tid),
isFollowing: async.apply(Topics.isFollowing, [tid], uid), isFollowing: async.apply(Topics.isFollowing, [tid], uid),
bookmark: async.apply(Topics.getUserBookmark, tid, uid) bookmark: async.apply(Topics.getUserBookmark, tid, uid)
}, function(err, results) { }, next);
if (err) { },
return callback(err); function(results, next) {
}
topicData.posts = results.posts; topicData.posts = results.posts;
topicData.category = results.category; topicData.category = results.category;
topicData.thread_tools = results.threadTools.tools; topicData.thread_tools = results.threadTools.tools;
@ -241,11 +247,12 @@ var async = require('async'),
topicData.locked = parseInt(topicData.locked, 10) === 1; topicData.locked = parseInt(topicData.locked, 10) === 1;
topicData.pinned = parseInt(topicData.pinned, 10) === 1; topicData.pinned = parseInt(topicData.pinned, 10) === 1;
plugins.fireHook('filter:topic.get', {topic: topicData, uid: uid}, function(err, data) { plugins.fireHook('filter:topic.get', {topic: topicData, uid: uid}, next);
callback(err, data ? data.topic : null); },
}); function(data, next) {
}); next(null, data.topic);
}); }
], callback);
}; };
function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback) { function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback) {

@ -2,7 +2,8 @@
'use strict'; 'use strict';
var db = require('../database'), var async = require('async'),
db = require('../database'),
posts = require('../posts'); posts = require('../posts');
@ -19,24 +20,22 @@ module.exports = function(Topics) {
}; };
Topics.getUids = function(tid, callback) { Topics.getUids = function(tid, callback) {
Topics.getPids(tid, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); Topics.getPids(tid, next);
} },
function(pids, next) {
posts.getPostsFields(pids, ['uid'], function(err, postData) { posts.getPostsFields(pids, ['uid'], next);
if (err) { },
return callback(err); function(postData, next) {
}
var uids = postData.map(function(post) { var uids = postData.map(function(post) {
return post && post.uid; return post && post.uid;
}).filter(function(uid, index, array) { }).filter(function(uid, index, array) {
return array.indexOf(uid) === index; return uid && array.indexOf(uid) === index;
}); });
callback(null, uids); next(null, uids);
}); }
}); ], callback);
}; };
}; };
Loading…
Cancel
Save