mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-15 18:26:15 +01:00
feat: move groups.join to api
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
const privileges = require('../privileges');
|
||||
const events = require('../events');
|
||||
const groups = require('../groups');
|
||||
const user = require('../user');
|
||||
const meta = require('../meta');
|
||||
|
||||
const groupsAPI = module.exports;
|
||||
|
||||
@@ -27,9 +29,60 @@ groupsAPI.create = async function (caller, data) {
|
||||
return groupData;
|
||||
};
|
||||
|
||||
// groupsAPI.join = async function (caller, data) {
|
||||
// // TODO:
|
||||
// };
|
||||
groupsAPI.join = async function (caller, data) {
|
||||
if (caller.uid <= 0 || !data.uid) {
|
||||
throw new Error('[[error:invalid-uid]]');
|
||||
}
|
||||
|
||||
const isSelf = parseInt(caller.uid, 10) === parseInt(data.uid, 10);
|
||||
const groupName = await groups.getGroupNameByGroupSlug(data.slug);
|
||||
if (!groupName) {
|
||||
throw new Error('[[error:no-group]]');
|
||||
}
|
||||
|
||||
if (groups.systemGroups.includes(groupName) || groups.isPrivilegeGroup(groupName)) {
|
||||
throw new Error('[[error:not-allowed]]');
|
||||
}
|
||||
|
||||
const [groupData, isCallerAdmin, isCallerOwner, userExists] = await Promise.all([
|
||||
groups.getGroupData(groupName),
|
||||
user.isAdministrator(caller.uid),
|
||||
groups.ownership.isOwner(caller.uid, groupName),
|
||||
user.exists(data.uid),
|
||||
]);
|
||||
|
||||
if (!userExists) {
|
||||
throw new Error('[[error:invalid-uid]]');
|
||||
}
|
||||
|
||||
if (!meta.config.allowPrivateGroups && isSelf) {
|
||||
// all groups are public!
|
||||
await groups.join(groupName, data.uid);
|
||||
logGroupEvent(caller, 'group-join', {
|
||||
groupName: groupName,
|
||||
targetUid: data.uid,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (groupData.private && groupData.disableJoinRequests) {
|
||||
throw new Error('[[error:group-join-disabled]]');
|
||||
}
|
||||
|
||||
if ((!groupData.private && isSelf) || isCallerAdmin || isCallerOwner) {
|
||||
await groups.join(groupName, data.uid);
|
||||
logGroupEvent(caller, 'group-join', {
|
||||
groupName: groupName,
|
||||
targetUid: data.uid,
|
||||
});
|
||||
} else if (isSelf) {
|
||||
await groups.requestMembership(groupName, caller.uid);
|
||||
logGroupEvent(caller, 'group-request-membership', {
|
||||
groupName: groupName,
|
||||
targetUid: data.uid,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// groupsAPI.leave = async function (caller, data) {
|
||||
// // TODO:
|
||||
|
||||
Reference in New Issue
Block a user