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

412 lines
9.8 KiB
JavaScript

11 years ago
var db = require('./database'),
posts = require('./posts'),
utils = require('./../public/src/utils'),
user = require('./user'),
Groups = require('./groups'),
11 years ago
topics = require('./topics'),
plugins = require('./plugins'),
11 years ago
CategoryTools = require('./categoryTools'),
meta = require('./meta'),
11 years ago
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) {
return callback(err);
}
12 years ago
var slug = cid + '/' + utils.slugify(data.name);
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
bgColor: data.bgColor,
11 years ago
background: 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
};
db.setObject('category:' + cid, category, function(err) {
if(err) {
return callback(err);
}
db.sortedSetAdd('categories:cid', data.order, cid);
callback(null, category);
});
12 years ago
});
};
12 years ago
11 years ago
Categories.getCategoryById = function(cid, start, end, uid, callback) {
if(parseInt(uid, 10)) {
Categories.markAsRead(cid, uid);
}
11 years ago
async.parallel({
category: function(next) {
Categories.getCategoryData(cid, next);
},
11 years ago
topics: function(next) {
Categories.getCategoryTopics(cid, start, end, uid, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
}
11 years ago
}, function(err, results) {
if(err) {
return callback(err);
}
var category = results.category;
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.pageCount = results.pageCount;
category.topic_row_size = 'col-md-9';
11 years ago
callback(null, category);
});
};
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
11 years ago
async.waterfall([
function(next) {
Categories.getTopicIds(cid, start, stop, next);
},
function(tids, next) {
topics.getTopicsByTids(tids, uid, next);
11 years ago
},
function(topics, next) {
if (!topics || !topics.length) {
return next(null, {
topics: [],
nextStart: 1
11 years ago
});
}
db.sortedSetRevRank('categories:' + cid + ':tid', topics[topics.length - 1].tid, function(err, rank) {
if(err) {
return next(err);
}
11 years ago
next(null, {
11 years ago
topics: topics,
nextStart: parseInt(rank, 10) + 1
11 years ago
});
});
}
11 years ago
], callback);
};
Categories.getTopicIds = function(cid, start, stop, callback) {
11 years ago
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
};
Categories.getPageCount = function(cid, uid, callback) {
db.sortedSetCard('categories:' + cid + ':tid', function(err, topicCount) {
if(err) {
return callback(err);
}
if (parseInt(topicCount, 10) === 0) {
return callback(null, 1);
}
user.getSettings(uid, function(err, settings) {
if(err) {
return callback(err);
}
callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage));
});
});
};
Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
11 years ago
if(cids && cids.length === 0) {
return callback(null, {categories : []});
}
11 years ago
Categories.getCategories(cids, uid, callback);
});
};
Categories.getModerators = function(cid, callback) {
11 years ago
Groups.getByGroupName('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
if (!err) {
if (groupObj.members && groupObj.members.length) {
user.getMultipleUserFields(groupObj.members, ['uid', 'username', 'userslug', 'picture'], function(err, moderators) {
callback(err, moderators);
});
} else {
callback(null, []);
}
} else {
// Probably no mods
callback(null, []);
}
});
};
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', function(err) {
if(typeof callback === 'function') {
callback(err);
}
});
};
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) {
11 years ago
if(err) {
return callback(err);
}
12 years ago
11 years ago
if (!privileges.read) {
return callback(null, []);
}
12 years ago
11 years ago
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err) {
return callback(err, []);
}
12 years ago
11 years ago
if (!pids || !pids.length) {
return callback(null, []);
}
posts.getPostSummaryByPids(pids, true, callback);
});
});
};
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();
});
}
12 years ago
topics.getPids(tid, function(err, pids) {
if(err) {
return callback(err);
12 years ago
}
async.each(pids, movePost, callback);
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;
11 years ago
data.disabled = data.disabled ? parseInt(data.disabled, 10) !== 0 : false;
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) {
11 years ago
return callback(new Error('invalid-cids'));
}
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!');
11 years ago
return callback(err);
}
Categories.hasReadCategory(cid, uid, function(hasRead) {
categoryData['unread-class'] = (parseInt(categoryData.topic_count, 10) === 0 || (hasRead && parseInt(uid, 10) !== 0)) ? '' : 'unread';
callback(null, categoryData);
});
12 years ago
});
}
12 years ago
async.map(cids, getCategory, function(err, categories) {
if (err) {
return callback(err);
}
12 years ago
categories = categories.filter(function(category) {
return !!category;
});
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) {
11 years ago
return callback(err);
}
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;
11 years ago
callback();
});
12 years ago
},
function(err) {
11 years ago
callback(err, active);
12 years ago
}
);
});
};
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) {
11 years ago
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, 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
});
11 years ago
};
11 years ago
}(exports));