mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 08:55:47 +01:00
Added new hook "filter:search.inContent"
This hook is fired after the search query returns a collection of pids, but before those pids' summaries are retrieved. It is useful to add a hook in here if you want to enumerate over the matched results before they are truncated in the response payload.
This commit is contained in:
@@ -41,6 +41,8 @@ function searchInContent(data, callback) {
|
|||||||
data.uid = data.uid || 0;
|
data.uid = data.uid || 0;
|
||||||
var matchCount = 0;
|
var matchCount = 0;
|
||||||
var pids;
|
var pids;
|
||||||
|
var metadata;
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
@@ -88,16 +90,24 @@ function searchInContent(data, callback) {
|
|||||||
filterAndSort(pids, data, next);
|
filterAndSort(pids, data, next);
|
||||||
},
|
},
|
||||||
function (pids, next) {
|
function (pids, next) {
|
||||||
matchCount = pids.length;
|
plugins.fireHook('filter:search.inContent', {
|
||||||
|
pids: pids,
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (_metadata, next) {
|
||||||
|
metadata = _metadata;
|
||||||
|
matchCount = metadata.pids.length;
|
||||||
if (data.page) {
|
if (data.page) {
|
||||||
var start = Math.max(0, (data.page - 1)) * 10;
|
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) {
|
function (posts, next) {
|
||||||
next(null, { posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)) });
|
// Append metadata from plugin hooks 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);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user