feat: convert search controller to async/await

v1.18.x
Baris Usakli 6 years ago
parent 6f375482a0
commit acf12e3dd8

@ -1,32 +1,27 @@
'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) {
privileges.global.can('search:content', req.uid, next);
},
function (allowed, next) {
if (!allowed) { if (!allowed) {
return helpers.notAllowed(req, res); return helpers.notAllowed(req, res);
} }
@ -38,7 +33,7 @@ searchController.search = function (req, res, next) {
req.query.hasTags = [req.query.hasTags]; req.query.hasTags = [req.query.hasTags];
} }
var data = { const data = {
query: req.query.term, query: req.query.term,
searchIn: req.query.in || 'posts', searchIn: req.query.in || 'posts',
matchWords: req.query.matchWords || 'all', matchWords: req.query.matchWords || 'all',
@ -58,13 +53,10 @@ searchController.search = function (req, res, next) {
qs: req.query, qs: req.query,
}; };
async.parallel({ const [searchData, categoriesData] = await Promise.all([
categories: async.apply(buildCategories, req.uid, searchOnly), search.search(data),
search: async.apply(search.search, data), buildCategories(req.uid, searchOnly),
}, next); ]);
},
function (results) {
var searchData = results.search;
searchData.pagination = pagination.create(page, searchData.pageCount, req.query); searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
searchData.multiplePages = searchData.pageCount > 1; searchData.multiplePages = searchData.pageCount > 1;
@ -75,14 +67,6 @@ searchController.search = function (req, res, next) {
return res.json(searchData); return res.json(searchData);
} }
results.categories = results.categories.filter(category => category && !category.link);
var categoriesData = [
{ value: 'all', text: '[[unread:all_categories]]' },
{ value: 'watched', text: '[[category:watched-categories]]' },
].concat(results.categories);
searchData.categories = categoriesData; searchData.categories = categoriesData;
searchData.categoriesCount = Math.max(10, Math.min(20, categoriesData.length)); searchData.categoriesCount = Math.max(10, Math.min(20, categoriesData.length));
searchData.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[global:search]]' }]); searchData.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[global:search]]' }]);
@ -94,13 +78,16 @@ searchController.search = function (req, res, next) {
searchData.searchDefaultSortBy = meta.config.searchDefaultSortBy || ''; searchData.searchDefaultSortBy = meta.config.searchDefaultSortBy || '';
res.render('search', searchData); 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…
Cancel
Save