2015-09-25 17:38:58 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2020-06-24 16:02:57 -04:00
|
|
|
const validator = require('validator');
|
2015-09-25 17:38:58 -04:00
|
|
|
|
2020-06-24 16:02:57 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const meta = require('../meta');
|
2015-09-25 17:38:58 -04:00
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
const intFields = [
|
|
|
|
|
'cid', 'parentCid', 'disabled', 'isSection', 'order',
|
2018-11-25 19:08:38 -05:00
|
|
|
'topic_count', 'post_count', 'numRecentReplies',
|
2020-06-24 16:02:57 -04:00
|
|
|
'minTags', 'maxTags',
|
2018-10-21 16:47:51 -04:00
|
|
|
];
|
2015-09-25 17:38:58 -04:00
|
|
|
|
2018-10-20 17:07:32 -04:00
|
|
|
module.exports = function (Categories) {
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoriesFields = async function (cids, fields) {
|
2015-09-25 17:38:58 -04:00
|
|
|
if (!Array.isArray(cids) || !cids.length) {
|
2019-07-16 00:41:42 -04:00
|
|
|
return [];
|
2015-09-25 17:38:58 -04:00
|
|
|
}
|
2020-06-19 11:51:41 -04:00
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
const keys = cids.map(cid => 'category:' + cid);
|
2020-06-19 11:51:41 -04:00
|
|
|
const categories = await (fields.length ? db.getObjectsFields(keys, fields) : db.getObjects(keys));
|
2019-07-16 00:41:42 -04:00
|
|
|
categories.forEach(category => modifyCategory(category, fields));
|
|
|
|
|
return categories;
|
2015-09-25 17:38:58 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoryData = async function (cid) {
|
|
|
|
|
const categories = await Categories.getCategoriesFields([cid], []);
|
|
|
|
|
return categories && categories.length ? categories[0] : null;
|
2018-10-20 17:07:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoriesData = async function (cids) {
|
|
|
|
|
return await Categories.getCategoriesFields(cids, []);
|
2018-10-20 17:07:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoryField = async function (cid, field) {
|
|
|
|
|
const category = await Categories.getCategoryFields(cid, [field]);
|
|
|
|
|
return category ? category[field] : null;
|
2018-10-20 17:07:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoryFields = async function (cid, fields) {
|
|
|
|
|
const categories = await Categories.getCategoriesFields([cid], fields);
|
|
|
|
|
return categories ? categories[0] : null;
|
2018-10-20 17:07:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getAllCategoryFields = async function (fields) {
|
|
|
|
|
const cids = await Categories.getAllCidsFromSet('categories:cid');
|
|
|
|
|
return await Categories.getCategoriesFields(cids, fields);
|
2015-09-25 17:38:58 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.setCategoryField = async function (cid, field, value) {
|
|
|
|
|
await db.setObjectField('category:' + cid, field, value);
|
2015-09-25 17:38:58 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.incrementCategoryFieldBy = async function (cid, field, value) {
|
|
|
|
|
await db.incrObjectFieldBy('category:' + cid, field, value);
|
2015-09-25 17:38:58 -04:00
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|
2018-10-20 17:07:32 -04:00
|
|
|
|
2018-10-25 19:58:01 -04:00
|
|
|
function modifyCategory(category, fields) {
|
2018-10-20 17:07:32 -04:00
|
|
|
if (!category) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 16:02:57 -04:00
|
|
|
if (!fields.length || fields.includes('minTags')) {
|
|
|
|
|
const useDefault = !category.hasOwnProperty('minTags') ||
|
|
|
|
|
category.minTags === null ||
|
|
|
|
|
category.minTags === '' ||
|
|
|
|
|
!parseInt(category.minTags, 10);
|
|
|
|
|
category.minTags = useDefault ? meta.config.minimumTagsPerTopic : category.minTags;
|
|
|
|
|
}
|
|
|
|
|
if (!fields.length || fields.includes('maxTags')) {
|
|
|
|
|
const useDefault = !category.hasOwnProperty('maxTags') ||
|
|
|
|
|
category.maxTags === null ||
|
|
|
|
|
category.maxTags === '' ||
|
|
|
|
|
!parseInt(category.maxTags, 10);
|
|
|
|
|
category.maxTags = useDefault ? meta.config.maximumTagsPerTopic : category.maxTags;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:58:01 -04:00
|
|
|
db.parseIntFields(category, intFields, fields);
|
2018-10-20 17:07:32 -04:00
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('name')) {
|
|
|
|
|
category.name = validator.escape(String(category.name || ''));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('icon')) {
|
|
|
|
|
category.icon = category.icon || 'hidden';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('post_count')) {
|
|
|
|
|
category.totalPostCount = category.post_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('topic_count')) {
|
|
|
|
|
category.totalTopicCount = category.topic_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.description) {
|
|
|
|
|
category.description = validator.escape(String(category.description));
|
|
|
|
|
category.descriptionParsed = category.descriptionParsed || category.description;
|
|
|
|
|
}
|
|
|
|
|
}
|