Files
NodeBB/src/categories.js

412 lines
9.8 KiB
JavaScript
Raw Normal View History

2014-01-26 17:17:34 -05:00
var db = require('./database'),
posts = require('./posts'),
utils = require('./../public/src/utils'),
user = require('./user'),
2014-02-07 11:21:23 -05:00
Groups = require('./groups'),
2014-01-26 17:17:34 -05:00
topics = require('./topics'),
plugins = require('./plugins'),
2014-01-08 21:50:19 -05:00
CategoryTools = require('./categoryTools'),
meta = require('./meta'),
2014-01-08 21:50:19 -05:00
async = require('async'),
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) {
return callback(err);
2013-09-17 15:54:08 -04:00
}
2013-09-11 16:04:34 -04:00
var slug = cid + '/' + utils.slugify(data.name);
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
2013-12-08 14:20:57 -05:00
bgColor: data.bgColor,
2014-02-19 15:33:59 -05:00
background: 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,
link: '',
numRecentReplies: 2,
2014-01-22 11:46:50 -05:00
class: 'col-md-3 col-xs-6',
imageClass: 'default'
2013-09-11 16:04:34 -04:00
};
db.setObject('category:' + cid, category, function(err) {
if(err) {
return callback(err);
}
db.sortedSetAdd('categories:cid', data.order, cid);
callback(null, 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
2014-02-21 18:31:59 -05:00
Categories.getCategoryById = function(cid, start, end, uid, callback) {
if(parseInt(uid, 10)) {
Categories.markAsRead(cid, uid);
}
2014-02-07 20:30:10 -05:00
async.parallel({
2014-02-26 16:43:21 -05:00
category: function(next) {
Categories.getCategoryData(cid, next);
},
2014-02-26 16:58:02 -05:00
topics: function(next) {
2014-02-26 16:43:21 -05:00
Categories.getCategoryTopics(cid, start, end, uid, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
}
2014-02-07 20:30:10 -05:00
}, function(err, results) {
if(err) {
return callback(err);
}
2013-07-19 16:13:00 -04:00
2014-02-26 16:43:21 -05:00
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';
2013-05-24 07:52:15 -04:00
2014-02-07 20:30:10 -05:00
callback(null, category);
});
2013-09-12 14:07:24 -04:00
};
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
2014-01-26 17:17:34 -05:00
async.waterfall([
function(next) {
Categories.getTopicIds(cid, start, stop, next);
},
function(tids, next) {
topics.getTopicsByTids(tids, cid, uid, next);
},
function(topics, next) {
2014-01-27 09:09:55 -05:00
if (topics && topics.length > 0) {
db.sortedSetRevRank('categories:' + cid + ':tid', topics[topics.length - 1].tid, function(err, rank) {
if(err) {
return next(err);
}
next(null, {
topics: topics,
nextStart: parseInt(rank, 10) + 1
});
});
} else {
next(null, {
2014-01-26 17:17:34 -05:00
topics: topics,
2014-01-27 09:09:55 -05:00
nextStart: 1
2014-01-26 17:17:34 -05:00
});
2014-01-27 09:09:55 -05:00
}
2014-01-17 12:49:21 -05:00
}
2014-01-26 17:17:34 -05:00
], callback);
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
2014-02-10 14:15:54 -05:00
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);
}
2014-02-10 14:15:54 -05:00
user.getSettings(uid, function(err, settings) {
if(err) {
return callback(err);
}
2014-02-10 14:15:54 -05:00
callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage));
});
});
};
Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
2013-11-26 19:09:32 -05:00
if(err) {
return callback(err);
}
2014-02-19 15:33:59 -05:00
2013-11-26 19:09:32 -05:00
if(cids && cids.length === 0) {
return callback(null, {categories : []});
}
2013-11-27 15:02:09 -05:00
Categories.getCategories(cids, uid, callback);
});
2013-09-12 14:07:24 -04:00
};
2013-05-16 12:49:39 -04:00
Categories.getModerators = function(cid, callback) {
2014-02-14 20:46:00 -05:00
Groups.getByGroupName('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
2013-09-17 13:09:37 -04:00
if (!err) {
2014-02-07 11:21:23 -05:00
if (groupObj.members && groupObj.members.length) {
user.getMultipleUserFields(groupObj.members, ['uid', 'username', 'userslug', 'picture'], function(err, moderators) {
2013-09-11 12:49:54 -04:00
callback(err, moderators);
2013-07-19 16:13:00 -04:00
});
} else {
callback(null, []);
}
} else {
// Probably no mods
callback(null, []);
2013-07-19 16:13:00 -04:00
}
2013-05-16 12:49:39 -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.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) {
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
2014-01-08 21:50:19 -05:00
Categories.getRecentReplies = function(cid, uid, count, callback) {
if(count === 0 || !count) {
return callback(null, []);
}
2014-01-08 21:50:19 -05:00
CategoryTools.privileges(cid, uid, function(err, privileges) {
2014-02-07 12:09:24 -05:00
if(err) {
return callback(err);
}
2013-08-20 12:11:17 -04:00
2014-02-07 12:09:24 -05:00
if (!privileges.read) {
return callback(null, []);
}
2013-07-02 19:46:58 -04:00
2014-02-07 12:09:24 -05:00
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err) {
return callback(err, []);
}
2013-07-15 14:34:15 -04:00
2014-02-07 12:09:24 -05:00
if (!pids || !pids.length) {
return callback(null, []);
}
posts.getPostSummaryByPids(pids, true, callback);
});
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);
2014-02-07 11:45:13 -05:00
callback();
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) {
2014-02-07 11:45:13 -05:00
return callback(err);
2013-07-22 12:07:05 -04:00
}
2013-11-15 14:57:50 -05:00
2014-02-07 11:45:13 -05:00
async.each(pids, movePost, callback);
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-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;
2014-02-19 15:33:59 -05:00
data.disabled = data.disabled ? parseInt(data.disabled, 10) !== 0 : false;
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) {
2014-02-26 16:58:02 -05:00
return callback(new Error('invalid-cids'));
}
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!');
2014-02-26 16:58:02 -05:00
return callback(err);
}
2013-11-26 14:25:46 -05:00
Categories.hasReadCategory(cid, uid, function(hasRead) {
2014-02-11 18:58:46 -05:00
categoryData['unread-class'] = (parseInt(categoryData.topic_count, 10) === 0 || (hasRead && parseInt(uid, 10) !== 0)) ? '' : 'unread';
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) {
return callback(err);
}
2013-08-20 12:11:17 -04:00
categories = categories.filter(function(category) {
2013-10-23 16:27:33 -04:00
return !!category;
});
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) {
2014-01-07 17:30:29 -05:00
db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
2013-09-17 15:54:08 -04:00
if (err) {
2014-02-26 16:58:02 -05:00
return callback(err);
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;
2014-02-26 16:58:02 -05:00
callback();
2013-09-12 14:07:24 -04:00
});
2013-08-27 13:32:43 -04:00
},
function(err) {
2014-02-26 16:58:02 -05:00
callback(err, active);
2013-08-27 13:32:43 -04:00
}
);
});
2013-09-12 14:07:24 -04:00
};
2013-08-27 13:32:43 -04:00
Categories.addActiveUser = function(cid, uid, timestamp) {
2013-12-02 21:20:55 -05:00
if(parseInt(uid, 10)) {
db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
2013-12-02 21:20:55 -05:00
}
2013-09-12 14:07:24 -04:00
};
2013-08-27 13:32:43 -04:00
Categories.removeActiveUser = function(cid, uid) {
db.sortedSetRemove('cid:' + cid + ':active_users', uid);
};
Categories.getActiveUsers = function(cid, callback) {
2014-02-12 17:29:37 -05:00
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
};
Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
2014-01-14 13:10:31 -05:00
function updateUser(uid, timestamp) {
Categories.addActiveUser(cid, uid, timestamp);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
if (!err && !active) {
Categories.removeActiveUser(oldCid, uid);
}
});
}
2014-01-14 13:10:31 -05:00
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);
}
}
});
}
});
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);
}
Categories.addActiveUser(cid, uid, timestamp);
2013-12-29 23:33:28 -05:00
});
2014-02-26 15:32:32 -05:00
};
2013-12-29 23:33:28 -05:00
2013-09-12 14:07:24 -04:00
}(exports));