autocompleteTags

v1.18.x
barisusakli 8 years ago
parent fdf2cb6f81
commit 8d897d8dcf

@ -4,6 +4,10 @@ var topics = require('../../topics');
var utils = require('../../../public/src/utils'); var utils = require('../../../public/src/utils');
module.exports = function(SocketTopics) { module.exports = function(SocketTopics) {
SocketTopics.autocompleteTags = function(socket, data, callback) {
topics.autocompleteTags(data, callback);
};
SocketTopics.searchTags = function(socket, data, callback) { SocketTopics.searchTags = function(socket, data, callback) {
topics.searchTags(data, callback); topics.searchTags(data, callback);
}; };

@ -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) { db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
data.query = data.query.toLowerCase(); query = query.toLowerCase();
var matches = []; var matches = [];
for(var i=0; i<tags.length; ++i) { for(var i=0; i<tags.length; ++i) {
if (tags[i].toLowerCase().startsWith(data.query)) { if (tags[i].toLowerCase().startsWith(query)) {
matches.push(tags[i]); matches.push(tags[i]);
if (matches.length > 19) { if (matches.length > 19) {
break; break;
@ -303,10 +329,9 @@ module.exports = function(Topics) {
matches = matches.sort(function(a, b) { matches = matches.sort(function(a, b) {
return a > b; return a > b;
}); });
callback(null, matches);
done(matches);
}); });
}; }
Topics.searchAndLoadTags = function(data, callback) { Topics.searchAndLoadTags = function(data, callback) {
var searchResult = { var searchResult = {

Loading…
Cancel
Save