Skip to content

Node.js based forum software built for the modern web

Merge branch '0.7.0'

Julian Lam957415463fd8
diff --cc src/search.js
index 86f39f6b52,e0be3ecafd..b8f8ffa849
--- a/src/search.js
+++ b/src/search.js
@@@ -40,9 -38,9 +40,9 @@@ search.search = function(data, callback
};
if (searchIn === 'posts') {
- searchInPosts(query, data.postedBy, uid, done);
+ searchInPosts(query, data, done);
} else if (searchIn === 'users') {
- searchInUsers(query, done);
- searchInUsers(query, uid, done);
++ searchInUsers(query, data.uid, done);
} else if (searchIn === 'tags') {
searchInTags(query, done);
} else {
@@@ -103,200 -99,9 +103,200 @@@ function searchInPosts(query, data, cal
});
}
+function filterAndSort(pids, data, searchCategories, callback) {
+ var postFields = ['pid', 'tid', 'timestamp'];
+ var topicFields = [];
+
+ if (data.postedBy) {
+ postFields.push('uid');
+ }
+
+ if (searchCategories.length) {
+ topicFields.push('cid');
+ }
+
+ if (data.replies) {
+ topicFields.push('postcount');
+ }
+
+ async.parallel({
+ posts: function(next) {
+ getMatchedPosts(pids, postFields, topicFields, next);
+ },
+ postedByUid: function(next) {
+ if (data.postedBy) {
+ user.getUidByUsername(data.postedBy, next);
+ } else {
+ next();
+ }
+ }
+ }, function(err, results) {
+ if (err) {
+ return callback(err);
+ }
+ if (!results.posts) {
+ return callback(null, pids);
+ }
+ var posts = results.posts.filter(Boolean);
+
+ posts = filterByUser(posts, results.postedByUid);
+ posts = filterByCategories(posts, searchCategories);
+ posts = filterByPostcount(posts, data.replies, data.repliesFilter);
+ posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
+
+ sortPosts(posts, data);
+
+ pids = posts.map(function(post) {
+ return post && post.pid;
+ });
+
+ callback(null, pids);
+ });
+}
+
+function getMatchedPosts(pids, postFields, topicFields, callback) {
+ var keys = pids.map(function(pid) {
+ return 'post:' + pid;
+ });
+ var posts;
+ async.waterfall([
+ function(next) {
+ db.getObjectsFields(keys, postFields, next);
+ },
+ function(_posts, next) {
+ posts = _posts;
+ if (!topicFields.length) {
+ return callback(null, posts);
+ }
+ var topicKeys = posts.map(function(post) {
+ return 'topic:' + post.tid;
+ });
+ db.getObjectsFields(topicKeys, topicFields, next);
+ },
+ function(topics, next) {
+ posts.forEach(function(post, index) {
+ post.topic = topics[index];
+ });
+
+ next(null, posts);
+ }
+ ], callback);
+}
+
+function filterByUser(posts, postedByUid) {
+ if (postedByUid) {
+ postedByUid = parseInt(postedByUid, 10);
+ posts = posts.filter(function(post) {
+ return parseInt(post.uid, 10) === postedByUid;
+ });
+ }
+ return posts;
+}
+
+function filterByCategories(posts, searchCategories) {
+ if (searchCategories.length) {
+ posts = posts.filter(function(post) {
+ return post.topic && searchCategories.indexOf(post.topic.cid) !== -1;
+ });
+ }
+ return posts;
+}
+
+function filterByPostcount(posts, postCount, repliesFilter) {
+ postCount = parseInt(postCount, 10);
+ if (postCount) {
+ if (repliesFilter === 'atleast') {
+ posts = posts.filter(function(post) {
+ return post.topic && post.topic.postcount >= postCount;
+ });
+ } else {
+ posts = posts.filter(function(post) {
+ return post.topic && post.topic.postcount <= postCount;
+ });
+ }
+ }
+ return posts;
+}
+
+function filterByTimerange(posts, timeRange, timeFilter) {
+ timeRange = parseInt(timeRange) * 1000;
+ if (timeRange) {
+ var time = Date.now() - timeRange;
+ if (timeFilter === 'newer') {
+ posts = posts.filter(function(post) {
+ return post.timestamp >= time;
+ });
+ } else {
+ posts = posts.filter(function(post) {
+ return post.timestamp <= time;
+ });
+ }
+ }
+ return posts;
+}
+
+function sortPosts(posts, data) {
+ posts.sort(function(p1, p2) {
+ return p2.timestamp - p1.timestamp;
+ });
+}
+
+function getSearchCategories(data, callback) {
+ if (!Array.isArray(data.categories) || !data.categories.length || data.categories.indexOf('all') !== -1) {
+ return callback(null, []);
+ }
+
+ async.parallel({
+ watchedCids: function(next) {
+ if (data.categories.indexOf('watched') !== -1) {
+ user.getWatchedCategories(data.uid, next);
+ } else {
+ next(null, []);
+ }
+ },
+ childrenCids: function(next) {
+ if (data.searchChildren) {
+ getChildrenCids(data.categories, data.uid, next);
+ } else {
+ next(null, []);
+ }
+ }
+ }, function(err, results) {
+ if (err) {
+ return callback(err);
+ }
+
+ var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(function(cid, index, array) {
+ return cid && array.indexOf(cid) === index;
+ });
+
+ callback(null, cids);
+ });
+}
+
+function getChildrenCids(cids, uid, callback) {
+ categories.getChildren(cids, uid, function(err, childrenCategories) {
+ if (err) {
+ return callback(err);
+ }
+
+ var childrenCids = [];
+ childrenCategories.forEach(function(childrens) {
+ childrenCids = childrenCids.concat(childrens.map(function(category) {
+ return category && category.cid;
+ }));
+ });
+
+ callback(null, childrenCids);
+ });
+}
+
- function searchInUsers(query, callback) {
- user.search({query: query}, function(err, results) {
+ function searchInUsers(query, uid, callback) {
+ user.search({query: query, uid: uid}, function(err, results) {
- callback(err, results ? results.users : null);
+ if (err) {
+ return callback(err);
+ }
+ callback(null, {matches: results.users, matchCount: results.matchCount});
});
}