fixed search routes

v1.18.x
psychobunny 11 years ago
parent 875c8ed66c
commit 6ea4d89edc

@ -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
});
});
};

@ -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) {

@ -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);

@ -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);

Loading…
Cancel
Save