Node.js based forum software built for the modern web
fixed search routes
psychobunny6ea4d89edc0d
diff --git a/src/controllers/index.js b/src/controllers/index.jsindex a40c6c4626..e43d08a9c1 100644--- a/src/controllers/index.js+++ b/src/controllers/index.js@@ -13,6 +13,8 @@ var topicsController = require('./topics'),auth = require('./../routes/authentication'),meta = require('./../meta'),user = require('./../user'),+ posts = require('./../posts'),+ topics = require('./../topics'),plugins = require('./../plugins'),categories = require('./../categories'),categoryTools = require('./../categoryTools');@@ -85,13 +87,67 @@ Controllers.home = function(req, res, next) {};Controllers.search = function(req, res, next) {- res.render('search', {- show_no_topics: 'hide',- show_no_posts: 'hide',- show_results: 'hide',- search_query: '',- posts: [],- topics: []+ if (!req.params.term) {+ return res.render('search', {+ show_no_topics: 'hide',+ show_no_posts: 'hide',+ show_results: 'hide',+ search_query: '',+ posts: [],+ topics: []+ });+ }++ if (!plugins.hasListeners('filter:search.query')) {+ return res.redirect('/404');+ }++ function searchPosts(callback) {+ plugins.fireHook('filter:search.query', {+ index: 'post',+ query: req.params.term+ }, function(err, pids) {+ if (err) {+ return callback(err);+ }++ posts.getPostSummaryByPids(pids, false, callback);+ });+ }++ function searchTopics(callback) {+ plugins.fireHook('filter:search.query', {+ index: 'topic',+ query: req.params.term+ }, function(err, tids) {+ if (err) {+ return callback(err);+ }++ topics.getTopicsByTids(tids, 0, callback);+ });+ }++ async.parallel([searchPosts, searchTopics], function (err, results) {+ if (err) {+ return next(err);+ }++ if(!results) {+ results = [];+ results[0] = results[1] = [];+ }++ return res.render('search', {+ show_no_topics: results[1].length ? 'hide' : '',+ show_no_posts: results[0].length ? 'hide' : '',+ show_results: '',+ search_query: req.params.term,+ posts: results[0],+ topics: results[1],+ post_matches : results[0].length,+ topic_matches : results[1].length+ });});};diff --git a/src/middleware/middleware.js b/src/middleware/middleware.jsindex 2cb4901422..2992d87fc7 100644--- a/src/middleware/middleware.js+++ b/src/middleware/middleware.js@@ -50,6 +50,8 @@ middleware.guestSearchingAllowed = function(req, res, next) {if (!req.user && meta.config.allowGuestSearching !== '1') {return res.redirect('/403');}++ next();};middleware.checkGlobalPrivacySettings = function(req, res, next) {diff --git a/src/routes/api.js b/src/routes/api.jsindex 62f3a46327..6a79159416 100644--- a/src/routes/api.js+++ b/src/routes/api.js@@ -16,60 +16,6 @@ var path = require('path'),-function searchTerm(req, res, next) {- if (!plugins.hasListeners('filter:search.query')) {- return res.redirect('/404');- }-- function searchPosts(callback) {- plugins.fireHook('filter:search.query', {- index: 'post',- query: req.params.term- }, function(err, pids) {- if (err) {- return callback(err);- }-- posts.getPostSummaryByPids(pids, false, callback);- });- }-- function searchTopics(callback) {- plugins.fireHook('filter:search.query', {- index: 'topic',- query: req.params.term- }, function(err, tids) {- if (err) {- return callback(err);- }-- topics.getTopicsByTids(tids, 0, callback);- });- }-- async.parallel([searchPosts, searchTopics], function (err, results) {- if (err) {- return next(err);- }-- if(!results) {- results = [];- results[0] = results[1] = [];- }-- return res.json({- show_no_topics: results[1].length ? 'hide' : '',- show_no_posts: results[0].length ? 'hide' : '',- show_results: '',- search_query: req.params.term,- posts: results[0],- topics: results[1],- post_matches : results[0].length,- topic_matches : results[1].length- });- });-}-function upload(req, res, filesIterator, next) {if(!req.user) {return res.json(403, {message:'not allowed'});@@ -162,7 +108,6 @@ module.exports = function(app, middleware, controllers) {app.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID);app.get('/get_templates_listing', getTemplatesListing);- app.get('/search/:term', middleware.guestSearchingAllowed, searchTerm); // todo: look at this, may not belong here.app.get('/categories/:cid/moderators', getModerators);app.get('/recent/posts/:term?', getRecentPosts);diff --git a/src/routes/index.js b/src/routes/index.jsindex 1729294169..892fce5f5d 100644--- a/src/routes/index.js+++ b/src/routes/index.js@@ -40,7 +40,8 @@ module.exports = function(app, middleware) {app.get('/api/outgoing', controllers.outgoing);app.get('/search/:term?', middleware.buildHeader, middleware.guestSearchingAllowed, controllers.search);- app.get('/api/search/:term?', middleware.guestSearchingAllowed, controllers.search);+ app.get('/api/search/:term', middleware.prepareAPI, middleware.guestSearchingAllowed, controllers.search); // todo: look at this, may not belong here.+ //app.get('/api/search/:term?', middleware.guestSearchingAllowed, controllers.search);app.get('/reset/:code?', middleware.buildHeader, controllers.reset);app.get('/api/reset/:code?', controllers.reset);