also fixed a bug where tags with 0 topics were showing up in /tags
This commit is contained in:
barisusakli
2014-11-13 14:29:44 -05:00
parent 45affa3043
commit 68e8039ac7
5 changed files with 81 additions and 7 deletions

View File

@@ -49,7 +49,7 @@ tagsController.getTag = function(req, res, next) {
};
tagsController.getTags = function(req, res, next) {
topics.getTags(0, 99, function(err, tags) {
topics.getTags(0, 100, function(err, tags) {
if (err) {
return next(err);
}

View File

@@ -11,5 +11,9 @@ Tags.update = function(socket, data, callback) {
topics.updateTag(data.tag, data, callback);
};
Tags.deleteTags = function(socket, data, callback) {
topics.deleteTags(data.tags, callback);
};
module.exports = Tags;

View File

@@ -64,7 +64,7 @@ module.exports = function(Topics) {
function updateTagCount(tag, callback) {
callback = callback || function() {};
Topics.getTagTopicCount(tag, function(err, count) {
if (!err) {
if (!err && count) {
db.sortedSetAdd('tags:topic:count', count, tag, callback);
}
});
@@ -78,13 +78,49 @@ module.exports = function(Topics) {
db.sortedSetCard('tag:' + tag + ':topics', callback);
};
Topics.deleteTags = function(tags, callback) {
if (!Array.isArray(tags) || !tags.length) {
return callback();
}
async.series([
function(next) {
removeTagsFromTopics(tags, next);
},
function(next) {
var keys = tags.map(function(tag) {
return 'tag:' + tag + ':topics';
});
db.deleteAll(keys, next);
},
function(next) {
db.sortedSetRemove('tags:topic:count', tags, next);
}
], callback);
};
function removeTagsFromTopics(tags, callback) {
async.eachLimit(tags, 50, function(tag, next) {
db.getSortedSetRange('tag:' + tag + ':topics', 0, -1, function(err, tids) {
if (err || !tids.length) {
return next(err);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid + ':tags';
});
db.setsRemove(keys, tag, next);
});
}, callback);
}
Topics.deleteTag = function(tag) {
db.delete('tag:' + tag + ':topics');
db.sortedSetRemove('tags:topic:count', tag);
};
Topics.getTags = function(start, end, callback) {
db.getSortedSetRevRangeWithScores('tags:topic:count', start, end, function(err, tags) {
Topics.getTags = function(start, count, callback) {
db.getSortedSetRevRangeByScoreWithScores('tags:topic:count', start, count, '+inf', 1, function(err, tags) {
if (err) {
return callback(err);
}

View File

@@ -40,6 +40,7 @@
<div class="panel-body">
<p>Select tags via clicking and/or dragging, use shift to select multiple.</p>
<button class="btn btn-primary btn-md" id="modify">Modify Selected Tags</button>
<button class="btn btn-warning btn-md" id="deleteSelected">Delete Selected Tags</button>
</div>
</div>
@@ -50,5 +51,5 @@
</div>
</div>
</div>
</div>