From e0862be3c8d834002d5e293e7f3b3c9fa8f40183 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 28 Jul 2017 14:10:04 -0400 Subject: [PATCH] added new search hook to allow for enumeration after query --- src/search.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/search.js b/src/search.js index 58b93c8a5a..d54f1e0ae2 100644 --- a/src/search.js +++ b/src/search.js @@ -41,6 +41,7 @@ function searchInContent(data, callback) { data.uid = data.uid || 0; var matchCount = 0; var pids; + var metadata; async.waterfall([ function (next) { async.parallel({ @@ -88,16 +89,23 @@ function searchInContent(data, callback) { filterAndSort(pids, data, next); }, function (pids, next) { - matchCount = pids.length; + plugin.fireHook('filter:search.inContent', { + pids: pids, + }, next); + }, + function (metadata, next) { + matchCount = metadata.pids.length; if (data.page) { var start = Math.max(0, (data.page - 1)) * 10; - pids = pids.slice(start, start + 10); + metadata.pids = metadata.pids.slice(start, start + 10); } - posts.getPostSummaryByPids(pids, data.uid, {}, next); + posts.getPostSummaryByPids(metadata.pids, data.uid, {}, next); }, function (posts, next) { - next(null, { posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) }); + // Append metadata to returned payload (without pids) + delete metadata.pids; + next(null, Object.assign({ posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) }, metadata)); }, ], callback); }