From 9ac4704848ae36e4556a64a9dc1d34f7c5d1fd84 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 11 Oct 2015 22:27:53 -0400 Subject: [PATCH] more search cleanup --- public/src/admin/manage/tags.js | 8 +++--- public/src/client/tags.js | 2 +- src/search.js | 43 ++++++--------------------------- src/topics/tags.js | 26 ++++++++++++-------- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/public/src/admin/manage/tags.js b/public/src/admin/manage/tags.js index d85df5c266..27c5b2459b 100644 --- a/public/src/admin/manage/tags.js +++ b/public/src/admin/manage/tags.js @@ -1,5 +1,5 @@ "use strict"; -/*global define, socket, app, admin, utils, bootbox, RELATIVE_PATH*/ +/*global define, socket, app, utils, bootbox*/ define('admin/manage/tags', [ 'forum/infinitescroll', @@ -25,12 +25,12 @@ define('admin/manage/tags', [ } timeoutId = setTimeout(function() { - socket.emit('topics.searchAndLoadTags', {query: $('#tag-search').val()}, function(err, tags) { + socket.emit('topics.searchAndLoadTags', {query: $('#tag-search').val()}, function(err, result) { if (err) { return app.alertError(err.message); } - infinitescroll.parseAndTranslate('admin/manage/tags', 'tags', {tags: tags}, function(html) { + infinitescroll.parseAndTranslate('admin/manage/tags', 'tags', {tags: result.tags}, function(html) { $('.tag-list').html(html); utils.makeNumbersHumanReadable(html.find('.human-readable-number')); timeoutId = 0; @@ -43,7 +43,7 @@ define('admin/manage/tags', [ } function handleModify() { - $('#modify').on('click', function(ev) { + $('#modify').on('click', function() { var tagsToModify = $('.tag-row.selected'); if (!tagsToModify.length) { return; diff --git a/public/src/client/tags.js b/public/src/client/tags.js index 4763d3aa44..dedeb4a746 100644 --- a/public/src/client/tags.js +++ b/public/src/client/tags.js @@ -24,7 +24,7 @@ define('forum/tags', ['forum/infinitescroll'], function(infinitescroll) { if (err) { return app.alertError(err.message); } - onTagsLoaded(results, true, function() { + onTagsLoaded(results.tags, true, function() { timeoutId = 0; }); }); diff --git a/src/search.js b/src/search.js index 33fc7ffca8..94bc4a708f 100644 --- a/src/search.js +++ b/src/search.js @@ -22,15 +22,13 @@ search.search = function(data, callback) { return callback(err); } - result.search_query = validator.escape(query); + data.search_query = validator.escape(query); if (searchIn === 'titles' || searchIn === 'titlesposts') { searchIn = 'posts'; } - result[searchIn] = data.matches; - result.matchCount = data.matchCount; - result.pageCount = data.pageCount; - result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2); - callback(null, result); + + data.time = (process.elapsedTimeSince(start) / 1000).toFixed(2); + callback(null, data); } var start = process.hrtime(); @@ -38,18 +36,12 @@ search.search = function(data, callback) { var query = data.query; var searchIn = data.searchIn || 'titlesposts'; - var result = { - posts: [], - users: [], - tags: [] - }; - if (searchIn === 'posts' || searchIn === 'titles' || searchIn === 'titlesposts') { searchInContent(data, done); } else if (searchIn === 'users') { - searchInUsers(data, done); + user.search(data, done); } else if (searchIn === 'tags') { - searchInTags(query, done); + topics.searchAndLoadTags(data, done); } else { callback(new Error('[[error:unknown-search-filter]]')); } @@ -91,7 +83,7 @@ function searchInContent(data, callback) { var matchCount = 0; if (!results || (!results.pids.length && !results.tids.length)) { - return callback(null, {matches: [], matchCount: matchCount, pageCount: 1}); + return callback(null, {posts: [], matchCount: matchCount, pageCount: 1}); } async.waterfall([ @@ -118,7 +110,7 @@ function searchInContent(data, callback) { posts.getPostSummaryByPids(pids, data.uid, {}, next); }, function(posts, next) { - next(null, {matches: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10))}); + next(null, {posts: posts, matchCount: matchCount, pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10))}); } ], callback); }); @@ -423,25 +415,6 @@ function getSearchUids(data, callback) { } } -function searchInUsers(data, callback) { - user.search(data, function(err, results) { - if (err) { - return callback(err); - } - callback(null, {matches: results.users, matchCount: results.matchCount, pageCount: results.pageCount}); - }); -} - -function searchInTags(query, callback) { - topics.searchAndLoadTags({query: query}, function(err, tags) { - if (err) { - return callback(err); - } - - callback(null, {matches: tags, matchCount: tags.length, pageCount: 1}); - }); -} - search.searchQuery = function(index, content, cids, uids, callback) { plugins.fireHook('filter:search.query', { index: index, diff --git a/src/topics/tags.js b/src/topics/tags.js index fe2e8d8ea2..54514ddc42 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -2,12 +2,12 @@ 'use strict'; var async = require('async'), - winston = require('winston'), + db = require('../database'), meta = require('../meta'), _ = require('underscore'), - plugins = require('../plugins'), - utils = require('../../public/src/utils'); + plugins = require('../plugins'); + module.exports = function(Topics) { @@ -248,7 +248,7 @@ module.exports = function(Topics) { }; Topics.searchTags = function(data, callback) { - if (!data) { + if (!data || !data.query) { return callback(null, []); } @@ -256,9 +256,7 @@ module.exports = function(Topics) { if (err) { return callback(null, []); } - if (data.query === '') { - return callback(null, tags); - } + data.query = data.query.toLowerCase(); var matches = []; @@ -279,8 +277,14 @@ module.exports = function(Topics) { }; Topics.searchAndLoadTags = function(data, callback) { + var searchResult = { + tags: [], + matchCount: 0, + pageCount: 1 + }; + if (!data.query || !data.query.length) { - return callback(null, []); + return callback(null, searchResult); } Topics.searchTags(data, function(err, tags) { if (err) { @@ -307,8 +311,10 @@ module.exports = function(Topics) { results.tagData.sort(function(a, b) { return b.score - a.score; }); - - callback(null, results.tagData); + searchResult.tags = results.tagData; + searchResult.matchCount = results.tagData.length; + searchResult.pageCount = 1; + callback(null, searchResult); }); }); };