feat: convert search controller to async/await
parent
6f375482a0
commit
acf12e3dd8
@ -1,106 +1,93 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
|
||||||
|
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var search = require('../search');
|
const search = require('../search');
|
||||||
var categories = require('../categories');
|
const categories = require('../categories');
|
||||||
var pagination = require('../pagination');
|
const pagination = require('../pagination');
|
||||||
var privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
var helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
|
|
||||||
var searchController = module.exports;
|
const searchController = module.exports;
|
||||||
|
|
||||||
searchController.search = function (req, res, next) {
|
searchController.search = async function (req, res, next) {
|
||||||
if (!plugins.hasListeners('filter:search.query')) {
|
if (!plugins.hasListeners('filter:search.query')) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
var page = Math.max(1, parseInt(req.query.page, 10)) || 1;
|
const page = Math.max(1, parseInt(req.query.page, 10)) || 1;
|
||||||
|
|
||||||
const searchOnly = parseInt(req.query.searchOnly, 10) === 1;
|
const searchOnly = parseInt(req.query.searchOnly, 10) === 1;
|
||||||
|
|
||||||
async.waterfall([
|
const allowed = await privileges.global.can('search:content', req.uid);
|
||||||
function (next) {
|
if (!allowed) {
|
||||||
privileges.global.can('search:content', req.uid, next);
|
return helpers.notAllowed(req, res);
|
||||||
},
|
}
|
||||||
function (allowed, next) {
|
|
||||||
if (!allowed) {
|
if (req.query.categories && !Array.isArray(req.query.categories)) {
|
||||||
return helpers.notAllowed(req, res);
|
req.query.categories = [req.query.categories];
|
||||||
}
|
}
|
||||||
|
if (req.query.hasTags && !Array.isArray(req.query.hasTags)) {
|
||||||
if (req.query.categories && !Array.isArray(req.query.categories)) {
|
req.query.hasTags = [req.query.hasTags];
|
||||||
req.query.categories = [req.query.categories];
|
}
|
||||||
}
|
|
||||||
if (req.query.hasTags && !Array.isArray(req.query.hasTags)) {
|
const data = {
|
||||||
req.query.hasTags = [req.query.hasTags];
|
query: req.query.term,
|
||||||
}
|
searchIn: req.query.in || 'posts',
|
||||||
|
matchWords: req.query.matchWords || 'all',
|
||||||
var data = {
|
postedBy: req.query.by,
|
||||||
query: req.query.term,
|
categories: req.query.categories,
|
||||||
searchIn: req.query.in || 'posts',
|
searchChildren: req.query.searchChildren,
|
||||||
matchWords: req.query.matchWords || 'all',
|
hasTags: req.query.hasTags,
|
||||||
postedBy: req.query.by,
|
replies: req.query.replies,
|
||||||
categories: req.query.categories,
|
repliesFilter: req.query.repliesFilter,
|
||||||
searchChildren: req.query.searchChildren,
|
timeRange: req.query.timeRange,
|
||||||
hasTags: req.query.hasTags,
|
timeFilter: req.query.timeFilter,
|
||||||
replies: req.query.replies,
|
sortBy: req.query.sortBy || meta.config.searchDefaultSortBy || '',
|
||||||
repliesFilter: req.query.repliesFilter,
|
sortDirection: req.query.sortDirection,
|
||||||
timeRange: req.query.timeRange,
|
page: page,
|
||||||
timeFilter: req.query.timeFilter,
|
itemsPerPage: req.query.itemsPerPage,
|
||||||
sortBy: req.query.sortBy || meta.config.searchDefaultSortBy || '',
|
uid: req.uid,
|
||||||
sortDirection: req.query.sortDirection,
|
qs: req.query,
|
||||||
page: page,
|
};
|
||||||
itemsPerPage: req.query.itemsPerPage,
|
|
||||||
uid: req.uid,
|
const [searchData, categoriesData] = await Promise.all([
|
||||||
qs: req.query,
|
search.search(data),
|
||||||
};
|
buildCategories(req.uid, searchOnly),
|
||||||
|
]);
|
||||||
async.parallel({
|
|
||||||
categories: async.apply(buildCategories, req.uid, searchOnly),
|
searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
|
||||||
search: async.apply(search.search, data),
|
searchData.multiplePages = searchData.pageCount > 1;
|
||||||
}, next);
|
searchData.search_query = validator.escape(String(req.query.term || ''));
|
||||||
},
|
searchData.term = req.query.term;
|
||||||
function (results) {
|
|
||||||
var searchData = results.search;
|
if (searchOnly) {
|
||||||
|
return res.json(searchData);
|
||||||
searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
|
}
|
||||||
searchData.multiplePages = searchData.pageCount > 1;
|
|
||||||
searchData.search_query = validator.escape(String(req.query.term || ''));
|
searchData.categories = categoriesData;
|
||||||
searchData.term = req.query.term;
|
searchData.categoriesCount = Math.max(10, Math.min(20, categoriesData.length));
|
||||||
|
searchData.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[global:search]]' }]);
|
||||||
if (searchOnly) {
|
searchData.expandSearch = !req.query.term;
|
||||||
return res.json(searchData);
|
|
||||||
}
|
searchData.showAsPosts = !req.query.showAs || req.query.showAs === 'posts';
|
||||||
|
searchData.showAsTopics = req.query.showAs === 'topics';
|
||||||
results.categories = results.categories.filter(category => category && !category.link);
|
searchData.title = '[[global:header.search]]';
|
||||||
|
|
||||||
var categoriesData = [
|
searchData.searchDefaultSortBy = meta.config.searchDefaultSortBy || '';
|
||||||
{ value: 'all', text: '[[unread:all_categories]]' },
|
res.render('search', searchData);
|
||||||
{ value: 'watched', text: '[[category:watched-categories]]' },
|
|
||||||
].concat(results.categories);
|
|
||||||
|
|
||||||
|
|
||||||
searchData.categories = categoriesData;
|
|
||||||
searchData.categoriesCount = Math.max(10, Math.min(20, categoriesData.length));
|
|
||||||
searchData.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[global:search]]' }]);
|
|
||||||
searchData.expandSearch = !req.query.term;
|
|
||||||
|
|
||||||
searchData.showAsPosts = !req.query.showAs || req.query.showAs === 'posts';
|
|
||||||
searchData.showAsTopics = req.query.showAs === 'topics';
|
|
||||||
searchData.title = '[[global:header.search]]';
|
|
||||||
|
|
||||||
searchData.searchDefaultSortBy = meta.config.searchDefaultSortBy || '';
|
|
||||||
res.render('search', searchData);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function buildCategories(uid, searchOnly, callback) {
|
async function buildCategories(uid, searchOnly) {
|
||||||
if (searchOnly) {
|
if (searchOnly) {
|
||||||
return setImmediate(callback, null, []);
|
return [];
|
||||||
}
|
}
|
||||||
categories.buildForSelect(uid, 'read', callback);
|
let categoriesData = await categories.buildForSelect(uid, 'read');
|
||||||
|
categoriesData = categoriesData.filter(category => category && !category.link);
|
||||||
|
return [
|
||||||
|
{ value: 'all', text: '[[unread:all_categories]]' },
|
||||||
|
{ value: 'watched', text: '[[category:watched-categories]]' },
|
||||||
|
].concat(categoriesData);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue