some tag hooks/filters

v1.18.x
barisusakli 11 years ago
parent 9f6c05bb24
commit 6d8cc5648c

@ -1,7 +1,7 @@
'use strict';
/*globals define*/
/*globals define, config, socket, app*/
define('composer/tags', function() {
var tags = {};
@ -16,15 +16,20 @@ define('composer/tags', function() {
maxTags: config.tagsPerTopic,
confirmKeys: [13, 44]
});
tagEl.on('itemAdded', function(event) {
$(window).trigger('action:tag.added', {cid: postData.cid, tagEl: tagEl, tag: event.item});
});
addTags(postData.tags, tagEl);
var input = postContainer.find('.bootstrap-tagsinput input');
input.autocomplete({
delay: 100,
source: function(request, response) {
socket.emit('topics.searchTags', request.term, function(err, tags) {
socket.emit('topics.searchTags', {query: request.term, cid: postData.cid}, function(err, tags) {
if (err) {
return app.alertError(err.message)
return app.alertError(err.message);
}
if (tags) {
response(tags);

@ -412,8 +412,8 @@ SocketTopics.getTidIndex = function(socket, tid, callback) {
categories.getTopicIndex(tid, callback);
};
SocketTopics.searchTags = function(socket, query, callback) {
topics.searchTags(query, callback);
SocketTopics.searchTags = function(socket, data, callback) {
topics.searchTags(data, callback);
};
module.exports = SocketTopics;

@ -4,6 +4,7 @@
var async = require('async'),
db = require('../database'),
meta = require('../meta'),
plugins = require('../plugins'),
utils = require('../../public/src/utils');
module.exports = function(Topics) {
@ -15,10 +16,15 @@ module.exports = function(Topics) {
return callback();
}
tags = tags.slice(0, meta.config.tagsPerTopic || 5);
plugins.fireHook('filter:tags.filter', {tags: tags, tid: tid}, function(err, data) {
if (err) {
return callback(err);
}
tags = data.tags.slice(0, meta.config.tagsPerTopic || 5);
async.each(tags, function(tag, next) {
tag = cleanUpTag(tag);
tag = Topics.cleanUpTag(tag);
if (tag.length < (meta.config.minimumTagLength || 3)) {
return next();
@ -32,9 +38,10 @@ module.exports = function(Topics) {
next(err);
});
}, callback);
});
};
function cleanUpTag(tag) {
Topics.cleanUpTag = function(tag) {
if (typeof tag !== 'string' || !tag.length ) {
return '';
}
@ -47,7 +54,7 @@ module.exports = function(Topics) {
tag = matches[1];
}
return tag;
}
};
function updateTagCount(tag) {
Topics.getTagTopicCount(tag, function(err, count) {
@ -127,20 +134,28 @@ module.exports = function(Topics) {
});
};
Topics.searchTags = function(query, callback) {
if (!query || query.length === 0) {
Topics.searchTags = function(data, callback) {
if (!data) {
return callback(null, []);
}
db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) {
if (plugins.hasListeners('filter:tags.category')) {
plugins.fireHook('filter:tags.category', {tags: [], cid: data.cid}, function(err, data) {
doSearch(err, data ? data.tags : null);
});
} else {
db.getSortedSetRevRange('tags:topic:count', 0, -1, doSearch);
}
function doSearch(err, tags) {
if (err) {
return callback(null, []);
}
query = query.toLowerCase();
data.query = data.query.toLowerCase();
var matches = [];
for(var i=0; i<tags.length; ++i) {
if (tags[i].toLowerCase().indexOf(query) === 0) {
if (tags[i].toLowerCase().indexOf(data.query) === 0) {
matches.push(tags[i]);
}
}
@ -150,7 +165,7 @@ module.exports = function(Topics) {
});
callback(null, matches);
});
}
};
};
Loading…
Cancel
Save