search style

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 9ec65db539
commit dcf3db104b

@ -12,9 +12,7 @@ var plugins = require('./plugins');
var privileges = require('./privileges'); var privileges = require('./privileges');
var utils = require('./utils'); var utils = require('./utils');
var search = {}; var search = module.exports;
module.exports = search;
search.search = function (data, callback) { search.search = function (data, callback) {
var start = process.hrtime(); var start = process.hrtime();
@ -42,99 +40,95 @@ search.search = function (data, callback) {
function searchInContent(data, callback) { function searchInContent(data, callback) {
data.uid = data.uid || 0; data.uid = data.uid || 0;
async.parallel({ var matchCount = 0;
searchCids: function (next) { var pids;
getSearchCids(data, next); async.waterfall([
}, function (next) {
searchUids: function (next) { async.parallel({
getSearchUids(data, next); searchCids: function (next) {
getSearchCids(data, next);
},
searchUids: function (next) {
getSearchUids(data, next);
},
}, next);
}, },
}, function (err, results) { function (results, next) {
if (err) { function doSearch(type, searchIn, next) {
return callback(err); if (searchIn.indexOf(data.searchIn) !== -1) {
} search.searchQuery(type, data.query, results.searchCids, results.searchUids, next);
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, []);
}
},
tids: function (next) {
if (data.searchIn === 'titles' || data.searchIn === 'titlesposts') {
search.searchQuery('topic', data.query, results.searchCids, results.searchUids, next);
} else { } else {
next(null, []); next(null, []);
} }
},
}, function (err, results) {
if (err) {
return callback(err);
} }
async.parallel({
var matchCount = 0; pids: function (next) {
doSearch('post', ['posts', 'titlesposts'], next);
},
tids: function (next) {
doSearch('topic', ['titles', 'titlesposts'], next);
},
}, next);
},
function (results, next) {
pids = results.pids;
if (!results || (!results.pids.length && !results.tids.length)) { if (!results || (!results.pids.length && !results.tids.length)) {
return callback(null, { posts: [], matchCount: matchCount, pageCount: 1 }); return callback(null, { posts: [], matchCount: matchCount, pageCount: 1 });
} }
async.waterfall([ topics.getMainPids(results.tids, next);
function (next) { },
topics.getMainPids(results.tids, next); function (mainPids, next) {
}, pids = mainPids.concat(pids).map(function (pid) {
function (mainPids, next) { return pid && pid.toString();
results.pids = mainPids.concat(results.pids).map(function (pid) { }).filter(function (pid, index, array) {
return pid && pid.toString(); return pid && array.indexOf(pid) === index;
}).filter(function (pid, index, array) { });
return pid && array.indexOf(pid) === index;
});
privileges.posts.filter('read', results.pids, data.uid, next);
},
function (pids, next) {
filterAndSort(pids, data, next);
},
function (pids, next) {
matchCount = pids.length;
if (data.page) {
var start = Math.max(0, (data.page - 1)) * 10;
pids = pids.slice(start, start + 10);
}
posts.getPostSummaryByPids(pids, data.uid, {}, next); privileges.posts.filter('read', pids, data.uid, next);
}, },
function (posts, next) { function (pids, next) {
next(null, { posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) }); filterAndSort(pids, data, next);
}, },
], callback); function (pids, next) {
}); matchCount = pids.length;
}); if (data.page) {
var start = Math.max(0, (data.page - 1)) * 10;
pids = pids.slice(start, start + 10);
}
posts.getPostSummaryByPids(pids, data.uid, {}, next);
},
function (posts, next) {
next(null, { posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) });
},
], callback);
} }
function filterAndSort(pids, data, callback) { function filterAndSort(pids, data, callback) {
getMatchedPosts(pids, data, function (err, posts) { async.waterfall([
if (err) { function (next) {
return callback(err); getMatchedPosts(pids, data, next);
} },
function (posts, next) {
if (!Array.isArray(posts) || !posts.length) { if (!Array.isArray(posts) || !posts.length) {
return callback(null, pids); return callback(null, pids);
} }
posts = posts.filter(Boolean); posts = posts.filter(Boolean);
posts = filterByPostcount(posts, data.replies, data.repliesFilter); posts = filterByPostcount(posts, data.replies, data.repliesFilter);
posts = filterByTimerange(posts, data.timeRange, data.timeFilter); posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
posts = filterByTags(posts, data.hasTags); posts = filterByTags(posts, data.hasTags);
sortPosts(posts, data); sortPosts(posts, data);
pids = posts.map(function (post) { pids = posts.map(function (post) {
return post && post.pid; return post && post.pid;
}); });
callback(null, pids); next(null, pids);
}); },
], callback);
} }
function getMatchedPosts(pids, data, callback) { function getMatchedPosts(pids, data, callback) {
@ -229,25 +223,22 @@ function getMatchedPosts(pids, data, callback) {
}, },
}, next); }, next);
}, },
], function (err, results) { function (results, next) {
if (err) { topicsData.forEach(function (topic, index) {
return next(err); if (topic && results.categories && results.categories[index]) {
} topic.category = results.categories[index];
}
topicsData.forEach(function (topic, index) { if (topic && results.teasers && results.teasers[index]) {
if (topic && results.categories && results.categories[index]) { topic.teaser = results.teasers[index];
topic.category = results.categories[index]; }
} if (topic && results.tags && results.tags[index]) {
if (topic && results.teasers && results.teasers[index]) { topic.tags = results.tags[index];
topic.teaser = results.teasers[index]; }
} });
if (topic && results.tags && results.tags[index]) {
topic.tags = results.tags[index];
}
});
next(null, topicsData); next(null, topicsData);
}); },
], next);
}, },
}, next); }, next);
}, },
@ -383,52 +374,53 @@ function getSearchCids(data, callback) {
return; return;
} }
async.parallel({ async.waterfall([
watchedCids: function (next) { function (next) {
if (data.categories.indexOf('watched') !== -1) { async.parallel({
user.getWatchedCategories(data.uid, next); watchedCids: function (next) {
} else { if (data.categories.indexOf('watched') !== -1) {
next(null, []); user.getWatchedCategories(data.uid, next);
} } else {
next(null, []);
}
},
childrenCids: function (next) {
if (data.searchChildren) {
getChildrenCids(data.categories, data.uid, next);
} else {
next(null, []);
}
},
}, next);
}, },
childrenCids: function (next) { function (results, next) {
if (data.searchChildren) { var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(function (cid, index, array) {
getChildrenCids(data.categories, data.uid, next); return cid && array.indexOf(cid) === index;
} else { });
next(null, []); next(null, cids);
}
}, },
}, function (err, results) { ], callback);
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) { function getChildrenCids(cids, uid, callback) {
categories.getChildren(cids, uid, function (err, childrenCategories) { async.waterfall([
if (err) { function (next) {
return callback(err); categories.getChildren(cids, uid, next);
} },
function (childrenCategories, next) {
var childrenCids = []; var childrenCids = [];
var allCategories = []; var allCategories = [];
childrenCategories.forEach(function (childrens) { childrenCategories.forEach(function (childrens) {
categories.flattenCategories(allCategories, childrens); categories.flattenCategories(allCategories, childrens);
childrenCids = childrenCids.concat(allCategories.map(function (category) { childrenCids = childrenCids.concat(allCategories.map(function (category) {
return category && category.cid; return category && category.cid;
})); }));
}); });
callback(null, childrenCids); next(null, childrenCids);
}); },
], callback);
} }
function getSearchUids(data, callback) { function getSearchUids(data, callback) {

Loading…
Cancel
Save