added tag counts to api return

v1.18.x
barisusakli 11 years ago
parent 931d46fe7e
commit e10eaf0d50

@ -113,6 +113,10 @@ module.exports = function(db, module) {
}); });
}; };
module.sortedSetsCard = function(keys, callback) {
async.map(keys, module.sortedSetCard, callback);
};
module.sortedSetRank = function(key, value, callback) { module.sortedSetRank = function(key, value, callback) {
module.getListRange(key, 0, -1, function(err, list) { module.getListRange(key, 0, -1, function(err, list) {
for (var i = 0, ii=list.length; i< ii; i++) { for (var i = 0, ii=list.length; i< ii; i++) {

@ -101,6 +101,10 @@ module.exports = function(db, module) {
}); });
}; };
module.sortedSetsCard = function(keys, callback) {
async.map(keys, module.sortedSetCard, callback);
};
module.sortedSetRank = function(key, value, callback) { module.sortedSetRank = function(key, value, callback) {
getSortedSetRank(module.getSortedSetRange, key, value, callback); getSortedSetRank(module.getSortedSetRange, key, value, callback);
}; };

@ -51,6 +51,17 @@ module.exports = function(redisClient, module) {
redisClient.zcard(key, callback); redisClient.zcard(key, callback);
}; };
module.sortedSetsCard = function(keys, callback) {
if (Array.isArray(keys) && !keys.length) {
return callback(null, []);
}
var multi = redisClient.multi();
for(var i=0; i<keys.length; ++i) {
multi.zcard(keys[i]);
}
multi.exec(callback);
};
module.sortedSetRank = function(key, value, callback) { module.sortedSetRank = function(key, value, callback) {
redisClient.zrank(key, value, callback); redisClient.zrank(key, value, callback);
}; };

@ -5,6 +5,7 @@ var async = require('async'),
winston = require('winston'), winston = require('winston'),
db = require('../database'), db = require('../database'),
meta = require('../meta'), meta = require('../meta'),
_ = require('underscore'),
plugins = require('../plugins'), plugins = require('../plugins'),
utils = require('../../public/src/utils'); utils = require('../../public/src/utils');
@ -87,8 +88,8 @@ module.exports = function(Topics) {
}; };
Topics.getTopicTagsObjects = function(tid, callback) { Topics.getTopicTagsObjects = function(tid, callback) {
Topics.getTopicTags(tid, function(err, tags) { Topics.getTopicsTagsObjects([tid], function(err, data) {
callback(err, mapToObject(tags)); callback(err, Array.isArray(data) && data.length ? data[0] : []);
}); });
}; };
@ -102,21 +103,34 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
var uniqueTags = _.uniq(_.flatten(members));
var tagTopicSets = uniqueTags.map(function(tag) {
return 'tag:' + tag + ':topics';
});
db.sortedSetsCard(tagTopicSets, function(err, data) {
if (err) {
return callback(err);
}
var tagCounts = _.object(uniqueTags, data);
members.forEach(function(tags, index) { members.forEach(function(tags, index) {
if (Array.isArray(tags)) { if (Array.isArray(tags)) {
members[index] = mapToObject(tags); members[index] = mapToObject(tags, tagCounts);
} }
}) });
callback(null, members); callback(null, members);
}); });
});
}; };
function mapToObject(tags) { function mapToObject(tags, tagCounts) {
if (!tags) { if (!tags) {
return tags; return tags;
} }
return tags.map(function(tag) { return tags.map(function(tag) {
return {name: tag}; return {name: tag, score: tagCounts ? tagCounts[tag] : 0};
}); });
} }

Loading…
Cancel
Save