From abf27954d37855b25be0e3b958f9ff7c36855531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 13 Mar 2015 16:33:00 -0400 Subject: [PATCH] use topic.getMainPids --- src/search.js | 28 +++++----------------------- src/topics.js | 6 +++++- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/search.js b/src/search.js index 8ccdf77d78..e44dedc216 100644 --- a/src/search.js +++ b/src/search.js @@ -85,16 +85,14 @@ function searchInContent(query, data, callback) { async.waterfall([ function(next) { - getMainPids(results.tids, next); + topics.getMainPids(results.tids, next); }, function(mainPids, next) { - results.pids.forEach(function(pid) { - if (mainPids.indexOf(pid.toString()) === -1) { - mainPids.push(pid); - } + results.pids = mainPids.concat(results.pids).filter(function(pid, index, array) { + return pid && array.indexOf(pid) === index; }); - - privileges.posts.filter('read', mainPids, data.uid, next); + + privileges.posts.filter('read', results.pids, data.uid, next); }, function(pids, next) { filterAndSort(pids, data, results.searchCategories, next); @@ -460,22 +458,6 @@ function searchInTags(query, callback) { }); } -function getMainPids(tids, callback) { - if (!Array.isArray(tids) || !tids.length) { - return callback(null, []); - } - - topics.getTopicsFields(tids, ['mainPid'], function(err, topics) { - if (err) { - return callback(err); - } - topics = topics.map(function(topic) { - return topic && topic.mainPid && topic.mainPid.toString(); - }).filter(Boolean); - callback(null, topics); - }); -} - search.searchQuery = function(index, query, callback) { plugins.fireHook('filter:search.query', { index: index, diff --git a/src/topics.js b/src/topics.js index aecdcb1000..0cc153e85e 100644 --- a/src/topics.js +++ b/src/topics.js @@ -287,13 +287,17 @@ var async = require('async'), }; Topics.getMainPids = function(tids, callback) { + if (!Array.isArray(tids) || !tids.length) { + return callback(null, []); + } + Topics.getTopicsFields(tids, ['mainPid'], function(err, topicData) { if (err) { return callback(err); } var mainPids = topicData.map(function(topic) { - return topic ? topic.mainPid : null; + return topic && topic.mainPid; }); callback(null, mainPids); });