mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
refactor: returned fields
This commit is contained in:
@@ -320,19 +320,23 @@ Categories.getTree = function (categories, parentCid) {
|
||||
return tree;
|
||||
};
|
||||
|
||||
Categories.buildForSelect = async function (uid, privilege) {
|
||||
let categories = await Categories.getCategoriesByPrivilege('categories:cid', uid, privilege);
|
||||
categories = Categories.getTree(categories);
|
||||
return Categories.buildForSelectCategories(categories);
|
||||
Categories.buildForSelect = async function (uid, privilege, fields) {
|
||||
const cids = await Categories.getCidsByPrivilege('categories:cid', uid, privilege);
|
||||
return await getSelectData(cids, fields);
|
||||
};
|
||||
|
||||
Categories.buildForSelectAll = async function (uid) {
|
||||
const categoryData = await Categories.getAllCategories(uid);
|
||||
Categories.buildForSelectAll = async function (fields) {
|
||||
const cids = await Categories.getAllCidsFromSet('categories:cid');
|
||||
return await getSelectData(cids, fields);
|
||||
};
|
||||
|
||||
async function getSelectData(cids, fields) {
|
||||
const categoryData = await Categories.getCategoriesData(cids);
|
||||
const tree = Categories.getTree(categoryData);
|
||||
return Categories.buildForSelectCategories(tree);
|
||||
};
|
||||
return Categories.buildForSelectCategories(tree, fields);
|
||||
}
|
||||
|
||||
Categories.buildForSelectCategories = function (categories) {
|
||||
Categories.buildForSelectCategories = function (categories, fields) {
|
||||
function recursive(category, categoriesData, level, depth) {
|
||||
const bullet = level ? '• ' : '';
|
||||
category.value = category.cid;
|
||||
@@ -347,14 +351,21 @@ Categories.buildForSelectCategories = function (categories) {
|
||||
|
||||
const categoriesData = [];
|
||||
|
||||
categories = categories.filter(category => category && !category.parentCid);
|
||||
const rootCategories = categories.filter(category => category && !category.parentCid);
|
||||
|
||||
rootCategories.forEach(category => recursive(category, categoriesData, '', 0));
|
||||
|
||||
categories.forEach(category => recursive(category, categoriesData, '', 0));
|
||||
const pickFields = [
|
||||
'name', 'level', 'disabledClass', 'icon', 'value', 'text',
|
||||
'cid', 'parentCid', 'color', 'bgColor', 'backgroundImage', 'imageClass',
|
||||
'disabled', 'depth',
|
||||
'cid', 'name', 'level', 'icon', 'parentCid',
|
||||
'color', 'bgColor', 'backgroundImage', 'imageClass',
|
||||
];
|
||||
fields = fields || [];
|
||||
if (fields.includes('text') && fields.includes('value')) {
|
||||
return categoriesData.map(category => _.pick(category, fields));
|
||||
}
|
||||
if (fields.length) {
|
||||
pickFields.push(...fields);
|
||||
}
|
||||
|
||||
return categoriesData.map(category => _.pick(category, pickFields));
|
||||
};
|
||||
|
||||
@@ -13,7 +13,7 @@ categoriesController.get = async function (req, res, next) {
|
||||
}
|
||||
const [states, categoriesData] = await Promise.all([
|
||||
user.getCategoryWatchState(userData.uid),
|
||||
categories.buildForSelect(userData.uid, 'find'),
|
||||
categories.buildForSelect(userData.uid, 'find', ['descriptionParsed', 'depth']),
|
||||
]);
|
||||
|
||||
categoriesData.forEach(function (category) {
|
||||
|
||||
@@ -22,7 +22,7 @@ AdminsMods.get = async function (req, res) {
|
||||
};
|
||||
|
||||
async function getModeratorsOfCategories(uid) {
|
||||
const categoryData = await categories.buildForSelect(uid, 'find');
|
||||
const categoryData = await categories.buildForSelect(uid, 'find', ['depth']);
|
||||
const moderators = await Promise.all(categoryData.map(c => categories.getModerators(c.cid)));
|
||||
categoryData.forEach((c, index) => {
|
||||
c.moderators = moderators[index];
|
||||
|
||||
@@ -11,7 +11,7 @@ categoriesController.get = async function (req, res, next) {
|
||||
const [categoryData, parent, allCategories] = await Promise.all([
|
||||
categories.getCategories([req.params.category_id], req.uid),
|
||||
categories.getParents([req.params.category_id]),
|
||||
categories.buildForSelectAll(req.uid),
|
||||
categories.buildForSelectAll(['text', 'value']),
|
||||
]);
|
||||
|
||||
const category = categoryData[0];
|
||||
@@ -22,7 +22,7 @@ categoriesController.get = async function (req, res, next) {
|
||||
category.parent = parent[0];
|
||||
allCategories.forEach(function (category) {
|
||||
if (category) {
|
||||
category.selected = parseInt(category.cid, 10) === parseInt(req.params.category_id, 10);
|
||||
category.selected = parseInt(category.value, 10) === parseInt(req.params.category_id, 10);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ privilegesController.get = async function (req, res) {
|
||||
const cid = req.params.cid ? parseInt(req.params.cid, 10) : 0;
|
||||
const [privilegesData, categoriesData] = await Promise.all([
|
||||
cid ? privileges.categories.list(cid) : privileges.global.list(),
|
||||
categories.buildForSelectAll(req.uid),
|
||||
categories.buildForSelectAll(),
|
||||
]);
|
||||
|
||||
categoriesData.unshift({
|
||||
|
||||
@@ -89,8 +89,7 @@ async function buildCategories(uid, searchOnly) {
|
||||
let categoriesData = await categories.getCategoriesData(cids);
|
||||
categoriesData = categoriesData.filter(category => category && !category.link);
|
||||
categoriesData = categories.getTree(categoriesData);
|
||||
categoriesData = categories.buildForSelectCategories(categoriesData);
|
||||
categoriesData = categoriesData.map(category => ({ value: category.value, text: category.text }));
|
||||
categoriesData = categories.buildForSelectCategories(categoriesData, ['text', 'value']);
|
||||
|
||||
return [
|
||||
{ value: 'all', text: '[[unread:all_categories]]' },
|
||||
|
||||
@@ -96,7 +96,7 @@ SocketCategories.getMoveCategories = async function (socket, data) {
|
||||
SocketCategories.getSelectCategories = async function (socket) {
|
||||
const [isAdmin, categoriesData] = await Promise.all([
|
||||
user.isAdministrator(socket.uid),
|
||||
categories.buildForSelect(socket.uid, 'find'),
|
||||
categories.buildForSelect(socket.uid, 'find', ['disabled', 'link']),
|
||||
]);
|
||||
return categoriesData.filter(category => category && (!category.disabled || isAdmin) && !category.link);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user