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

442 lines
10 KiB
JavaScript

11 years ago
'use strict';
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) {
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) {
CategoryTools.exists(cid, function(err, exists) {
if(err || !exists) {
return callback(err || new Error('category-not-found [' + cid + ']'));
}
if(parseInt(uid, 10)) {
Categories.markAsRead(cid, uid);
11 years ago
}
async.parallel({
category: function(next) {
Categories.getCategoryData(cid, next);
},
topics: function(next) {
Categories.getCategoryTopics(cid, start, end, uid, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
}
}, 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';
callback(null, category);
});
});
};
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
11 years ago
var tids;
11 years ago
async.waterfall([
function(next) {
Categories.getTopicIds(cid, start, stop, next);
},
11 years ago
function(topicIds, next) {
tids = topicIds;
topics.getTopicsByTids(tids, uid, next);
11 years ago
},
function(topics, next) {
if (!topics || !topics.length) {
return next(null, {
topics: [],
nextStart: 1
11 years ago
});
}
var indices = {},
i = 0;
for(i=0; i<tids.length; ++i) {
11 years ago
indices[tids[i]] = start + i;
}
for(i=0; i<topics.length; ++i) {
11 years ago
topics[i].index = indices[topics[i].tid];
}
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);
};
11 years ago
Categories.getTopicIndex = function(tid, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
db.sortedSetRevRank('categories:' + cid + ':tid', tid, 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) {
11 years ago
return callback(err);
}
11 years ago
if (!Array.isArray(cids) || !cids.length) {
11 years ago
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, callback);
};
Categories.hasReadCategory = function(cid, uid, callback) {
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
};
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) {
Categories.getCategoriesData([cid], function(err, categories) {
callback(err, categories ? categories[0] : null);
});
};
12 years ago
Categories.getCategoriesData = function(cids, callback) {
var keys = cids.map(function(cid) {
return 'category:'+cid;
});
db.getObjects(keys, function(err, categories) {
if (err) {
return callback(err);
}
if (!Array.isArray(categories)) {
return callback(null, []);
}
for (var i=0; i<categories.length; ++i) {
if (categories[i]) {
categories[i].backgroundImage = categories[i].image ? 'url(' + nconf.get('relative_path') + categories[i].image + ')' : '';
categories[i].disabled = categories[i].disabled ? parseInt(categories[i].disabled, 10) !== 0 : false;
}
}
callback(null, categories);
});
11 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) {
12 years ago
if (!Array.isArray(cids) || cids.length === 0) {
return callback(new Error('invalid-cids'));
}
12 years ago
async.parallel({
categories: function(next) {
Categories.getCategoriesData(cids, next);
},
hasRead: function(next) {
Categories.hasReadCategories(cids, uid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
12 years ago
var categories = results.categories;
var hasRead = results.hasRead;
uid = parseInt(uid, 10);
for(var i=0; i<results.categories.length; ++i) {
categories[i]['unread-class'] = (parseInt(categories[i].topic_count, 10) === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
}
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));