mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 22:45:46 +01:00
waterfall for topics
This commit is contained in:
129
src/topics.js
129
src/topics.js
@@ -97,7 +97,7 @@ var async = require('async'),
|
|||||||
user.getSettings(uid, next);
|
user.getSettings(uid, next);
|
||||||
}
|
}
|
||||||
}, function(err, results) {
|
}, function(err, results) {
|
||||||
if(err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
callback(null, Math.ceil((results.index + 1) / results.settings.topicsPerPage));
|
callback(null, Math.ceil((results.index + 1) / results.settings.topicsPerPage));
|
||||||
@@ -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,43 +144,44 @@ var async = require('async'),
|
|||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.getTopicsData(tids, function(err, topics) {
|
var uids, cids, 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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (err) {
|
async.waterfall([
|
||||||
return callback(err);
|
function(next) {
|
||||||
}
|
Topics.getTopicsData(tids, next);
|
||||||
|
},
|
||||||
var uids = mapFilter(topics, 'uid');
|
function(_topics, next) {
|
||||||
var cids = mapFilter(topics, 'cid');
|
function mapFilter(array, field) {
|
||||||
|
return array.map(function(topic) {
|
||||||
async.parallel({
|
return topic && topic[field] && topic[field].toString();
|
||||||
teasers: function(next) {
|
}).filter(function(value, index, array) {
|
||||||
Topics.getTeasers(topics, next);
|
return utils.isNumber(value) && array.indexOf(value) === index;
|
||||||
},
|
});
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 users = _.object(uids, results.users);
|
||||||
var categories = _.object(cids, results.categories);
|
var categories = _.object(cids, results.categories);
|
||||||
|
|
||||||
@@ -204,31 +205,36 @@ 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);
|
||||||
|
},
|
||||||
async.parallel({
|
function(_topicData, next) {
|
||||||
posts: async.apply(getMainPostAndReplies, topicData, set, uid, start, stop, reverse),
|
topicData = _topicData;
|
||||||
category: async.apply(Topics.getCategoryData, tid),
|
if (!topicData) {
|
||||||
threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
|
return callback(new Error('[[error:no-topic]]'));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.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);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user