From 8d897d8dcf317047f21286bb279f9e3d3a57a559 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 8 Sep 2016 17:24:20 +0300 Subject: [PATCH] autocompleteTags --- src/socket.io/topics/tags.js | 4 ++++ src/topics/tags.js | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/socket.io/topics/tags.js b/src/socket.io/topics/tags.js index f55ec377fb..54bad0e7b3 100644 --- a/src/socket.io/topics/tags.js +++ b/src/socket.io/topics/tags.js @@ -4,6 +4,10 @@ var topics = require('../../topics'); var utils = require('../../../public/src/utils'); module.exports = function(SocketTopics) { + SocketTopics.autocompleteTags = function(socket, data, callback) { + topics.autocompleteTags(data, callback); + }; + SocketTopics.searchTags = function(socket, data, callback) { topics.searchTags(data, callback); }; diff --git a/src/topics/tags.js b/src/topics/tags.js index 4d8e01b912..ee5f7d8b9d 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -283,16 +283,42 @@ module.exports = function(Topics) { }); } + findMatches(data.query, function(err, matches) { + if (err) { + return callback(err); + } + done(matches); + }); + }; + + Topics.autocompleteTags = function(data, callback) { + if (!data || !data.query) { + return callback(null, []); + } + + if (plugins.hasListeners('filter:topics.autocompleteTags')) { + return plugins.fireHook('filter:topics.autocompleteTags', {data: data}, function(err, data) { + if (err) { + return callback(err); + } + callback(null, data.matches); + }); + } + + findMatches(data.query, callback); + }; + + function findMatches(query, callback) { db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) { if (err) { return callback(err); } - data.query = data.query.toLowerCase(); + query = query.toLowerCase(); var matches = []; for(var i=0; i 19) { break; @@ -303,10 +329,9 @@ module.exports = function(Topics) { matches = matches.sort(function(a, b) { return a > b; }); - - done(matches); + callback(null, matches); }); - }; + } Topics.searchAndLoadTags = function(data, callback) { var searchResult = {