feat: #7743 , search.js

v1.18.x
Baris Usakli 6 years ago
parent 879104ccde
commit 6d3a92b851

@ -1,107 +1,89 @@
'use strict'; 'use strict';
var async = require('async'); const _ = require('lodash');
var _ = require('lodash');
const db = require('./database');
var db = require('./database'); const posts = require('./posts');
var posts = require('./posts'); const topics = require('./topics');
var topics = require('./topics'); const categories = require('./categories');
var categories = require('./categories'); const user = require('./user');
var user = require('./user'); const plugins = require('./plugins');
var plugins = require('./plugins'); const privileges = require('./privileges');
var privileges = require('./privileges'); const utils = require('./utils');
var utils = require('./utils');
const search = module.exports;
var search = module.exports;
search.search = async function (data) {
search.search = function (data, callback) { const start = process.hrtime();
var start = process.hrtime();
data.searchIn = data.searchIn || 'titlesposts'; data.searchIn = data.searchIn || 'titlesposts';
data.sortBy = data.sortBy || 'relevance'; data.sortBy = data.sortBy || 'relevance';
async.waterfall([
function (next) { let result;
if (data.searchIn === 'posts' || data.searchIn === 'titles' || data.searchIn === 'titlesposts') { if (data.searchIn === 'posts' || data.searchIn === 'titles' || data.searchIn === 'titlesposts') {
searchInContent(data, next); result = await searchInContent(data);
} else if (data.searchIn === 'users') { } else if (data.searchIn === 'users') {
user.search(data, next); result = await user.search(data);
} else if (data.searchIn === 'tags') { } else if (data.searchIn === 'tags') {
topics.searchAndLoadTags(data, next); result = await topics.searchAndLoadTags(data);
} else { } else {
next(new Error('[[error:unknown-search-filter]]')); throw new Error('[[error:unknown-search-filter]]');
} }
},
function (result, next) {
result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2); result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2);
next(null, result); return result;
},
], callback);
}; };
function searchInContent(data, callback) { async function searchInContent(data) {
data.uid = data.uid || 0; data.uid = data.uid || 0;
var pids;
var metadata; const itemsPerPage = Math.min(data.itemsPerPage || 10, 100);
var itemsPerPage = Math.min(data.itemsPerPage || 10, 100);
const returnData = { const returnData = {
posts: [], posts: [],
matchCount: 0, matchCount: 0,
pageCount: 1, pageCount: 1,
}; };
async.waterfall([
function (next) { const [searchCids, searchUids] = await Promise.all([
async.parallel({ getSearchCids(data),
searchCids: async.apply(getSearchCids, data), getSearchUids(data),
searchUids: async.apply(getSearchUids, data), ]);
}, next);
}, async function doSearch(type, searchIn) {
function (results, next) {
function doSearch(type, searchIn, next) {
if (searchIn.includes(data.searchIn)) { if (searchIn.includes(data.searchIn)) {
plugins.fireHook('filter:search.query', { return await plugins.fireHook('filter:search.query', {
index: type, index: type,
content: data.query, content: data.query,
matchWords: data.matchWords || 'all', matchWords: data.matchWords || 'all',
cid: results.searchCids, cid: searchCids,
uid: results.searchUids, uid: searchUids,
searchData: data, searchData: data,
}, next); });
} else {
next(null, []);
} }
return [];
} }
async.parallel({ const [pids, tids] = await Promise.all([
pids: async.apply(doSearch, 'post', ['posts', 'titlesposts']), doSearch('post', ['posts', 'titlesposts']),
tids: async.apply(doSearch, 'topic', ['titles', 'titlesposts']), doSearch('topic', ['titles', 'titlesposts']),
}, next); ]);
},
function (results, next) {
pids = results.pids;
if (data.returnIds) { if (data.returnIds) {
return callback(null, results); return { pids: pids, tids: tids };
} }
if (!pids.length && !tids.length) {
if (!results.pids.length && !results.tids.length) { return returnData;
return callback(null, returnData); }
}
const mainPids = await topics.getMainPids(tids);
topics.getMainPids(results.tids, next);
}, let allPids = mainPids.concat(pids).filter(Boolean);
function (mainPids, next) {
pids = mainPids.concat(pids).filter(Boolean); allPids = await privileges.posts.filter('topics:read', allPids, data.uid);
allPids = await filterAndSort(allPids, data);
privileges.posts.filter('topics:read', pids, data.uid, next);
}, const metadata = await plugins.fireHook('filter:search.inContent', {
function (pids, next) { pids: allPids,
filterAndSort(pids, data, next); });
},
function (pids, next) {
plugins.fireHook('filter:search.inContent', {
pids: pids,
}, next);
},
function (_metadata, next) {
metadata = _metadata;
returnData.matchCount = metadata.pids.length; returnData.matchCount = metadata.pids.length;
returnData.pageCount = Math.max(1, Math.ceil(parseInt(returnData.matchCount, 10) / itemsPerPage)); returnData.pageCount = Math.max(1, Math.ceil(parseInt(returnData.matchCount, 10) / itemsPerPage));
@ -110,139 +92,109 @@ function searchInContent(data, callback) {
metadata.pids = metadata.pids.slice(start, start + itemsPerPage); metadata.pids = metadata.pids.slice(start, start + itemsPerPage);
} }
posts.getPostSummaryByPids(metadata.pids, data.uid, {}, next); returnData.posts = await posts.getPostSummaryByPids(metadata.pids, data.uid, {});
},
function (posts, next) {
returnData.posts = posts;
// Append metadata to returned payload (without pids)
delete metadata.pids; delete metadata.pids;
next(null, Object.assign(returnData, metadata)); return Object.assign(returnData, metadata);
},
], callback);
} }
function filterAndSort(pids, data, callback) { 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) {
return setImmediate(callback, null, pids); return pids;
} }
let postsData = await getMatchedPosts(pids, data);
async.waterfall([ if (!postsData.length) {
function (next) { return pids;
getMatchedPosts(pids, data, next);
},
function (posts, next) {
if (!posts.length) {
return callback(null, pids);
} }
posts = posts.filter(Boolean); postsData = postsData.filter(Boolean);
postsData = filterByPostcount(postsData, data.replies, data.repliesFilter);
postsData = filterByTimerange(postsData, data.timeRange, data.timeFilter);
postsData = filterByTags(postsData, data.hasTags);
posts = filterByPostcount(posts, data.replies, data.repliesFilter); sortPosts(postsData, data);
posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
posts = filterByTags(posts, data.hasTags);
sortPosts(posts, data); const result = await plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: postsData, data: data });
plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: posts, data: data }, next); return result.posts.map(post => post && post.pid);
},
function (result, next) {
pids = result.posts.map(post => post && post.pid);
next(null, pids);
},
], callback);
} }
function getMatchedPosts(pids, data, callback) { async function getMatchedPosts(pids, data) {
var postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted', 'upvotes', 'downvotes']; const postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted', 'upvotes', 'downvotes'];
var categoryFields = [];
if (data.sortBy.startsWith('category.')) { let postsData = await posts.getPostsFields(pids, postFields);
categoryFields.push(data.sortBy.split('.')[1]); postsData = postsData.filter(post => post && !post.deleted);
const uids = _.uniq(postsData.map(post => post.uid));
const tids = _.uniq(postsData.map(post => post.tid));
const [users, topics] = await Promise.all([
getUsers(uids, data),
getTopics(tids, data),
]);
const tidToTopic = _.zipObject(tids, topics);
const uidToUser = _.zipObject(uids, users);
postsData.forEach(function (post) {
if (topics && tidToTopic[post.tid]) {
post.topic = tidToTopic[post.tid];
if (post.topic && post.topic.category) {
post.category = post.topic.category;
}
} }
var postsData; if (uidToUser[post.uid]) {
let tids; post.user = uidToUser[post.uid];
let uids; }
async.waterfall([ });
function (next) {
posts.getPostsFields(pids, postFields, next);
},
function (_postsData, next) {
postsData = _postsData.filter(post => post && !post.deleted);
async.parallel({ return postsData.filter(post => post && post.topic && !post.topic.deleted);
users: function (next) {
if (data.sortBy.startsWith('user')) {
uids = _.uniq(postsData.map(post => post.uid));
user.getUsersFields(uids, ['username'], next);
} else {
next();
}
},
topics: function (next) {
var topicsData;
tids = _.uniq(postsData.map(post => post.tid));
let cids;
async.waterfall([
function (next) {
topics.getTopicsData(tids, next);
},
function (_topics, next) {
topicsData = _topics;
async.parallel({
categories: function (next) {
if (!categoryFields.length) {
return next();
} }
cids = _.uniq(topicsData.map(topic => topic && topic.cid)); async function getUsers(uids, data) {
db.getObjectsFields(cids.map(cid => 'category:' + cid), categoryFields, next); if (data.sortBy.startsWith('user')) {
}, return user.getUsersFields(uids, ['username']);
tags: function (next) {
if (Array.isArray(data.hasTags) && data.hasTags.length) {
topics.getTopicsTags(tids, next);
} else {
setImmediate(next);
} }
}, return [];
}, next); }
},
function (results, next) { async function getTopics(tids, data) {
const cidToCategory = _.zipObject(cids, results.categories); const topicsData = await topics.getTopicsData(tids);
const cids = _.uniq(topicsData.map(topic => topic && topic.cid));
const [categories, tags] = await Promise.all([
getCategories(topicsData, data),
getTags(tids, data),
]);
const cidToCategory = _.zipObject(cids, categories);
topicsData.forEach(function (topic, index) { topicsData.forEach(function (topic, index) {
if (topic && results.categories && cidToCategory[topic.cid]) { if (topic && categories && cidToCategory[topic.cid]) {
topic.category = cidToCategory[topic.cid]; topic.category = cidToCategory[topic.cid];
} }
if (topic && results.tags && results.tags[index]) { if (topic && tags && tags[index]) {
topic.tags = results.tags[index]; topic.tags = tags[index];
} }
}); });
next(null, topicsData); return topicsData;
},
], next);
},
}, next);
},
function (results, next) {
const tidToTopic = _.zipObject(tids, results.topics);
const uidToUser = _.zipObject(uids, results.users);
postsData.forEach(function (post) {
if (results.topics && tidToTopic[post.tid]) {
post.topic = tidToTopic[post.tid];
if (post.topic && post.topic.category) {
post.category = post.topic.category;
} }
async function getCategories(cids, data) {
const categoryFields = [];
if (data.sortBy.startsWith('category.')) {
categoryFields.push(data.sortBy.split('.')[1]);
}
if (!categoryFields.length) {
return null;
} }
if (uidToUser[post.uid]) { return await db.getObjectsFields(cids.map(cid => 'category:' + cid), categoryFields);
post.user = uidToUser[post.uid];
} }
});
postsData = postsData.filter(post => post && post.topic && !post.topic.deleted); async function getTags(tids, data) {
next(null, postsData); if (Array.isArray(data.hasTags) && data.hasTags.length) {
}, return await topics.getTopicsTags(tids);
], callback); }
return null;
} }
function filterByPostcount(posts, postCount, repliesFilter) { function filterByPostcount(posts, postCount, repliesFilter) {
@ -316,58 +268,42 @@ function sortPosts(posts, data) {
} }
} }
function getSearchCids(data, callback) { async function getSearchCids(data) {
if (!Array.isArray(data.categories) || !data.categories.length) { if (!Array.isArray(data.categories) || !data.categories.length) {
return callback(null, []); return [];
} }
if (data.categories.includes('all')) { if (data.categories.includes('all')) {
return categories.getCidsByPrivilege('categories:cid', data.uid, 'read', callback); return await categories.getCidsByPrivilege('categories:cid', data.uid, 'read');
} }
async.waterfall([ const [watchedCids, childrenCids] = await Promise.all([
function (next) { getWatchedCids(data),
async.parallel({ getChildrenCids(data),
watchedCids: function (next) { ]);
if (data.categories.includes('watched')) { return _.uniq(watchedCids.concat(childrenCids).concat(data.categories).filter(Boolean));
user.getCategoriesByStates(data.uid, [categories.watchStates.watching], next);
} else {
setImmediate(next, null, []);
} }
},
childrenCids: function (next) { async function getWatchedCids(data) {
if (data.searchChildren) { if (!data.categories.includes('watched')) {
getChildrenCids(data.categories, data.uid, next); return [];
} else { }
setImmediate(next, null, []); return await user.getCategoriesByStates(data.uid, [categories.watchStates.watching]);
} }
},
}, next); async function getChildrenCids(data) {
}, if (!data.searchChildren) {
function (results, next) { return [];
const cids = _.uniq(results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(Boolean)); }
next(null, cids); const childrenCids = await Promise.all(data.categories.map(cid => categories.getChildrenCids(cid)));
}, return await privileges.categories.filterCids('find', _.uniq(_.flatten(childrenCids)), data.uid);
], callback); }
}
async function getSearchUids(data) {
function getChildrenCids(cids, uid, callback) { if (!data.postedBy) {
async.waterfall([ return [];
function (next) {
async.map(cids, categories.getChildrenCids, next);
},
function (childrenCids, next) {
privileges.categories.filterCids('find', _.uniq(_.flatten(childrenCids)), uid, next);
},
], callback);
}
function getSearchUids(data, callback) {
if (data.postedBy) {
user.getUidsByUsernames(Array.isArray(data.postedBy) ? data.postedBy : [data.postedBy], callback);
} else {
setImmediate(callback, null, []);
} }
return await user.getUidsByUsernames(Array.isArray(data.postedBy) ? data.postedBy : [data.postedBy]);
} }
search.async = require('./promisify')(search); search.async = require('./promisify')(search);

Loading…
Cancel
Save