From 911918e34cbfc1b46bbf7f390ffd9bf178533bb7 Mon Sep 17 00:00:00 2001 From: TJ Patel Date: Wed, 22 Jan 2025 20:30:36 -0500 Subject: [PATCH 1/4] used helper functions to reduce cognitive complexity --- src/api/groups.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/api/groups.js b/src/api/groups.js index 95074c4b6a..06f78eebbe 100644 --- a/src/api/groups.js +++ b/src/api/groups.js @@ -118,26 +118,45 @@ async function canSearchMembers(uid, groupName) { } } -groupsAPI.join = async function (caller, data) { +async function validcaller(caller) { + if (caller.uid <= 0) { + throw new Error('[[error:invalid-uid]]'); + } +} + +async function validdata(data) { if (!data) { throw new Error('[[error:invalid-data]]'); } - if (caller.uid <= 0 || !data.uid) { + if (!data.uid) { throw new Error('[[error:invalid-uid]]'); } +} +async function validGroup(data) { const groupName = await groups.getGroupNameByGroupSlug(data.slug); if (!groupName) { throw new Error('[[error:no-group]]'); } + return groupName; +} +async function validPriv(caller, groupName) { const isCallerAdmin = await privileges.admin.can('admin:groups', caller.uid); - if (!isCallerAdmin && ( - groups.systemGroups.includes(groupName) || - groups.isPrivilegeGroup(groupName) - )) { + if ( + !isCallerAdmin && + (groups.systemGroups.includes(groupName) || groups.isPrivilegeGroup(groupName)) + ) { throw new Error('[[error:not-allowed]]'); } + return isCallerAdmin; +} + +groupsAPI.join = async function (caller, data) { + await validdata(data); + await validcaller(caller); + const groupName = await validGroup(data); + const isCallerAdmin = await validPriv(caller, groupName); const [groupData, userExists] = await Promise.all([ groups.getGroupData(groupName), @@ -149,8 +168,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! await groups.join(groupName, data.uid); logGroupEvent(caller, 'group-join', { groupName: groupName, From 9617424aa8e7cef093e4d9d96819c51ea955d0bc Mon Sep 17 00:00:00 2001 From: TJ Patel Date: Thu, 23 Jan 2025 20:33:08 -0500 Subject: [PATCH 2/4] added helper and / or functions to reduce complexity --- src/api/groups.js | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/api/groups.js b/src/api/groups.js index 06f78eebbe..64a44c0b08 100644 --- a/src/api/groups.js +++ b/src/api/groups.js @@ -118,45 +118,36 @@ async function canSearchMembers(uid, groupName) { } } -async function validcaller(caller) { - if (caller.uid <= 0) { - throw new Error('[[error:invalid-uid]]'); +groupsAPI.join = async function (caller, data) { + function helpor(arg1, arg2) { + return arg1 || arg2; } -} -async function validdata(data) { if (!data) { throw new Error('[[error:invalid-data]]'); } - if (!data.uid) { + if (helpor(caller.uid <= 0, !data.uid)) { throw new Error('[[error:invalid-uid]]'); } -} -async function validGroup(data) { + function helpand(con1, con2) { + return con1 && con2; + } + const groupName = await groups.getGroupNameByGroupSlug(data.slug); if (!groupName) { throw new Error('[[error:no-group]]'); } - return groupName; -} -async function validPriv(caller, groupName) { const isCallerAdmin = await privileges.admin.can('admin:groups', caller.uid); if ( - !isCallerAdmin && - (groups.systemGroups.includes(groupName) || groups.isPrivilegeGroup(groupName)) + helpand( + !isCallerAdmin, + helpor(groups.systemGroups.includes(groupName), groups.isPrivilegeGroup(groupName)) + ) ) { throw new Error('[[error:not-allowed]]'); } - return isCallerAdmin; -} - -groupsAPI.join = async function (caller, data) { - await validdata(data); - await validcaller(caller); - const groupName = await validGroup(data); - const isCallerAdmin = await validPriv(caller, groupName); const [groupData, userExists] = await Promise.all([ groups.getGroupData(groupName), @@ -169,7 +160,7 @@ groupsAPI.join = async function (caller, data) { const isSelf = parseInt(caller.uid, 10) === parseInt(data.uid, 10); - if (!meta.config.allowPrivateGroups && isSelf) { + if (helpand(!meta.config.allowPrivateGroups, isSelf)) { await groups.join(groupName, data.uid); logGroupEvent(caller, 'group-join', { groupName: groupName, @@ -178,11 +169,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, From ecf418dab7d0b84db8bba71ffd0058ee096b00f7 Mon Sep 17 00:00:00 2001 From: TJ Patel Date: Thu, 23 Jan 2025 21:10:04 -0500 Subject: [PATCH 3/4] console log --- src/api/groups.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/groups.js b/src/api/groups.js index 64a44c0b08..6e29287bb0 100644 --- a/src/api/groups.js +++ b/src/api/groups.js @@ -1,5 +1,7 @@ 'use strict'; +console.log('TJ Patel'); + const validator = require('validator'); const privileges = require('../privileges'); From 78e051e69868e5229649ca47fae81c0711bfe8c3 Mon Sep 17 00:00:00 2001 From: TJ Patel Date: Mon, 10 Feb 2025 00:02:59 -0500 Subject: [PATCH 4/4] pushing --- src/api/groups.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/groups.js b/src/api/groups.js index 6e29287bb0..312c1d2eb0 100644 --- a/src/api/groups.js +++ b/src/api/groups.js @@ -1,7 +1,6 @@ 'use strict'; console.log('TJ Patel'); - const validator = require('validator'); const privileges = require('../privileges');