Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor createCategory function to reduce cognitive complexity #587

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "nodebb"
}
}
82 changes: 40 additions & 42 deletions src/categories/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cache = require('../cache');

module.exports = function (Categories) {
Categories.create = async function (data) {
const parentCid = data.parentCid ? data.parentCid : 0;
const parentCid = data.parentCid || 0;
const [cid, firstChild] = await Promise.all([
db.incrObjectField('global', 'nextCid'),
db.getSortedSetRangeWithScores(`cid:${parentCid}:children`, 0, 0),
Expand All @@ -24,52 +24,14 @@ module.exports = function (Categories) {
const order = data.order || smallestOrder; // If no order provided, place it at the top
const colours = Categories.assignColours();

let category = {
cid: cid,
name: data.name,
description: data.description ? data.description : '',
descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '',
icon: data.icon ? data.icon : '',
bgColor: data.bgColor || colours[0],
color: data.color || colours[1],
slug: slug,
parentCid: parentCid,
topic_count: 0,
post_count: 0,
disabled: data.disabled ? 1 : 0,
order: order,
link: data.link || '',
numRecentReplies: 1,
class: (data.class ? data.class : 'col-md-3 col-6'),
imageClass: 'cover',
isSection: 0,
subCategoriesPerPage: 10,
};
let category = createCategory(data, cid, colours, slug, parentCid, order);

if (data.backgroundImage) {
category.backgroundImage = data.backgroundImage;
}

const defaultPrivileges = [
'groups:find',
'groups:read',
'groups:topics:read',
'groups:topics:create',
'groups:topics:reply',
'groups:topics:tag',
'groups:posts:edit',
'groups:posts:history',
'groups:posts:delete',
'groups:posts:upvote',
'groups:posts:downvote',
'groups:topics:delete',
];
const modPrivileges = defaultPrivileges.concat([
'groups:topics:schedule',
'groups:posts:view_deleted',
'groups:purge',
]);
const guestPrivileges = ['groups:find', 'groups:read', 'groups:topics:read'];
const { defaultPrivileges, modPrivileges, guestPrivileges } = getCategoryPrivileges();


const result = await plugins.hooks.fire('filter:category.create', {
category: category,
Expand Down Expand Up @@ -259,4 +221,40 @@ module.exports = function (Categories) {
await privileges.categories.give(givePrivs, toCid, group);
await privileges.categories.rescind(rescindPrivs, toCid, group);
}
function getCategoryPrivileges() {
const defaultPrivileges = [
'groups:find', 'groups:read', 'groups:topics:read', 'groups:topics:create',
'groups:topics:reply', 'groups:topics:tag', 'groups:posts:edit',
'groups:posts:history', 'groups:posts:delete', 'groups:posts:upvote',
'groups:posts:downvote', 'groups:topics:delete',
];
const modPrivileges = defaultPrivileges.concat([
'groups:topics:schedule', 'groups:posts:view_deleted', 'groups:purge',
]);
const guestPrivileges = ['groups:find', 'groups:read', 'groups:topics:read'];
return { defaultPrivileges, modPrivileges, guestPrivileges };
}
function createCategory(data, cid, colours, slug, parentCid, order) {
return {
cid: cid,
name: data.name || `Category ${cid}`,
description: data.description || '',
descriptionParsed: data.descriptionParsed || '',
icon: data.icon || '',
bgColor: data.bgColor || colours[0],
color: data.color || colours[1],
slug: slug,
parentCid: parentCid,
topic_count: 0,
post_count: 0,
disabled: data.disabled ? 1 : 0,
order: order,
link: data.link || '',
numRecentReplies: 1,
class: data.class || 'col-md-3 col-6',
imageClass: 'cover',
isSection: 0,
subCategoriesPerPage: 10,
};
}
};