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

Refactoring code to resolve issue #111 dealing with cognitive complexity #172

Closed
wants to merge 4 commits into from
Closed
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
29 changes: 20 additions & 9 deletions src/api/groups.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

console.log('TJ Patel');
const validator = require('validator');

const privileges = require('../privileges');
Expand Down Expand Up @@ -119,23 +120,33 @@ async function canSearchMembers(uid, groupName) {
}

groupsAPI.join = async function (caller, data) {
function helpor(arg1, arg2) {
return arg1 || arg2;
}

if (!data) {
throw new Error('[[error:invalid-data]]');
}
if (caller.uid <= 0 || !data.uid) {
if (helpor(caller.uid <= 0, !data.uid)) {
throw new Error('[[error:invalid-uid]]');
}

function helpand(con1, con2) {
return con1 && con2;
}

const groupName = await groups.getGroupNameByGroupSlug(data.slug);
if (!groupName) {
throw new Error('[[error:no-group]]');
}

const isCallerAdmin = await privileges.admin.can('admin:groups', caller.uid);
if (!isCallerAdmin && (
groups.systemGroups.includes(groupName) ||
groups.isPrivilegeGroup(groupName)
)) {
if (
helpand(
!isCallerAdmin,
helpor(groups.systemGroups.includes(groupName), groups.isPrivilegeGroup(groupName))
)
) {
throw new Error('[[error:not-allowed]]');
}

Expand All @@ -149,8 +160,8 @@ groupsAPI.join = async function (caller, data) {
}

const isSelf = parseInt(caller.uid, 10) === parseInt(data.uid, 10);
if (!meta.config.allowPrivateGroups && isSelf) {
// all groups are public!

if (helpand(!meta.config.allowPrivateGroups, isSelf)) {
await groups.join(groupName, data.uid);
logGroupEvent(caller, 'group-join', {
groupName: groupName,
Expand All @@ -159,11 +170,11 @@ groupsAPI.join = async function (caller, data) {
return;
}

if (!isCallerAdmin && isSelf && groupData.private && groupData.disableJoinRequests) {
if (helpand(!isCallerAdmin, isSelf) && groupData.private && groupData.disableJoinRequests) {
throw new Error('[[error:group-join-disabled]]');
}

if ((!groupData.private && isSelf) || isCallerAdmin) {
if (helpand(!groupData.private, isSelf) || isCallerAdmin) {
await groups.join(groupName, data.uid);
logGroupEvent(caller, `group-${isSelf ? 'join' : 'add-member'}`, {
groupName: groupName,
Expand Down
Loading