2013-05-09 01:04:15 -04:00
|
|
|
var RDB = require('./redis.js'),
|
|
|
|
|
posts = require('./posts.js'),
|
2013-05-23 13:21:38 -04:00
|
|
|
utils = require('./../public/src/utils.js'),
|
2013-05-18 14:59:50 -04:00
|
|
|
user = require('./user.js'),
|
2013-05-21 21:07:26 -04:00
|
|
|
async = require('async'),
|
|
|
|
|
topics = require('./topics.js');
|
2013-05-09 01:04:15 -04:00
|
|
|
|
|
|
|
|
(function(Categories) {
|
|
|
|
|
|
2013-05-24 11:18:28 -04:00
|
|
|
Categories.getCategoryById = function(category_id, current_user, callback) {
|
2013-07-09 12:36:17 -04:00
|
|
|
|
2013-07-22 16:47:41 -04:00
|
|
|
Categories.getCategoryData(category_id, function(err, categoryData) {
|
2013-07-22 20:29:51 -04:00
|
|
|
if (err) return callback(err);
|
|
|
|
|
|
2013-07-19 16:13:00 -04:00
|
|
|
var category_name = categoryData.name,
|
2013-07-23 17:21:44 -04:00
|
|
|
category_slug = categoryData.slug,
|
|
|
|
|
category_description = categoryData.description;
|
2013-07-19 16:13:00 -04:00
|
|
|
|
|
|
|
|
function getTopicIds(next) {
|
2013-08-09 20:03:19 -04:00
|
|
|
Categories.getTopicIds(category_id, 0, 19, next);
|
2013-07-19 16:13:00 -04:00
|
|
|
}
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2013-07-19 16:13:00 -04:00
|
|
|
function getActiveUsers(next) {
|
|
|
|
|
Categories.getActiveUsers(category_id, next);
|
|
|
|
|
}
|
2013-05-23 16:53:19 -04:00
|
|
|
|
2013-07-19 16:13:00 -04:00
|
|
|
async.parallel([getTopicIds, getActiveUsers], function(err, results) {
|
|
|
|
|
var tids = results[0],
|
|
|
|
|
active_users = results[1];
|
|
|
|
|
|
|
|
|
|
var categoryData = {
|
|
|
|
|
'category_name' : category_name,
|
2013-07-23 17:21:44 -04:00
|
|
|
'category_description': category_description,
|
2013-07-19 16:13:00 -04:00
|
|
|
'show_sidebar' : 'show',
|
2013-07-23 13:17:54 -04:00
|
|
|
'show_topic_button': 'inline-block',
|
2013-07-19 16:13:00 -04:00
|
|
|
'no_topics_message': 'hidden',
|
|
|
|
|
'topic_row_size': 'span9',
|
|
|
|
|
'category_id': category_id,
|
|
|
|
|
'active_users': [],
|
|
|
|
|
'topics' : [],
|
|
|
|
|
'twitter-intent-url': 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug) + '&text=' + encodeURIComponent(category_name),
|
|
|
|
|
'facebook-share-url': 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug),
|
|
|
|
|
'google-share-url': 'https://plus.google.com/share?url=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getTopics(next) {
|
2013-07-28 14:02:50 -04:00
|
|
|
topics.getTopicsByTids(tids, current_user, function(topicsData) {
|
|
|
|
|
next(null, topicsData);
|
2013-07-19 16:13:00 -04:00
|
|
|
}, category_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getModerators(next) {
|
|
|
|
|
Categories.getModerators(category_id, next);
|
|
|
|
|
}
|
2013-06-24 14:33:53 -04:00
|
|
|
|
2013-07-19 16:13:00 -04:00
|
|
|
function getActiveUsers(next) {
|
|
|
|
|
user.getMultipleUserFields(active_users, ['username', 'userslug', 'picture'], function(users) {
|
|
|
|
|
next(null, users);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tids.length === 0) {
|
|
|
|
|
getModerators(function(err, moderators) {
|
|
|
|
|
categoryData.moderator_block_class = moderators.length > 0 ? '' : 'none';
|
|
|
|
|
categoryData.moderators = moderators;
|
|
|
|
|
categoryData.show_sidebar = 'hidden';
|
|
|
|
|
categoryData.no_topics_message = 'show';
|
|
|
|
|
|
2013-07-22 20:29:51 -04:00
|
|
|
callback(null, categoryData);
|
2013-07-19 16:13:00 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
async.parallel([getTopics, getModerators, getActiveUsers], function(err, results) {
|
|
|
|
|
categoryData.topics = results[0];
|
|
|
|
|
categoryData.moderator_block_class = results[1].length > 0 ? '' : 'none';
|
|
|
|
|
categoryData.moderators = results[1];
|
|
|
|
|
categoryData.active_users = results[2];
|
2013-07-22 20:29:51 -04:00
|
|
|
callback(null, categoryData);
|
2013-07-19 16:13:00 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-24 07:52:15 -04:00
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
});
|
2013-05-23 16:53:19 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-09 20:03:19 -04:00
|
|
|
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
|
|
|
|
|
Categories.getTopicIds(cid, start, stop, function(err, tids) {
|
|
|
|
|
topics.getTopicsByTids(tids, uid, function(topicsData) {
|
|
|
|
|
callback(topicsData);
|
|
|
|
|
}, cid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Categories.getTopicIds = function(cid, start, stop, callback) {
|
|
|
|
|
RDB.zrevrange('categories:' + cid + ':tid', start, stop, callback);
|
2013-07-19 16:13:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Categories.getActiveUsers = function(cid, callback) {
|
|
|
|
|
RDB.smembers('cid:' + cid + ':active_users', callback);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 21:07:26 -04:00
|
|
|
Categories.getAllCategories = function(callback, current_user) {
|
2013-05-09 01:04:15 -04:00
|
|
|
RDB.lrange('categories:cid', 0, -1, function(err, cids) {
|
|
|
|
|
RDB.handle(err);
|
2013-05-24 08:34:13 -04:00
|
|
|
Categories.getCategories(cids, callback, current_user);
|
2013-05-09 01:04:15 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 12:49:39 -04:00
|
|
|
Categories.getModerators = function(cid, callback) {
|
|
|
|
|
RDB.smembers('cid:' + cid + ':moderators', function(err, mods) {
|
2013-07-19 16:13:00 -04:00
|
|
|
if(!err) {
|
|
|
|
|
if(mods && mods.length) {
|
|
|
|
|
user.getMultipleUserFields(mods, ['username'], function(moderators) {
|
|
|
|
|
callback(null, moderators);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
}
|
2013-05-16 13:14:23 -04:00
|
|
|
|
2013-05-16 12:49:39 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 18:12:39 -04:00
|
|
|
|
2013-05-24 11:18:28 -04:00
|
|
|
Categories.privileges = function(cid, uid, callback) {
|
|
|
|
|
function isModerator(next) {
|
|
|
|
|
user.isModerator(uid, cid, function(isMod) {
|
|
|
|
|
next(null, isMod);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isAdministrator(next) {
|
|
|
|
|
user.isAdministrator(uid, function(isAdmin) {
|
|
|
|
|
next(null, isAdmin);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([isModerator, isAdministrator], function(err, results) {
|
|
|
|
|
callback({
|
|
|
|
|
editable: results.indexOf(true) !== -1 ? true : false,
|
|
|
|
|
view_deleted: results.indexOf(true) !== -1 ? true : false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-28 12:40:15 -04:00
|
|
|
Categories.isTopicsRead = function(cid, uid, callback) {
|
2013-08-09 20:03:19 -04:00
|
|
|
RDB.zrange('categories:' + cid + ':tid', 0, -1, function(err, tids) {
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2013-06-28 12:40:15 -04:00
|
|
|
topics.hasReadTopics(tids, uid, function(hasRead) {
|
|
|
|
|
|
|
|
|
|
var allread = true;
|
|
|
|
|
for (var i=0, ii=tids.length; i<ii; i++) {
|
|
|
|
|
if(hasRead[i] === 0) {
|
|
|
|
|
allread = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
callback(allread);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Categories.markAsRead = function(cid, uid) {
|
|
|
|
|
RDB.sadd('cid:' + cid + ':read_by_uid', uid);
|
|
|
|
|
}
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2013-05-21 18:12:39 -04:00
|
|
|
Categories.hasReadCategories = function(cids, uid, callback) {
|
|
|
|
|
var batch = RDB.multi();
|
|
|
|
|
|
|
|
|
|
for (var i=0, ii=cids.length; i<ii; i++) {
|
|
|
|
|
batch.sismember('cid:' + cids[i] + ':read_by_uid', uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
batch.exec(function(err, hasRead) {
|
|
|
|
|
callback(hasRead);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
Categories.hasReadCategory = function(cid, uid, callback) {
|
|
|
|
|
RDB.sismember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
|
|
|
|
|
RDB.handle(err);
|
|
|
|
|
|
|
|
|
|
callback(hasRead);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 22:02:24 -04:00
|
|
|
Categories.getRecentReplies = function(cid, count, callback) {
|
2013-07-15 14:34:15 -04:00
|
|
|
RDB.zrevrange('categories:recent_posts:cid:' + cid, 0, (count<10)?10:count, function(err, pids) {
|
2013-08-08 14:30:42 -04:00
|
|
|
if(err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
callback([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-07-02 19:46:58 -04:00
|
|
|
|
2013-05-24 15:05:59 -04:00
|
|
|
if (pids.length == 0) {
|
2013-07-06 22:02:24 -04:00
|
|
|
callback([]);
|
2013-05-24 15:05:59 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-07-15 14:34:15 -04:00
|
|
|
|
2013-05-24 14:34:55 -04:00
|
|
|
posts.getPostSummaryByPids(pids, function(posts) {
|
2013-07-15 14:34:15 -04:00
|
|
|
if(posts.length > count) {
|
|
|
|
|
posts = posts.slice(0, count);
|
|
|
|
|
}
|
2013-05-24 14:34:55 -04:00
|
|
|
callback(posts);
|
|
|
|
|
});
|
2013-05-24 12:46:19 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-21 18:12:39 -04:00
|
|
|
|
2013-07-22 12:07:05 -04:00
|
|
|
Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
|
|
|
|
|
topics.getPids(tid, function(err, pids) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
|
|
|
|
|
function movePost(pid, callback) {
|
|
|
|
|
posts.getPostField(pid, 'timestamp', function(timestamp) {
|
|
|
|
|
RDB.zrem('categories:recent_posts:cid:' + oldCid, pid);
|
|
|
|
|
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(pids, movePost, function(err) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
callback(null, 1)
|
|
|
|
|
} else {
|
|
|
|
|
console.log(err);
|
|
|
|
|
callback(err, null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.log(err);
|
|
|
|
|
callback(err, null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
Categories.getCategoryData = function(cid, callback) {
|
2013-07-22 20:29:51 -04:00
|
|
|
RDB.exists('category:' + cid, function(err, exists) {
|
|
|
|
|
if (exists) RDB.hgetall('category:' + cid, callback);
|
|
|
|
|
else callback(new Error('No category found!'));
|
|
|
|
|
});
|
2013-07-22 16:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Categories.getCategoryFields = function(cid, fields, callback) {
|
2013-07-22 17:08:07 -04:00
|
|
|
RDB.hmgetObject('category:' + cid, fields, function(err, data) {
|
|
|
|
|
if(err === null)
|
|
|
|
|
callback(data);
|
2013-07-22 16:47:41 -04:00
|
|
|
else
|
2013-07-03 12:59:45 -04:00
|
|
|
console.log(err);
|
2013-07-22 16:47:41 -04:00
|
|
|
});
|
2013-07-03 12:59:45 -04:00
|
|
|
}
|
2013-07-04 13:29:29 -04:00
|
|
|
|
|
|
|
|
Categories.setCategoryField = function(cid, field, value) {
|
|
|
|
|
RDB.hset('category:' + cid, field, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Categories.incrementCategoryFieldBy = function(cid, field, value) {
|
|
|
|
|
RDB.hincrby('category:' + cid, field, value);
|
|
|
|
|
}
|
2013-07-03 12:59:45 -04:00
|
|
|
|
2013-05-24 08:34:13 -04:00
|
|
|
Categories.getCategories = function(cids, callback, current_user) {
|
2013-07-22 16:57:18 -04:00
|
|
|
if (!cids || !Array.isArray(cids) || cids.length === 0) {
|
2013-05-24 11:18:28 -04:00
|
|
|
callback({'categories' : []});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
var categories = [];
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2013-07-22 16:57:18 -04:00
|
|
|
function getCategory(cid, callback) {
|
|
|
|
|
Categories.getCategoryData(cid, function(err, categoryData) {
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2013-07-22 16:57:18 -04:00
|
|
|
if(err) {
|
|
|
|
|
callback(err);
|
2013-07-03 12:59:45 -04:00
|
|
|
return;
|
2013-07-22 16:57:18 -04:00
|
|
|
}
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-07-22 16:57:18 -04:00
|
|
|
Categories.hasReadCategory(cid, current_user, function(hasRead) {
|
2013-07-03 12:59:45 -04:00
|
|
|
categoryData['badgeclass'] = (parseInt(categoryData.topic_count,10) === 0 || (hasRead && current_user != 0)) ? '' : 'badge-important';
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
categories.push(categoryData);
|
2013-07-22 16:57:18 -04:00
|
|
|
callback(null);
|
2013-07-03 12:59:45 -04:00
|
|
|
}) ;
|
2013-07-22 16:57:18 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.eachSeries(cids, getCategory, function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
callback(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback({'categories': categories});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}(exports));
|
2013-05-09 01:04:15 -04:00
|
|
|
|