diff --git a/public/src/modules/composer/tags.js b/public/src/modules/composer/tags.js index 2b50560d0c..3930376680 100644 --- a/public/src/modules/composer/tags.js +++ b/public/src/modules/composer/tags.js @@ -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); diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index b1af814351..68e16671a3 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -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; diff --git a/src/topics/tags.js b/src/topics/tags.js index fd6e7385ab..2103d840b9 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -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,26 +16,32 @@ 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); + } - async.each(tags, function(tag, next) { - tag = cleanUpTag(tag); + tags = data.tags.slice(0, meta.config.tagsPerTopic || 5); - if (tag.length < (meta.config.minimumTagLength || 3)) { - return next(); - } - db.setAdd('topic:' + tid + ':tags', tag); + async.each(tags, function(tag, next) { + tag = Topics.cleanUpTag(tag); - db.sortedSetAdd('tag:' + tag + ':topics', timestamp, tid, function(err) { - if (!err) { - updateTagCount(tag); + if (tag.length < (meta.config.minimumTagLength || 3)) { + return next(); } - next(err); - }); - }, callback); + db.setAdd('topic:' + tid + ':tags', tag); + + db.sortedSetAdd('tag:' + tag + ':topics', timestamp, tid, function(err) { + if (!err) { + updateTagCount(tag); + } + 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