fix tag counts

if a tag is removed from a topic, update number of topics in that tag
v1.18.x
barisusakli 11 years ago
parent bc2f7339ee
commit fa8fcc00ec

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async'),
winston = require('winston'),
db = require('../database'), db = require('../database'),
meta = require('../meta'), meta = require('../meta'),
plugins = require('../plugins'), plugins = require('../plugins'),
@ -56,10 +57,11 @@ module.exports = function(Topics) {
return tag; return tag;
}; };
function updateTagCount(tag) { function updateTagCount(tag, callback) {
callback = callback || function() {};
Topics.getTagTopicCount(tag, function(err, count) { Topics.getTagTopicCount(tag, function(err, count) {
if (!err) { if (!err) {
db.sortedSetAdd('tags:topic:count', count, tag); db.sortedSetAdd('tags:topic:count', count, tag, callback);
} }
}); });
} }
@ -85,7 +87,6 @@ module.exports = function(Topics) {
db.getSetMembers('topic:' + tid + ':tags', callback); db.getSetMembers('topic:' + tid + ':tags', callback);
}; };
//returns tags as objects cuz templates.js cant do arrays yet >_>
Topics.getTopicTagsObjects = function(tid, callback) { Topics.getTopicTagsObjects = function(tid, callback) {
Topics.getTopicTags(tid, function(err, tags) { Topics.getTopicTags(tid, function(err, tags) {
callback(err, mapToObject(tags)); callback(err, mapToObject(tags));
@ -103,13 +104,15 @@ module.exports = function(Topics) {
Topics.updateTags = function(tid, tags) { Topics.updateTags = function(tid, tags) {
Topics.getTopicField(tid, 'timestamp', function(err, timestamp) { Topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
if (!err) { if (err) {
return winston.error(err.message);
}
Topics.deleteTopicTags(tid, function(err) { Topics.deleteTopicTags(tid, function(err) {
if (!err) { if (!err) {
Topics.createTags(tags, tid, timestamp); Topics.createTags(tags, tid, timestamp);
} }
}); });
}
}); });
}; };
@ -119,7 +122,7 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
async.parallel([ async.series([
function(next) { function(next) {
db.delete('topic:' + tid + ':tags', next); db.delete('topic:' + tid + ':tags', next);
}, },
@ -129,6 +132,11 @@ module.exports = function(Topics) {
}); });
db.sortedSetsRemove(sets, tid, next); db.sortedSetsRemove(sets, tid, next);
},
function(next) {
async.each(tags, function(tag, next) {
updateTagCount(tag, next);
}, next);
} }
], callback); ], callback);
}); });

Loading…
Cancel
Save