Skip to content

Commit

Permalink
modified the GroupsAPI.join function
Browse files Browse the repository at this point in the history
  • Loading branch information
Aya-Abdel-Hamid committed Sep 5, 2024
1 parent e7d3e1d commit bb88086
Showing 1 changed file with 46 additions and 67 deletions.
113 changes: 46 additions & 67 deletions src/api/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,76 +124,55 @@ async function canSearchMembers(uid, groupName) {
throw new Error('[[error:no-privileges]]');
}
}

// modified this function
groupsAPI.join = async function (caller, data) {
if (!data || !data.uid) {
throw new Error('[[error:invalid-data]]');
}
if (caller.uid <= 0) {
throw new Error('[[error:invalid-uid]]');
}

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

const [groupData, userExists, isCallerAdmin] = await Promise.all([
groups.getGroupData(groupName),
user.exists(data.uid),
privileges.admin.can('admin:groups', caller.uid),
]);

if (!userExists) {
throw new Error('[[error:invalid-uid]]');
}

const isSelf = parseInt(caller.uid, 10) === parseInt(data.uid, 10);
if (!isCallerAdmin) {
if (groups.systemGroups.includes(groupName) || groups.isPrivilegeGroup(groupName)) {
throw new Error('[[error:not-allowed]]');
}
if (!isSelf && (groupData.private || groupData.disableJoinRequests)) {
throw new Error('[[error:group-join-disabled]]');
}
}

if (!meta.config.allowPrivateGroups && isSelf) {
await handlePublicGroups(caller, groupName, data.uid);
return;
}

if (isRestrictedJoin(isCallerAdmin, isSelf, groupData)) {
throw new Error('[[error:group-join-disabled]]');
}

if (canJoinGroup(groupData, isSelf, isCallerAdmin)) {
await groups.join(groupName, data.uid);
logGroupEvent(caller, `group-${isSelf ? 'join' : 'add-member'}`, {
groupName,
targetUid: data.uid,
});
} else {
throw new Error('[[error:not-allowed]]');
}

async function handlePublicGroups(caller, groupName, uid) {
await groups.join(groupName, uid);
logGroupEvent(caller, 'group-join', {
groupName,
targetUid: uid,
});
}

function isRestrictedJoin(isCallerAdmin, isSelf, groupData) {
return !isCallerAdmin && isSelf && groupData.private && groupData.disableJoinRequests;
}

function canJoinGroup(groupData, isSelf, isCallerAdmin) {
return (!groupData.private && isSelf) || isCallerAdmin;
}
if (!data || !data.uid) {

Check failure on line 129 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
throw new Error('[[error:invalid-data]]');

Check failure on line 130 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
}

Check failure on line 131 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

const callerUid = parseInt(caller.uid, 10);

Check failure on line 133 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
const targetUid = parseInt(data.uid, 10);

Check failure on line 134 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

if (callerUid <= 0 || targetUid <= 0) {

Check failure on line 136 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
throw new Error('[[error:invalid-uid]]');

Check failure on line 137 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
}

Check failure on line 138 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

const groupName = await groups.getGroupNameByGroupSlug(data.slug);

Check failure on line 140 in src/api/groups.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
if (!groupName) {
throw new Error('[[error:no-group]]');
}

const [groupData, userExists, isCallerAdmin, isSelf] = await Promise.all([
groups.getGroupData(groupName),
user.exists(targetUid),
privileges.admin.can('admin:groups', callerUid),
callerUid === targetUid
]);

if (!userExists) {
throw new Error('[[error:invalid-uid]]');
}

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

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

if (isCallerAdmin || (!groupData.private && isSelf)) {
await groups.join(groupName, targetUid);
logGroupEvent(caller, `group-${isSelf ? 'join' : 'add-member'}`, {
groupName,
targetUid
});
} else {
throw new Error('[[error:not-allowed]]');
}
};


groupsAPI.leave = async function (caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
Expand Down

0 comments on commit bb88086

Please sign in to comment.