2014-03-01 16:59:04 -05:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
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'),
|
2013-10-15 15:01:12 -04:00
|
|
|
plugins = require('./plugins'),
|
2014-01-08 21:50:19 -05:00
|
|
|
CategoryTools = require('./categoryTools'),
|
2014-01-25 01:34:56 -05:00
|
|
|
meta = require('./meta'),
|
2014-03-17 17:27:42 -04:00
|
|
|
emitter = require('./emitter'),
|
2014-03-25 12:23:55 -04:00
|
|
|
validator = require('validator'),
|
2014-01-08 21:50:19 -05:00
|
|
|
|
|
|
|
|
async = require('async'),
|
2013-09-17 15:38:50 -04:00
|
|
|
winston = require('winston'),
|
|
|
|
|
nconf = require('nconf');
|
2013-05-09 01:04:15 -04:00
|
|
|
|
|
|
|
|
(function(Categories) {
|
|
|
|
|
|
2014-03-12 21:41:53 -04:00
|
|
|
require('./categories/activeusers')(Categories);
|
|
|
|
|
require('./categories/recentreplies')(Categories);
|
2014-04-01 17:12:03 -04:00
|
|
|
require('./categories/update')(Categories);
|
2014-03-12 21:41:53 -04:00
|
|
|
|
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) {
|
2014-02-22 18:56:37 -05:00
|
|
|
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,
|
2013-11-26 15:14:12 -05:00
|
|
|
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,
|
2014-01-09 11:50:24 -05:00
|
|
|
order: data.order,
|
2014-02-22 18:56:37 -05:00
|
|
|
link: '',
|
2014-01-09 11:50:24 -05:00
|
|
|
numRecentReplies: 2,
|
2014-01-22 11:46:50 -05:00
|
|
|
class: 'col-md-3 col-xs-6',
|
2014-03-13 18:11:37 -04:00
|
|
|
imageClass: 'auto'
|
2013-09-11 16:04:34 -04:00
|
|
|
};
|
|
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
db.setObject('category:' + cid, category, function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.sortedSetAdd('categories:cid', data.order, cid);
|
|
|
|
|
|
|
|
|
|
callback(null, category);
|
2013-10-15 15:20:12 -04:00
|
|
|
});
|
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) {
|
2014-03-13 16:21:10 -04:00
|
|
|
Categories.getCategoryData(cid, function(err, category) {
|
|
|
|
|
if(err || !category) {
|
2014-04-09 21:56:30 -04:00
|
|
|
return callback(err || new Error('[[error:invalid-cid]]'));
|
2014-02-26 16:43:21 -05:00
|
|
|
}
|
2014-03-12 20:53:42 -04:00
|
|
|
|
|
|
|
|
if(parseInt(uid, 10)) {
|
|
|
|
|
Categories.markAsRead(cid, uid);
|
2014-02-07 20:30:10 -05:00
|
|
|
}
|
2013-07-19 16:13:00 -04:00
|
|
|
|
2014-03-12 20:53:42 -04:00
|
|
|
async.parallel({
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-03-12 20:53:42 -04:00
|
|
|
callback(null, category);
|
|
|
|
|
});
|
2013-05-23 16:53:19 -04:00
|
|
|
});
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-05-23 16:53:19 -04:00
|
|
|
|
2013-08-09 20:03:19 -04:00
|
|
|
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
|
2014-02-27 23:45:12 -05:00
|
|
|
var tids;
|
2014-01-26 17:17:34 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
Categories.getTopicIds(cid, start, stop, next);
|
|
|
|
|
},
|
2014-02-27 23:45:12 -05:00
|
|
|
function(topicIds, next) {
|
|
|
|
|
tids = topicIds;
|
2014-02-26 19:55:28 -05:00
|
|
|
topics.getTopicsByTids(tids, uid, next);
|
2014-01-26 17:17:34 -05:00
|
|
|
},
|
|
|
|
|
function(topics, next) {
|
2014-02-26 17:34:02 -05:00
|
|
|
if (!topics || !topics.length) {
|
|
|
|
|
return next(null, {
|
|
|
|
|
topics: [],
|
|
|
|
|
nextStart: 1
|
2014-01-27 09:09:55 -05:00
|
|
|
});
|
2014-02-26 17:34:02 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 17:36:29 -05:00
|
|
|
var indices = {},
|
|
|
|
|
i = 0;
|
|
|
|
|
for(i=0; i<tids.length; ++i) {
|
2014-02-27 23:45:12 -05:00
|
|
|
indices[tids[i]] = start + i;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 17:36:29 -05:00
|
|
|
for(i=0; i<topics.length; ++i) {
|
2014-02-27 23:45:12 -05:00
|
|
|
topics[i].index = indices[topics[i].tid];
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 17:34:02 -05:00
|
|
|
db.sortedSetRevRank('categories:' + cid + ':tid', topics[topics.length - 1].tid, function(err, rank) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-27 09:09:55 -05:00
|
|
|
next(null, {
|
2014-01-26 17:17:34 -05:00
|
|
|
topics: topics,
|
2014-02-26 17:34:02 -05:00
|
|
|
nextStart: parseInt(rank, 10) + 1
|
2014-01-26 17:17:34 -05:00
|
|
|
});
|
2014-02-26 17:34:02 -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
|
|
|
};
|
2013-08-09 20:03:19 -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-27 23:45:12 -05:00
|
|
|
Categories.getTopicIndex = function(tid, callback) {
|
|
|
|
|
topics.getTopicField(tid, 'cid', function(err, cid) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.sortedSetRevRank('categories:' + cid + ':tid', tid, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-10 14:15:54 -05:00
|
|
|
Categories.getPageCount = function(cid, uid, callback) {
|
2014-01-24 22:26:11 -05:00
|
|
|
db.sortedSetCard('categories:' + cid + ':tid', function(err, topicCount) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
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-01-24 22:26:11 -05:00
|
|
|
|
2014-02-10 14:15:54 -05:00
|
|
|
callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage));
|
|
|
|
|
});
|
2014-01-24 22:26:11 -05:00
|
|
|
});
|
|
|
|
|
};
|
2014-01-04 01:15:41 -05:00
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
Categories.getAllCategories = function(uid, callback) {
|
|
|
|
|
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
|
2014-03-01 16:59:04 -05:00
|
|
|
if (err) {
|
2013-11-26 19:09:32 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-02-19 15:33:59 -05:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
if (!Array.isArray(cids) || !cids.length) {
|
2013-11-26 19:09:32 -05:00
|
|
|
return callback(null, {categories : []});
|
|
|
|
|
}
|
2013-11-27 15:02:09 -05:00
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
Categories.getCategories(cids, uid, callback);
|
2013-05-09 01:04:15 -04:00
|
|
|
});
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-05-09 01:04:15 -04:00
|
|
|
|
2013-05-16 12:49:39 -04:00
|
|
|
Categories.getModerators = function(cid, callback) {
|
2014-03-20 09:12:49 -04:00
|
|
|
Groups.get('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 {
|
2014-02-07 12:10:46 -05:00
|
|
|
// 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
|
|
|
|
2014-03-21 17:48:32 -04:00
|
|
|
Categories.markAsRead = function(cid, uid, callback) {
|
|
|
|
|
db.setAdd('cid:' + cid + ':read_by_uid', uid, callback);
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-05-24 11:18:28 -04:00
|
|
|
|
2014-01-23 15:46:39 -05:00
|
|
|
Categories.markAsUnreadForAll = function(cid, callback) {
|
2014-02-18 15:14:33 -05:00
|
|
|
db.delete('cid:' + cid + ':read_by_uid', function(err) {
|
|
|
|
|
if(typeof callback === 'function') {
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-01-23 15:46:39 -05:00
|
|
|
};
|
|
|
|
|
|
2013-05-21 18:12:39 -04:00
|
|
|
Categories.hasReadCategories = function(cids, uid, callback) {
|
2013-12-04 15:13:43 -05:00
|
|
|
|
|
|
|
|
var sets = [];
|
2013-05-21 18:12:39 -04:00
|
|
|
|
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-05-21 18:12:39 -04:00
|
|
|
}
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
db.isMemberOfSets(sets, uid, callback);
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-05-21 18:12:39 -04:00
|
|
|
|
2013-07-03 12:59:45 -04:00
|
|
|
Categories.hasReadCategory = function(cid, uid, callback) {
|
2014-03-11 14:43:08 -04:00
|
|
|
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-07-03 12:59:45 -04:00
|
|
|
|
|
|
|
|
Categories.getCategoryData = function(cid, callback) {
|
2014-03-09 23:09:08 -04:00
|
|
|
Categories.getCategoriesData([cid], function(err, categories) {
|
|
|
|
|
callback(err, categories ? categories[0] : null);
|
2013-07-22 20:29:51 -04:00
|
|
|
});
|
2013-09-12 14:07:24 -04:00
|
|
|
};
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
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]) {
|
2014-03-25 12:23:55 -04:00
|
|
|
categories[i].name = validator.escape(categories[i].name);
|
|
|
|
|
categories[i].description = validator.escape(categories[i].description);
|
2014-03-23 18:53:21 -04:00
|
|
|
categories[i].backgroundImage = categories[i].image ? nconf.get('relative_path') + categories[i].image : '';
|
2014-03-09 23:09:08 -04:00
|
|
|
categories[i].disabled = categories[i].disabled ? parseInt(categories[i].disabled, 10) !== 0 : false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
callback(null, categories);
|
|
|
|
|
});
|
2014-03-10 00:17:06 -04:00
|
|
|
};
|
2014-03-09 23:09:08 -04:00
|
|
|
|
2013-08-11 16:12:20 -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
|
|
|
};
|
2013-08-11 16:12:20 -04:00
|
|
|
|
2013-07-22 16:47:41 -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-07-04 13:29:29 -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) {
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
if (!Array.isArray(cids) || cids.length === 0) {
|
2014-04-09 21:56:30 -04:00
|
|
|
return callback(new Error('[[error:invalid-cid]]'));
|
2013-07-22 16:57:18 -04:00
|
|
|
}
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
async.parallel({
|
|
|
|
|
categories: function(next) {
|
|
|
|
|
Categories.getCategoriesData(cids, next);
|
|
|
|
|
},
|
|
|
|
|
hasRead: function(next) {
|
|
|
|
|
Categories.hasReadCategories(cids, uid, next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
2013-09-17 13:09:37 -04:00
|
|
|
if (err) {
|
2014-02-22 18:56:37 -05:00
|
|
|
return callback(err);
|
2013-07-22 16:57:18 -04:00
|
|
|
}
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
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';
|
|
|
|
|
}
|
2013-10-07 17:48:11 -04:00
|
|
|
|
2014-03-09 23:09:08 -04:00
|
|
|
callback(null, {categories: categories});
|
2013-07-22 16:57:18 -04:00
|
|
|
});
|
2013-08-20 12:11:17 -04:00
|
|
|
};
|
2013-07-22 16:57:18 -04:00
|
|
|
|
2014-03-17 17:27:42 -04:00
|
|
|
Categories.onNewPostMade = function(postData) {
|
|
|
|
|
topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err.message);
|
|
|
|
|
}
|
2013-12-29 23:33:28 -05:00
|
|
|
|
|
|
|
|
var cid = topicData.cid;
|
|
|
|
|
|
2014-03-17 17:27:42 -04:00
|
|
|
db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid);
|
2013-12-29 23:33:28 -05:00
|
|
|
|
|
|
|
|
if(parseInt(topicData.pinned, 10) === 0) {
|
2014-03-17 17:27:42 -04:00
|
|
|
db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid);
|
2013-12-29 23:33:28 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-17 17:27:42 -04:00
|
|
|
Categories.addActiveUser(cid, postData.uid, postData.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
|
|
|
|
2014-03-17 17:38:47 -04:00
|
|
|
emitter.on('event:newpost', Categories.onNewPostMade);
|
2014-03-17 17:27:42 -04:00
|
|
|
|
2013-09-12 14:07:24 -04:00
|
|
|
}(exports));
|