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

131 lines
2.8 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
posts = require('./posts'),
topics = require('./topics'),
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';
var uid = data.uid || 0;
11 years ago
10 years ago
var result = {
posts: [],
users: [],
tags: []
};
if (searchIn === 'posts') {
searchInPosts(query, data.postedBy, uid, 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, postedBy, uid, callback) {
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);
},
postedByUid: function(next) {
if (postedBy) {
user.getUidByUsername(postedBy, next);
} else {
next(null, 0);
}
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
}
});
10 years ago
privileges.posts.filter('read', mainPids, uid, next);
},
function(pids, next) {
posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, next);
},
function(posts, next) {
if (postedBy) {
posts = posts.filter(function(post) {
return post && parseInt(post.uid, 10) === parseInt(results.postedByUid, 10);
});
}
next(null, posts);
10 years ago
}
], callback);
11 years ago
});
10 years ago
}
11 years ago
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);
}