From c24c2a5422f8b333032f713f8691dc206c2ca3d7 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 3 Mar 2014 17:16:53 -0500 Subject: [PATCH] search and reset routes, the very last of the views to be refactored :shipit: --- src/controllers/index.js | 28 ++++++++++++++++ src/middleware/middleware.js | 6 ++++ src/routes/index.js | 65 ++++-------------------------------- 3 files changed, 40 insertions(+), 59 deletions(-) diff --git a/src/controllers/index.js b/src/controllers/index.js index 30a0e1f459..ec23734aec 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -88,6 +88,34 @@ Controllers.home = function(req, res, next) { }); }; +Controllers.search = function(req, res, next) { + var data = { + show_no_topics: 'hide', + show_no_posts: 'hide', + show_results: 'hide', + search_query: '', + posts: [], + topics: [] + }; + + if (res.locals.isAPI) { + res.json(data); + } else { + res.render('search', data); + } +}; + +Controllers.reset = function(req, res, next) { + var data = { + reset_code: req.params.code ? req.params.code : null + } + + if (res.locals.isAPI) { + res.json(data); + } else { + res.render('reset', data); + } +}; Controllers.login = function(req, res, next) { var data = {}, diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index aaee7cf0c9..205a5f2ffd 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -46,6 +46,12 @@ middleware.prepareAPI = function(req, res, next) { next(); }; +middleware.guestSearchingAllowed = function(req, res, next) { + if (!req.user && meta.config.allowGuestSearching !== '1') { + return res.redirect('/403'); + } +}; + middleware.checkGlobalPrivacySettings = function(req, res, next) { var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; diff --git a/src/routes/index.js b/src/routes/index.js index a95820385f..624eb7d19f 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -40,6 +40,12 @@ module.exports = function(app, middleware) { app.get('/outgoing', middleware.buildHeader, controllers.outgoing); 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('/reset/:code?', middleware.buildHeader, controllers.reset); + app.get('/api/reset/:code?', controllers.reset); + /* Static Pages */ app.get('/404', middleware.buildHeader, controllers.static['404']); app.get('/api/404', controllers.static['404']); @@ -120,65 +126,6 @@ module.exports = function(app, middleware) { app.get('/sitemap.xml', controllers.sitemap); app.get('/robots.txt', controllers.robots); - app.get('api/search/:term?', function (req, res) { - if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') { - return res.json({ - show_no_topics: 'hide', - show_no_posts: 'hide', - show_results: 'hide', - search_query: '', - posts: [], - topics: [] - }); - } else { - res.send(403); - } - }); - - app.get('/search/:term?', function (req, res) { - - if (!req.user && meta.config.allowGuestSearching !== '1') { - return res.redirect('/403'); - } - if(!req.params.term) { - req.params.term = ''; - } - app.build_header({ - req: req, - res: res - }, function (err, header) { - //res.send(header + app.create_route('search/' + req.params.term, null, 'search') + templates.footer); - }); - }); - - - app.get('/reset/:code', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route('reset/' + req.params.code) + templates.footer); - }); - }); - - app.get('api/reset/:code', function (req, res) { - res.json({ - reset_code: req.params.code - }); - }); - - app.get('/reset', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route('reset') + templates.footer); - }); - }); - - app.get('api/reset', function (req, res) { - res.json({}); - }); // Other routes