mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 17:05:51 +01:00
search style
This commit is contained in:
106
src/search.js
106
src/search.js
@@ -12,9 +12,7 @@ var plugins = require('./plugins');
|
||||
var privileges = require('./privileges');
|
||||
var utils = require('./utils');
|
||||
|
||||
var search = {};
|
||||
|
||||
module.exports = search;
|
||||
var search = module.exports;
|
||||
|
||||
search.search = function (data, callback) {
|
||||
var start = process.hrtime();
|
||||
@@ -42,6 +40,10 @@ search.search = function (data, callback) {
|
||||
|
||||
function searchInContent(data, callback) {
|
||||
data.uid = data.uid || 0;
|
||||
var matchCount = 0;
|
||||
var pids;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
searchCids: function (next) {
|
||||
getSearchCids(data, next);
|
||||
@@ -49,48 +51,41 @@ function searchInContent(data, callback) {
|
||||
searchUids: function (next) {
|
||||
getSearchUids(data, next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
function doSearch(type, searchIn, next) {
|
||||
if (searchIn.indexOf(data.searchIn) !== -1) {
|
||||
search.searchQuery(type, data.query, results.searchCids, results.searchUids, next);
|
||||
} else {
|
||||
next(null, []);
|
||||
}
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
pids: function (next) {
|
||||
if (data.searchIn === 'posts' || data.searchIn === 'titlesposts') {
|
||||
search.searchQuery('post', data.query, results.searchCids, results.searchUids, next);
|
||||
} else {
|
||||
next(null, []);
|
||||
}
|
||||
doSearch('post', ['posts', 'titlesposts'], next);
|
||||
},
|
||||
tids: function (next) {
|
||||
if (data.searchIn === 'titles' || data.searchIn === 'titlesposts') {
|
||||
search.searchQuery('topic', data.query, results.searchCids, results.searchUids, next);
|
||||
} else {
|
||||
next(null, []);
|
||||
}
|
||||
doSearch('topic', ['titles', 'titlesposts'], next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var matchCount = 0;
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
pids = results.pids;
|
||||
if (!results || (!results.pids.length && !results.tids.length)) {
|
||||
return callback(null, { posts: [], matchCount: matchCount, pageCount: 1 });
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getMainPids(results.tids, next);
|
||||
},
|
||||
function (mainPids, next) {
|
||||
results.pids = mainPids.concat(results.pids).map(function (pid) {
|
||||
pids = mainPids.concat(pids).map(function (pid) {
|
||||
return pid && pid.toString();
|
||||
}).filter(function (pid, index, array) {
|
||||
return pid && array.indexOf(pid) === index;
|
||||
});
|
||||
|
||||
privileges.posts.filter('read', results.pids, data.uid, next);
|
||||
privileges.posts.filter('read', pids, data.uid, next);
|
||||
},
|
||||
function (pids, next) {
|
||||
filterAndSort(pids, data, next);
|
||||
@@ -108,16 +103,14 @@ function searchInContent(data, callback) {
|
||||
next(null, { posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) });
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function filterAndSort(pids, data, callback) {
|
||||
getMatchedPosts(pids, data, function (err, posts) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
getMatchedPosts(pids, data, next);
|
||||
},
|
||||
function (posts, next) {
|
||||
if (!Array.isArray(posts) || !posts.length) {
|
||||
return callback(null, pids);
|
||||
}
|
||||
@@ -133,8 +126,9 @@ function filterAndSort(pids, data, callback) {
|
||||
return post && post.pid;
|
||||
});
|
||||
|
||||
callback(null, pids);
|
||||
});
|
||||
next(null, pids);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function getMatchedPosts(pids, data, callback) {
|
||||
@@ -229,11 +223,7 @@ function getMatchedPosts(pids, data, callback) {
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
], function (err, results) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
function (results, next) {
|
||||
topicsData.forEach(function (topic, index) {
|
||||
if (topic && results.categories && results.categories[index]) {
|
||||
topic.category = results.categories[index];
|
||||
@@ -247,7 +237,8 @@ function getMatchedPosts(pids, data, callback) {
|
||||
});
|
||||
|
||||
next(null, topicsData);
|
||||
});
|
||||
},
|
||||
], next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
@@ -383,6 +374,8 @@ function getSearchCids(data, callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
watchedCids: function (next) {
|
||||
if (data.categories.indexOf('watched') !== -1) {
|
||||
@@ -398,25 +391,23 @@ function getSearchCids(data, callback) {
|
||||
next(null, []);
|
||||
}
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(function (cid, index, array) {
|
||||
return cid && array.indexOf(cid) === index;
|
||||
});
|
||||
|
||||
callback(null, cids);
|
||||
});
|
||||
next(null, cids);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function getChildrenCids(cids, uid, callback) {
|
||||
categories.getChildren(cids, uid, function (err, childrenCategories) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
categories.getChildren(cids, uid, next);
|
||||
},
|
||||
function (childrenCategories, next) {
|
||||
var childrenCids = [];
|
||||
var allCategories = [];
|
||||
|
||||
@@ -427,8 +418,9 @@ function getChildrenCids(cids, uid, callback) {
|
||||
}));
|
||||
});
|
||||
|
||||
callback(null, childrenCids);
|
||||
});
|
||||
next(null, childrenCids);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function getSearchUids(data, callback) {
|
||||
|
||||
Reference in New Issue
Block a user