You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/categories.js

439 lines
11 KiB
JavaScript

11 years ago
var db = require('./database.js'),
posts = require('./posts.js'),
utils = require('./../public/src/utils.js'),
user = require('./user.js'),
topics = require('./topics.js'),
plugins = require('./plugins'),
11 years ago
CategoryTools = require('./categoryTools'),
async = require('async'),
winston = require('winston'),
nconf = require('nconf');
(function(Categories) {
"use strict";
12 years ago
Categories.create = function(data, callback) {
11 years ago
db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) {
12 years ago
return callback(err, null);
}
12 years ago
var slug = cid + '/' + utils.slugify(data.name);
11 years ago
db.listAppend('categories:cid', cid);
12 years ago
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
bgColor: data.bgColor,
color: data.color,
12 years ago
slug: slug,
topic_count: 0,
11 years ago
disabled: 0,
order: data.order,
link: "",
numRecentReplies: 2,
class: 'col-md-3 col-xs-6',
imageClass: 'default'
12 years ago
};
11 years ago
db.setObject('category:' + cid, category, function(err, data) {
callback(err, category);
});
12 years ago
});
};
12 years ago
Categories.getCategoryById = function(category_id, current_user, callback) {
Categories.getCategoryData(category_id, function(err, categoryData) {
if (err) {
return callback(err);
}
function getTopicIds(next) {
Categories.getTopicIds(category_id, 0, 19, next);
}
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) {
var tids = results[0],
active_users = results[1],
sidebars = results[2];
var category = {
'category_name': categoryData.name,
'category_description': categoryData.description,
'link': categoryData.link,
'disabled': categoryData.disabled || '0',
'show_sidebar': 'show',
12 years ago
'show_topic_button': 'inline-block',
'no_topics_message': 'hidden',
'topic_row_size': 'col-md-9',
'category_id': category_id,
'active_users': [],
'topics': [],
11 years ago
'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false,
'sidebars': sidebars
};
function getTopics(next) {
topics.getTopicsByTids(tids, category_id, current_user, next);
}
12 years ago
function getModerators(next) {
Categories.getModerators(category_id, next);
}
function getActiveUsers(next) {
11 years ago
user.getMultipleUserFields(active_users, ['uid', 'username', 'userslug', 'picture'], function(err, users) {
next(err, users);
});
}
if (tids.length === 0) {
getModerators(function(err, moderators) {
category.moderator_block_class = moderators.length > 0 ? '' : 'none';
category.moderators = moderators;
category.show_sidebar = 'hidden';
category.no_topics_message = 'show';
callback(null, category);
});
} else {
async.parallel([getTopics, getModerators, getActiveUsers], function(err, results) {
category.topics = results[0];
category.moderator_block_class = results[1].length > 0 ? '' : 'none';
category.moderators = results[1];
category.active_users = results[2];
category.show_sidebar = category.topics.length > 0 ? 'show' : 'hidden';
callback(null, category);
});
}
});
});
};
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
Categories.getTopicIds(cid, start, stop, function(err, tids) {
if(err) {
return callback(err);
}
topics.getTopicsByTids(tids, cid, uid, callback);
});
};
Categories.getTopicIds = function(cid, start, stop, callback) {
11 years ago
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
};
Categories.getAllCategories = function(current_user, callback) {
11 years ago
db.getListRange('categories:cid', 0, -1, function(err, cids) {
11 years ago
if(err) {
return callback(err);
}
if(cids && cids.length === 0) {
return callback(null, {categories : []});
}
11 years ago
Categories.getCategories(cids, current_user, callback);
});
};
Categories.getModerators = function(cid, callback) {
11 years ago
db.getSetMembers('cid:' + cid + ':moderators', function(err, mods) {
if (!err) {
if (mods && mods.length) {
user.getMultipleUserFields(mods, ['username'], function(err, moderators) {
callback(err, moderators);
});
} else {
callback(null, []);
}
} else {
callback(err, null);
}
});
};
Categories.isTopicsRead = function(cid, uid, callback) {
11 years ago
db.getSortedSetRange('categories:' + cid + ':tid', 0, -1, function(err, tids) {
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) {
11 years ago
db.setAdd('cid:' + cid + ':read_by_uid', uid);
};
Categories.markAsUnreadForAll = function(cid, callback) {
db.delete('cid:' + cid + ':read_by_uid', callback);
};
Categories.hasReadCategories = function(cids, uid, callback) {
var sets = [];
for (var i = 0, ii = cids.length; i < ii; i++) {
sets.push('cid:' + cids[i] + ':read_by_uid');
}
12 years ago
db.isMemberOfSets(sets, uid, function(err, hasRead) {
callback(hasRead);
});
};
Categories.hasReadCategory = function(cid, uid, callback) {
11 years ago
db.isSetMember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
if(err) {
return callback(false);
}
12 years ago
callback(hasRead);
12 years ago
});
};
11 years ago
Categories.getRecentReplies = function(cid, uid, count, callback) {
if(count === 0 || !count) {
return callback(null, []);
}
11 years ago
CategoryTools.privileges(cid, uid, function(err, privileges) {
if (privileges.read) {
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
12 years ago
11 years ago
if (err) {
winston.err(err);
return callback(err, []);
}
12 years ago
11 years ago
if (pids.length === 0) {
return callback(null, []);
}
12 years ago
11 years ago
posts.getPostSummaryByPids(pids, true, function(err, postData) {
if(err) {
return callback(err);
}
callback(null, postData);
});
});
} else {
callback(null, []);
}
});
};
12 years ago
Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
function movePost(pid, callback) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return callback(err);
}
11 years ago
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback(null);
});
}
12 years ago
topics.getPids(tid, function(err, pids) {
if(err) {
return callback(err, null);
12 years ago
}
async.each(pids, movePost, function(err) {
if(err) {
return callback(err, null);
}
callback(null, 1);
});
12 years ago
});
};
12 years ago
12 years ago
Categories.getCategoryData = function(cid, callback) {
11 years ago
db.exists('category:' + cid, function(err, exists) {
if (exists) {
db.getObject('category:' + cid, function(err, data) {
data.background = data.image ? 'url(' + data.image + ')' : data.bgColor;
callback(err, data);
});
} else {
callback(new Error('No category found!'));
}
});
};
12 years ago
Categories.getCategoryField = function(cid, field, callback) {
11 years ago
db.getObjectField('category:' + cid, field, callback);
};
Categories.getCategoryFields = function(cid, fields, callback) {
11 years ago
db.getObjectFields('category:' + cid, fields, callback);
};
12 years ago
11 years ago
Categories.setCategoryField = function(cid, field, value, callback) {
11 years ago
db.setObjectField('category:' + cid, field, value, callback);
};
11 years ago
Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
11 years ago
db.incrObjectFieldBy('category:' + cid, field, value, callback);
};
Categories.getCategories = function(cids, uid, callback) {
if (!cids || !Array.isArray(cids) || cids.length === 0) {
return callback(new Error('invalid-cids'), null);
}
12 years ago
function getCategory(cid, callback) {
Categories.getCategoryData(cid, function(err, categoryData) {
if (err) {
winston.warn('Attempted to retrieve cid ' + cid + ', but nothing was returned!');
return callback(err, null);
}
Categories.hasReadCategory(cid, uid, function(hasRead) {
categoryData.badgeclass = (parseInt(categoryData.topic_count, 10) === 0 || (hasRead && uid !== 0)) ? '' : 'badge-important';
callback(null, categoryData);
});
12 years ago
});
}
12 years ago
async.map(cids, getCategory, function(err, categories) {
if (err) {
winston.err(err);
return callback(err, null);
}
12 years ago
categories = categories.filter(function(category) {
return !!category;
}).sort(function(a, b) {
return parseInt(a.order, 10) - parseInt(b.order, 10);
});
11 years ago
callback(null, {
'categories': categories
});
});
12 years ago
};
12 years ago
Categories.isUserActiveIn = function(cid, uid, callback) {
db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
if (err) {
12 years ago
return callback(err, null);
}
12 years ago
var index = 0,
active = false;
async.whilst(
function() {
return active === false && index < pids.length;
},
function(callback) {
posts.getCidByPid(pids[index], function(err, postCid) {
if (err) {
12 years ago
return callback(err);
}
if (postCid === cid) {
12 years ago
active = true;
}
12 years ago
++index;
callback(null);
});
12 years ago
},
function(err) {
if (err) {
12 years ago
return callback(err, null);
}
12 years ago
callback(null, active);
}
);
});
};
12 years ago
Categories.addActiveUser = function(cid, uid, timestamp) {
11 years ago
if(parseInt(uid, 10)) {
db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
11 years ago
}
};
12 years ago
Categories.removeActiveUser = function(cid, uid) {
db.sortedSetRemove('cid:' + cid + ':active_users', uid);
};
Categories.getActiveUsers = function(cid, callback) {
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 15, callback);
};
Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
11 years ago
function updateUser(uid, timestamp) {
Categories.addActiveUser(cid, uid, timestamp);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
if (!err && !active) {
Categories.removeActiveUser(oldCid, uid);
}
});
}
11 years ago
topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
if(!err) {
topics.getUids(tid, function(err, uids) {
if (!err && uids) {
for (var i = 0; i < uids.length; ++i) {
updateUser(uids[i], timestamp);
}
}
});
}
});
};
11 years ago
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);
}
Categories.addActiveUser(cid, uid, timestamp);
11 years ago
});
}
}(exports));