mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
fixed search routes
This commit is contained in:
@@ -13,6 +13,8 @@ var topicsController = require('./topics'),
|
|||||||
auth = require('./../routes/authentication'),
|
auth = require('./../routes/authentication'),
|
||||||
meta = require('./../meta'),
|
meta = require('./../meta'),
|
||||||
user = require('./../user'),
|
user = require('./../user'),
|
||||||
|
posts = require('./../posts'),
|
||||||
|
topics = require('./../topics'),
|
||||||
plugins = require('./../plugins'),
|
plugins = require('./../plugins'),
|
||||||
categories = require('./../categories'),
|
categories = require('./../categories'),
|
||||||
categoryTools = require('./../categoryTools');
|
categoryTools = require('./../categoryTools');
|
||||||
@@ -85,13 +87,67 @@ Controllers.home = function(req, res, next) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Controllers.search = function(req, res, next) {
|
Controllers.search = function(req, res, next) {
|
||||||
res.render('search', {
|
if (!req.params.term) {
|
||||||
show_no_topics: 'hide',
|
return res.render('search', {
|
||||||
show_no_posts: 'hide',
|
show_no_topics: 'hide',
|
||||||
show_results: 'hide',
|
show_no_posts: 'hide',
|
||||||
search_query: '',
|
show_results: 'hide',
|
||||||
posts: [],
|
search_query: '',
|
||||||
topics: []
|
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') {
|
if (!req.user && meta.config.allowGuestSearching !== '1') {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
middleware.checkGlobalPrivacySettings = function(req, res, 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) {
|
function upload(req, res, filesIterator, next) {
|
||||||
if(!req.user) {
|
if(!req.user) {
|
||||||
return res.json(403, {message:'not allowed'});
|
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('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID);
|
||||||
app.get('/get_templates_listing', getTemplatesListing);
|
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('/categories/:cid/moderators', getModerators);
|
||||||
app.get('/recent/posts/:term?', getRecentPosts);
|
app.get('/recent/posts/:term?', getRecentPosts);
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ module.exports = function(app, middleware) {
|
|||||||
app.get('/api/outgoing', controllers.outgoing);
|
app.get('/api/outgoing', controllers.outgoing);
|
||||||
|
|
||||||
app.get('/search/:term?', middleware.buildHeader, middleware.guestSearchingAllowed, controllers.search);
|
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('/reset/:code?', middleware.buildHeader, controllers.reset);
|
||||||
app.get('/api/reset/:code?', controllers.reset);
|
app.get('/api/reset/:code?', controllers.reset);
|
||||||
|
|||||||
Reference in New Issue
Block a user