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

Loading…
Cancel
Save