mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: new search hooks
filter:search.isAllowed, allow plugins to check if searching is allowed filter:search.searchIn, allow searching in custom items
This commit is contained in:
@@ -29,11 +29,15 @@ searchController.search = async function (req, res, next) {
|
|||||||
'search:tags': privileges.global.can('search:tags', req.uid),
|
'search:tags': privileges.global.can('search:tags', req.uid),
|
||||||
});
|
});
|
||||||
req.query.in = req.query.in || meta.config.searchDefaultIn || 'titlesposts';
|
req.query.in = req.query.in || meta.config.searchDefaultIn || 'titlesposts';
|
||||||
const allowed = (req.query.in === 'users' && userPrivileges['search:users']) ||
|
let allowed = (req.query.in === 'users' && userPrivileges['search:users']) ||
|
||||||
(req.query.in === 'tags' && userPrivileges['search:tags']) ||
|
(req.query.in === 'tags' && userPrivileges['search:tags']) ||
|
||||||
(req.query.in === 'categories') ||
|
(req.query.in === 'categories') ||
|
||||||
(['titles', 'titlesposts', 'posts'].includes(req.query.in) && userPrivileges['search:content']);
|
(['titles', 'titlesposts', 'posts'].includes(req.query.in) && userPrivileges['search:content']);
|
||||||
|
({ allowed } = await plugins.hooks.fire('filter:search.isAllowed', {
|
||||||
|
uid: req.uid,
|
||||||
|
query: req.query,
|
||||||
|
allowed,
|
||||||
|
}));
|
||||||
if (!allowed) {
|
if (!allowed) {
|
||||||
return helpers.notAllowed(req, res);
|
return helpers.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ search.search = async function (data) {
|
|||||||
result = await categories.search(data);
|
result = await categories.search(data);
|
||||||
} else if (data.searchIn === 'tags') {
|
} else if (data.searchIn === 'tags') {
|
||||||
result = await topics.searchAndLoadTags(data);
|
result = await topics.searchAndLoadTags(data);
|
||||||
|
} else if (data.searchIn) {
|
||||||
|
result = await plugins.hooks.fire('filter:search.searchIn', {
|
||||||
|
data,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new Error('[[error:unknown-search-filter]]');
|
throw new Error('[[error:unknown-search-filter]]');
|
||||||
}
|
}
|
||||||
@@ -108,6 +112,7 @@ async function searchInContent(data) {
|
|||||||
returnData.posts = await posts.getPostSummaryByPids(metadata.pids, data.uid, {});
|
returnData.posts = await posts.getPostSummaryByPids(metadata.pids, data.uid, {});
|
||||||
await plugins.hooks.fire('filter:search.contentGetResult', { result: returnData, data: data });
|
await plugins.hooks.fire('filter:search.contentGetResult', { result: returnData, data: data });
|
||||||
delete metadata.pids;
|
delete metadata.pids;
|
||||||
|
delete metadata.data;
|
||||||
return Object.assign(returnData, metadata);
|
return Object.assign(returnData, metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user