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;
|
return tree;
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.buildForSelect = async function (uid, privilege) {
|
Categories.buildForSelect = async function (uid, privilege, fields) {
|
||||||
let categories = await Categories.getCategoriesByPrivilege('categories:cid', uid, privilege);
|
const cids = await Categories.getCidsByPrivilege('categories:cid', uid, privilege);
|
||||||
categories = Categories.getTree(categories);
|
return await getSelectData(cids, fields);
|
||||||
return Categories.buildForSelectCategories(categories);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.buildForSelectAll = async function (uid) {
|
Categories.buildForSelectAll = async function (fields) {
|
||||||
const categoryData = await Categories.getAllCategories(uid);
|
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);
|
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) {
|
function recursive(category, categoriesData, level, depth) {
|
||||||
const bullet = level ? '• ' : '';
|
const bullet = level ? '• ' : '';
|
||||||
category.value = category.cid;
|
category.value = category.cid;
|
||||||
@@ -347,14 +351,21 @@ Categories.buildForSelectCategories = function (categories) {
|
|||||||
|
|
||||||
const categoriesData = [];
|
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 = [
|
const pickFields = [
|
||||||
'name', 'level', 'disabledClass', 'icon', 'value', 'text',
|
'cid', 'name', 'level', 'icon', 'parentCid',
|
||||||
'cid', 'parentCid', 'color', 'bgColor', 'backgroundImage', 'imageClass',
|
'color', 'bgColor', 'backgroundImage', 'imageClass',
|
||||||
'disabled', 'depth',
|
|
||||||
];
|
];
|
||||||
|
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));
|
return categoriesData.map(category => _.pick(category, pickFields));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ categoriesController.get = async function (req, res, next) {
|
|||||||
}
|
}
|
||||||
const [states, categoriesData] = await Promise.all([
|
const [states, categoriesData] = await Promise.all([
|
||||||
user.getCategoryWatchState(userData.uid),
|
user.getCategoryWatchState(userData.uid),
|
||||||
categories.buildForSelect(userData.uid, 'find'),
|
categories.buildForSelect(userData.uid, 'find', ['descriptionParsed', 'depth']),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
categoriesData.forEach(function (category) {
|
categoriesData.forEach(function (category) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ AdminsMods.get = async function (req, res) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function getModeratorsOfCategories(uid) {
|
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)));
|
const moderators = await Promise.all(categoryData.map(c => categories.getModerators(c.cid)));
|
||||||
categoryData.forEach((c, index) => {
|
categoryData.forEach((c, index) => {
|
||||||
c.moderators = moderators[index];
|
c.moderators = moderators[index];
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ categoriesController.get = async function (req, res, next) {
|
|||||||
const [categoryData, parent, allCategories] = await Promise.all([
|
const [categoryData, parent, allCategories] = await Promise.all([
|
||||||
categories.getCategories([req.params.category_id], req.uid),
|
categories.getCategories([req.params.category_id], req.uid),
|
||||||
categories.getParents([req.params.category_id]),
|
categories.getParents([req.params.category_id]),
|
||||||
categories.buildForSelectAll(req.uid),
|
categories.buildForSelectAll(['text', 'value']),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const category = categoryData[0];
|
const category = categoryData[0];
|
||||||
@@ -22,7 +22,7 @@ categoriesController.get = async function (req, res, next) {
|
|||||||
category.parent = parent[0];
|
category.parent = parent[0];
|
||||||
allCategories.forEach(function (category) {
|
allCategories.forEach(function (category) {
|
||||||
if (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 cid = req.params.cid ? parseInt(req.params.cid, 10) : 0;
|
||||||
const [privilegesData, categoriesData] = await Promise.all([
|
const [privilegesData, categoriesData] = await Promise.all([
|
||||||
cid ? privileges.categories.list(cid) : privileges.global.list(),
|
cid ? privileges.categories.list(cid) : privileges.global.list(),
|
||||||
categories.buildForSelectAll(req.uid),
|
categories.buildForSelectAll(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
categoriesData.unshift({
|
categoriesData.unshift({
|
||||||
|
|||||||
@@ -89,8 +89,7 @@ async function buildCategories(uid, searchOnly) {
|
|||||||
let categoriesData = await categories.getCategoriesData(cids);
|
let categoriesData = await categories.getCategoriesData(cids);
|
||||||
categoriesData = categoriesData.filter(category => category && !category.link);
|
categoriesData = categoriesData.filter(category => category && !category.link);
|
||||||
categoriesData = categories.getTree(categoriesData);
|
categoriesData = categories.getTree(categoriesData);
|
||||||
categoriesData = categories.buildForSelectCategories(categoriesData);
|
categoriesData = categories.buildForSelectCategories(categoriesData, ['text', 'value']);
|
||||||
categoriesData = categoriesData.map(category => ({ value: category.value, text: category.text }));
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ value: 'all', text: '[[unread:all_categories]]' },
|
{ value: 'all', text: '[[unread:all_categories]]' },
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ SocketCategories.getMoveCategories = async function (socket, data) {
|
|||||||
SocketCategories.getSelectCategories = async function (socket) {
|
SocketCategories.getSelectCategories = async function (socket) {
|
||||||
const [isAdmin, categoriesData] = await Promise.all([
|
const [isAdmin, categoriesData] = await Promise.all([
|
||||||
user.isAdministrator(socket.uid),
|
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);
|
return categoriesData.filter(category => category && (!category.disabled || isAdmin) && !category.link);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user