From aeb44faf5c7c7002bba85d5ce33fe8112baeca0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 20 Aug 2019 20:06:03 -0400 Subject: [PATCH] feat: add filter:search.getPosts --- src/search.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/search.js b/src/search.js index 5094f4951d..a83ff3b626 100644 --- a/src/search.js +++ b/src/search.js @@ -93,12 +93,13 @@ async function searchInContent(data) { } returnData.posts = await posts.getPostSummaryByPids(metadata.pids, data.uid, {}); + await plugins.fireHook('filter:search.getPosts', { posts: returnData.posts }); delete metadata.pids; return Object.assign(returnData, metadata); } async function filterAndSort(pids, data) { - if (data.sortBy === 'relevance' && !data.replies && !data.timeRange && !data.hasTags) { + if (data.sortBy === 'relevance' && !data.replies && !data.timeRange && !data.hasTags && !plugins.hasListeners('filter:search.filterAndSort')) { return pids; } let postsData = await getMatchedPosts(pids, data); @@ -114,7 +115,6 @@ async function filterAndSort(pids, data) { sortPosts(postsData, data); const result = await plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: postsData, data: data }); - return result.posts.map(post => post && post.pid); } @@ -241,18 +241,18 @@ function sortPosts(posts, data) { } data.sortDirection = data.sortDirection || 'desc'; - var direction = data.sortDirection === 'desc' ? 1 : -1; + const direction = data.sortDirection === 'desc' ? 1 : -1; const fields = data.sortBy.split('.'); if (fields.length === 1) { return posts.sort((p1, p2) => direction * (p2[fields[0]] - p1[fields[0]])); } - var firstPost = posts[0]; + const firstPost = posts[0]; if (!fields || fields.length !== 2 || !firstPost[fields[0]] || !firstPost[fields[0]][fields[1]]) { return; } - var isNumeric = utils.isNumber(firstPost[fields[0]][fields[1]]); + const isNumeric = utils.isNumber(firstPost[fields[0]][fields[1]]); if (isNumeric) { posts.sort((p1, p2) => direction * (p2[fields[0]][fields[1]] - p1[fields[0]][fields[1]]));