You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/search.js

189 lines
4.5 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
posts = require('./posts'),
topics = require('./topics'),
categories = require('./categories'),
10 years ago
user = require('./user'),
11 years ago
plugins = require('./plugins'),
privileges = require('./privileges');
var search = {};
module.exports = search;
search.search = function(data, callback) {
10 years ago
function done(err, data) {
if (err) {
return callback(err);
}
result.search_query = query;
result[searchIn] = data;
result.matchCount = data.length;
result.hidePostedBy = searchIn !== 'posts';
10 years ago
result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2);
callback(null, result);
}
11 years ago
var start = process.hrtime();
var query = data.query;
var searchIn = data.searchIn || 'posts';
11 years ago
10 years ago
var result = {
posts: [],
users: [],
tags: []
};
if (searchIn === 'posts') {
searchInPosts(query, data, done);
10 years ago
} else if (searchIn === 'users') {
searchInUsers(query, done);
} else if (searchIn === 'tags') {
searchInTags(query, done);
} else {
callback(new Error('[[error:unknown-search-filter]]'));
}
};
function searchInPosts(query, data, callback) {
data.uid = data.uid || 0;
11 years ago
async.parallel({
pids: function(next) {
10 years ago
searchQuery('post', query, next);
11 years ago
},
tids: function(next) {
10 years ago
searchQuery('topic', query, next);
},
searchCategories: function(next) {
getSearchCategories(data, next);
11 years ago
}
}, function (err, results) {
if (err) {
return callback(err);
}
if (!results || (!results.pids.length && !results.tids.length)) {
10 years ago
return callback(null, []);
11 years ago
}
10 years ago
async.waterfall([
function(next) {
getMainPids(results.tids, next);
},
function(mainPids, next) {
results.pids.forEach(function(pid) {
if (mainPids.indexOf(pid) === -1) {
mainPids.push(pid);
11 years ago
}
});
privileges.posts.filter('read', mainPids, data.uid, next);
10 years ago
},
function(pids, next) {
posts.getPostSummaryByPids(pids, data.uid, {stripTags: true, parse: false}, next);
},
function(posts, next) {
posts = filterPosts(data, results.searchCategories, posts);
next(null, posts);
10 years ago
}
], callback);
11 years ago
});
10 years ago
}
11 years ago
function filterPosts(data, searchCategories, posts) {
var postedBy = data.postedBy;
var isAtLeast = data.repliesFilter === 'atleast';
data.replies = parseInt(data.replies, 10);
if (postedBy || searchCategories.length || data.replies) {
posts = posts.filter(function(post) {
return post &&
(postedBy ? post.user && (post.user.username === postedBy) : true) &&
(searchCategories.length ? (post.category && searchCategories.indexOf(post.category.cid) !== -1) : true) &&
(data.replies ? (isAtLeast ? post.topic.postcount >= data.replies : post.topic.postcount <= data.replies) : true);
});
}
return posts;
}
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);
});
}
10 years ago
function searchInUsers(query, callback) {
user.search({query: query}, function(err, results) {
callback(err, results ? results.users : null);
});
}
function searchInTags(query, callback) {
topics.searchAndLoadTags({query: query}, callback);
}
11 years ago
function getMainPids(tids, callback) {
topics.getTopicsFields(tids, ['mainPid'], function(err, topics) {
if (err) {
return callback(err);
}
topics = topics.map(function(topic) {
return topic && topic.mainPid;
}).filter(Boolean);
11 years ago
callback(null, topics);
});
}
10 years ago
function searchQuery(index, query, callback) {
11 years ago
plugins.fireHook('filter:search.query', {
index: index,
10 years ago
query: query
11 years ago
}, callback);
}