some tag hooks/filters

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

@ -1,7 +1,7 @@
'use strict'; 'use strict';
/*globals define*/ /*globals define, config, socket, app*/
define('composer/tags', function() { define('composer/tags', function() {
var tags = {}; var tags = {};
@ -16,15 +16,20 @@ define('composer/tags', function() {
maxTags: config.tagsPerTopic, maxTags: config.tagsPerTopic,
confirmKeys: [13, 44] 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); addTags(postData.tags, tagEl);
var input = postContainer.find('.bootstrap-tagsinput input'); var input = postContainer.find('.bootstrap-tagsinput input');
input.autocomplete({ input.autocomplete({
delay: 100, delay: 100,
source: function(request, response) { 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) { if (err) {
return app.alertError(err.message) return app.alertError(err.message);
} }
if (tags) { if (tags) {
response(tags); response(tags);

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

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