Files
NodeBB/src/categories.js

425 lines
11 KiB
JavaScript
Raw Normal View History

2013-12-02 21:20:55 -05:00
var db = require('./database.js'),
posts = require('./posts.js'),
utils = require('./../public/src/utils.js'),
user = require('./user.js'),
async = require('async'),
topics = require('./topics.js'),
plugins = require('./plugins'),
winston = require('winston'),
nconf = require('nconf');
(function(Categories) {
2013-09-17 15:54:08 -04:00
"use strict";
2013-09-11 16:04:34 -04:00
Categories.create = function(data, callback) {
2013-12-02 21:20:55 -05:00
db.incrObjectField('global', 'nextCid', function(err, cid) {
2013-09-17 15:54:08 -04:00
if (err) {
2013-09-11 16:04:34 -04:00
return callback(err, null);
2013-09-17 15:54:08 -04:00
}
2013-09-11 16:04:34 -04:00
var slug = cid + '/' + utils.slugify(data.name);
2013-12-02 21:20:55 -05:00
db.listAppend('categories:cid', cid);
2013-09-11 16:04:34 -04:00
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
2013-12-08 14:20:57 -05:00
bgColor: data.bgColor,
color: data.color,
2013-09-11 16:04:34 -04:00
slug: slug,
topic_count: 0,
2013-10-07 17:48:11 -04:00
disabled: 0,
order: data.order
2013-09-11 16:04:34 -04:00
};
2013-12-02 21:20:55 -05:00
db.setObject('category:' + cid, category, function(err, data) {
callback(err, category);
});
2013-09-11 16:04:34 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-09-11 16:04:34 -04:00
Categories.getCategoryById = function(category_id, current_user, callback) {
Categories.getCategoryData(category_id, function(err, categoryData) {
2013-09-17 15:54:08 -04:00
if (err) {
return callback(err);
}
2013-07-19 16:13:00 -04:00
var category_name = categoryData.name,
category_slug = categoryData.slug,
2013-08-30 14:47:31 -04:00
disabled = categoryData.disabled || '0',
category_description = categoryData.description;
2013-07-19 16:13:00 -04:00
function getTopicIds(next) {
Categories.getTopicIds(category_id, 0, 19, next);
2013-07-19 16:13:00 -04:00
}
2013-07-19 16:13:00 -04:00
function getActiveUsers(next) {
Categories.getActiveUsers(category_id, next);
}
function getSidebars(next) {
plugins.fireHook('filter:category.build_sidebars', [], function(err, sidebars) {
next(err, sidebars);
});
}
async.parallel([getTopicIds, getActiveUsers, getSidebars], function(err, results) {
2013-07-19 16:13:00 -04:00
var tids = results[0],
active_users = results[1],
sidebars = results[2];
2013-07-19 16:13:00 -04:00
var categoryData = {
2013-09-17 13:09:37 -04:00
'category_name': category_name,
'category_description': category_description,
2013-08-30 14:47:31 -04:00
'disabled': disabled,
2013-09-17 13:09:37 -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': 'col-md-9',
2013-07-19 16:13:00 -04:00
'category_id': category_id,
'active_users': [],
2013-09-17 13:09:37 -04:00
'topics': [],
2014-01-02 14:26:08 -05:00
'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false,
2013-08-19 13:31:04 -04:00
'twitter-intent-url': 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(nconf.get('url') + 'category/' + category_slug) + '&text=' + encodeURIComponent(category_name),
'facebook-share-url': 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(nconf.get('url') + 'category/' + category_slug),
'google-share-url': 'https://plus.google.com/share?url=' + encodeURIComponent(nconf.get('url') + 'category/' + category_slug),
'sidebars': sidebars
2013-07-19 16:13:00 -04:00
};
function getTopics(next) {
topics.getTopicsByTids(tids, current_user, function(topicsData) {
next(null, topicsData);
2013-07-19 16:13:00 -04:00
}, category_id);
}
2013-08-20 12:11:17 -04:00
2013-07-19 16:13:00 -04:00
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) {
2013-11-01 16:01:48 -04:00
user.getMultipleUserFields(active_users, ['uid', 'username', 'userslug', 'picture'], function(err, users) {
2013-09-11 12:49:54 -04:00
next(err, users);
2013-07-19 16:13:00 -04:00
});
}
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';
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-09-17 13:09:37 -04:00
categoryData.show_sidebar = categoryData.topics.length > 0 ? 'show' : 'hidden';
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-09-12 14:07:24 -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);
});
2013-09-12 14:07:24 -04:00
};
Categories.getTopicIds = function(cid, start, stop, callback) {
2013-12-02 21:20:55 -05:00
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
2013-09-12 14:07:24 -04:00
};
2013-07-19 16:13:00 -04:00
Categories.getActiveUsers = function(cid, callback) {
2013-12-02 21:20:55 -05:00
db.getSetMembers('cid:' + cid + ':active_users', callback);
2013-09-12 14:07:24 -04:00
};
2013-07-19 16:13:00 -04:00
2013-11-26 14:25:46 -05:00
Categories.getAllCategories = function(current_user, callback) {
2013-12-02 21:20:55 -05:00
db.getListRange('categories:cid', 0, -1, function(err, cids) {
2013-11-26 19:09:32 -05:00
if(err) {
return callback(err);
}
if(cids && cids.length === 0) {
return callback(null, {categories : []});
}
2013-11-27 15:02:09 -05:00
2013-11-26 14:25:46 -05:00
Categories.getCategories(cids, current_user, callback);
});
2013-09-12 14:07:24 -04:00
};
2013-05-16 12:49:39 -04:00
Categories.getModerators = function(cid, callback) {
2013-12-02 21:20:55 -05:00
db.getSetMembers('cid:' + cid + ':moderators', function(err, mods) {
2013-09-17 13:09:37 -04:00
if (!err) {
if (mods && mods.length) {
2013-09-11 12:49:54 -04:00
user.getMultipleUserFields(mods, ['username'], function(err, moderators) {
callback(err, moderators);
2013-07-19 16:13:00 -04:00
});
} else {
callback(null, []);
}
} else {
callback(err, null);
}
2013-05-16 13:14:23 -04:00
2013-05-16 12:49:39 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-05-16 12:49:39 -04:00
2013-06-28 12:40:15 -04:00
Categories.isTopicsRead = function(cid, uid, callback) {
2013-12-02 21:20:55 -05:00
db.getSortedSetRange('categories:' + cid + ':tid', 0, -1, function(err, tids) {
2013-06-28 12:40:15 -04:00
topics.hasReadTopics(tids, uid, function(hasRead) {
var allread = true;
2013-09-17 13:09:37 -04:00
for (var i = 0, ii = tids.length; i < ii; i++) {
if (hasRead[i] === 0) {
2013-06-28 12:40:15 -04:00
allread = false;
break;
}
}
callback(allread);
2013-06-28 12:40:15 -04:00
});
});
2013-09-12 14:07:24 -04:00
};
2013-06-28 12:40:15 -04:00
Categories.markAsRead = function(cid, uid) {
2013-12-02 21:20:55 -05:00
db.setAdd('cid:' + cid + ':read_by_uid', uid);
2013-09-12 14:07:24 -04:00
};
Categories.hasReadCategories = function(cids, uid, callback) {
2013-12-04 15:13:43 -05:00
var sets = [];
2013-09-17 13:09:37 -04:00
for (var i = 0, ii = cids.length; i < ii; i++) {
2013-12-04 15:13:43 -05:00
sets.push('cid:' + cids[i] + ':read_by_uid');
}
2013-08-20 12:11:17 -04:00
2013-12-04 15:13:43 -05:00
db.isMemberOfSets(sets, uid, function(err, hasRead) {
callback(hasRead);
});
2013-09-12 14:07:24 -04:00
};
2013-07-03 12:59:45 -04:00
Categories.hasReadCategory = function(cid, uid, callback) {
2013-12-02 21:20:55 -05:00
db.isSetMember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
if(err) {
return callback(false);
}
2013-08-20 12:11:17 -04:00
2013-07-03 12:59:45 -04:00
callback(hasRead);
2013-08-20 12:11:17 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-07-03 12:59:45 -04:00
Categories.getRecentReplies = function(cid, count, callback) {
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
2013-08-20 12:11:17 -04:00
2013-09-17 13:09:37 -04:00
if (err) {
winston.err(err);
2013-12-10 15:39:53 -05:00
return callback(err, []);
2013-08-08 14:30:42 -04:00
}
2013-07-02 19:46:58 -04:00
2013-09-12 14:07:24 -04:00
if (pids.length === 0) {
2013-12-10 15:39:53 -05:00
return callback(null, []);
}
2013-07-15 14:34:15 -04:00
2013-12-06 21:08:21 -05:00
posts.getPostSummaryByPids(pids, true, function(err, postData) {
if(err) {
return callback(err);
2013-07-15 14:34:15 -04:00
}
callback(null, postData);
});
2013-05-24 12:46:19 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-07-22 12:07:05 -04:00
Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
2013-09-12 14:07:24 -04:00
function movePost(pid, callback) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return callback(err);
}
2013-12-02 21:20:55 -05:00
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
2013-11-15 14:57:50 -05:00
callback(null);
2013-09-12 14:07:24 -04:00
});
}
2013-07-22 12:07:05 -04:00
topics.getPids(tid, function(err, pids) {
2013-11-15 14:57:50 -05:00
if(err) {
return callback(err, null);
2013-07-22 12:07:05 -04:00
}
2013-11-15 14:57:50 -05:00
async.each(pids, movePost, function(err) {
if(err) {
return callback(err, null);
}
callback(null, 1);
});
2013-07-22 12:07:05 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-07-22 12:07:05 -04:00
2013-08-27 13:32:43 -04:00
Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
2013-09-12 14:07:24 -04:00
function updateUser(uid) {
Categories.addActiveUser(cid, uid);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
2013-08-27 13:32:43 -04:00
2013-09-17 13:09:37 -04:00
if (!err && !active) {
2013-09-12 14:07:24 -04:00
Categories.removeActiveUser(oldCid, uid);
2013-08-27 13:32:43 -04:00
}
2013-09-12 14:07:24 -04:00
});
}
2013-08-27 13:32:43 -04:00
2013-09-12 14:07:24 -04:00
topics.getUids(tid, function(err, uids) {
2013-09-17 13:09:37 -04:00
if (!err && uids) {
for (var i = 0; i < uids.length; ++i) {
2013-08-27 13:32:43 -04:00
updateUser(uids[i]);
}
}
});
2013-09-12 14:07:24 -04:00
};
2013-08-27 13:32:43 -04:00
2013-07-03 12:59:45 -04:00
Categories.getCategoryData = function(cid, callback) {
2013-12-02 21:20:55 -05:00
db.exists('category:' + cid, function(err, exists) {
2013-09-17 15:54:08 -04:00
if (exists) {
db.getObject('category:' + cid, function(err, data) {
data.background = data.image ? 'url(' + data.image + ')' : data.bgColor;
callback(err, data);
});
2013-09-17 15:54:08 -04:00
} else {
callback(new Error('No category found!'));
}
});
2013-09-12 14:07:24 -04:00
};
2013-08-20 12:11:17 -04:00
Categories.getCategoryField = function(cid, field, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectField('category:' + cid, field, callback);
2013-09-12 14:07:24 -04:00
};
Categories.getCategoryFields = function(cid, fields, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectFields('category:' + cid, fields, callback);
2013-09-12 14:07:24 -04:00
};
2013-08-20 12:11:17 -04:00
2013-11-27 15:02:09 -05:00
Categories.setCategoryField = function(cid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.setObjectField('category:' + cid, field, value, callback);
2013-09-12 14:07:24 -04:00
};
2013-11-27 15:02:09 -05:00
Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.incrObjectFieldBy('category:' + cid, field, value, callback);
2013-09-12 14:07:24 -04:00
};
2013-07-03 12:59:45 -04:00
2013-11-26 14:25:46 -05:00
Categories.getCategories = function(cids, uid, callback) {
if (!cids || !Array.isArray(cids) || cids.length === 0) {
2013-11-26 14:25:46 -05:00
return callback(new Error('invalid-cids'), null);
}
2013-08-20 12:11:17 -04:00
function getCategory(cid, callback) {
Categories.getCategoryData(cid, function(err, categoryData) {
2013-09-17 13:09:37 -04:00
if (err) {
winston.warn('Attempted to retrieve cid ' + cid + ', but nothing was returned!');
2013-11-26 14:25:46 -05:00
return callback(err, null);
}
2013-11-26 14:25:46 -05:00
Categories.hasReadCategory(cid, uid, function(hasRead) {
categoryData.badgeclass = (parseInt(categoryData.topic_count, 10) === 0 || (hasRead && uid !== 0)) ? '' : 'badge-important';
callback(null, categoryData);
2013-09-17 13:09:37 -04:00
});
2013-08-20 12:11:17 -04:00
});
}
2013-08-20 12:11:17 -04:00
async.map(cids, getCategory, function(err, categories) {
2013-09-17 13:09:37 -04:00
if (err) {
winston.err(err);
2013-11-26 14:25:46 -05:00
return callback(err, null);
}
2013-08-20 12:11:17 -04:00
categories = categories.filter(function(category) {
2013-10-23 16:27:33 -04:00
return !!category;
}).sort(function(a, b) {
return parseInt(a.order, 10) - parseInt(b.order, 10);
});
2013-10-07 17:48:11 -04:00
2013-11-26 14:25:46 -05:00
callback(null, {
2013-09-17 13:09:37 -04:00
'categories': categories
});
});
2013-08-20 12:11:17 -04:00
};
2013-08-27 13:32:43 -04:00
Categories.isUserActiveIn = function(cid, uid, callback) {
2013-12-02 21:20:55 -05:00
db.getListRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
2013-09-17 15:54:08 -04:00
if (err) {
2013-08-27 13:32:43 -04:00
return callback(err, null);
2013-09-17 15:54:08 -04:00
}
2013-08-27 13:32:43 -04:00
var index = 0,
active = false;
async.whilst(
function() {
return active === false && index < pids.length;
},
function(callback) {
2013-11-15 14:57:50 -05:00
posts.getCidByPid(pids[index], function(err, postCid) {
2013-09-17 15:54:08 -04:00
if (err) {
2013-08-27 13:32:43 -04:00
return callback(err);
2013-09-17 15:54:08 -04:00
}
if (postCid === cid) {
2013-08-27 13:32:43 -04:00
active = true;
2013-09-17 15:54:08 -04:00
}
2013-08-27 13:32:43 -04:00
++index;
callback(null);
2013-09-12 14:07:24 -04:00
});
2013-08-27 13:32:43 -04:00
},
function(err) {
2013-09-17 15:54:08 -04:00
if (err) {
2013-08-27 13:32:43 -04:00
return callback(err, null);
2013-09-17 15:54:08 -04:00
}
2013-08-27 13:32:43 -04:00
callback(null, active);
}
);
});
2013-09-12 14:07:24 -04:00
};
2013-08-27 13:32:43 -04:00
Categories.addActiveUser = function(cid, uid) {
2013-12-02 21:20:55 -05:00
if(parseInt(uid, 10)) {
db.setAdd('cid:' + cid + ':active_users', uid);
}
2013-09-12 14:07:24 -04:00
};
2013-08-27 13:32:43 -04:00
Categories.removeActiveUser = function(cid, uid) {
2013-12-02 21:20:55 -05:00
db.setRemove('cid:' + cid + ':active_users', uid);
2013-09-12 14:07:24 -04:00
};
2013-12-29 23:33:28 -05:00
Categories.onNewPostMade = function(uid, tid, pid, timestamp) {
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {
var cid = topicData.cid;
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
if(parseInt(topicData.pinned, 10) === 0) {
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
}
db.setCount('cid:' + cid + ':active_users', function(err, amount) {
if (amount > 15) {
db.setRemoveRandom('cid:' + cid + ':active_users');
}
Categories.addActiveUser(cid, uid);
});
});
}
2013-09-12 14:07:24 -04:00
}(exports));