mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-08 14:52:03 +02:00
refactor: move groups.leave, fix some tests
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const validator = require('validator');
|
||||
|
||||
const privileges = require('../privileges');
|
||||
const events = require('../events');
|
||||
const groups = require('../groups');
|
||||
const user = require('../user');
|
||||
const meta = require('../meta');
|
||||
const notifications = require('../notifications');
|
||||
const slugify = require('../slugify');
|
||||
|
||||
const groupsAPI = module.exports;
|
||||
|
||||
@@ -100,10 +104,64 @@ groupsAPI.join = async function (caller, data) {
|
||||
}
|
||||
};
|
||||
|
||||
// groupsAPI.leave = async function (caller, data) {
|
||||
// // TODO:
|
||||
// };
|
||||
groupsAPI.leave = async function (caller, data) {
|
||||
if (caller.uid <= 0) {
|
||||
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 (typeof groupName !== 'string') {
|
||||
throw new Error('[[error:invalid-group-name]]');
|
||||
}
|
||||
|
||||
if (groupName === 'administrators' && isSelf) {
|
||||
throw new Error('[[error:cant-remove-self-as-admin]]');
|
||||
}
|
||||
|
||||
const [groupData, isCallerAdmin, isCallerOwner, userExists, isMember] = await Promise.all([
|
||||
groups.getGroupData(groupName),
|
||||
user.isAdministrator(caller.uid),
|
||||
groups.ownership.isOwner(caller.uid, groupName),
|
||||
user.exists(data.uid),
|
||||
groups.isMember(data.uid, groupName),
|
||||
]);
|
||||
|
||||
if (!userExists) {
|
||||
throw new Error('[[error:invalid-uid]]');
|
||||
}
|
||||
if (!isMember) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (groupData.disableLeave && isSelf) {
|
||||
throw new Error('[[error:group-leave-disabled]]');
|
||||
}
|
||||
|
||||
if (isSelf || isCallerAdmin || isCallerOwner) {
|
||||
await groups.leave(groupName, data.uid);
|
||||
} else {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
const username = await user.getUserField(data.uid, 'username');
|
||||
const notification = await notifications.create({
|
||||
type: 'group-leave',
|
||||
bodyShort: '[[groups:membership.leave.notification_title, ' + username + ', ' + groupName + ']]',
|
||||
nid: 'group:' + validator.escape(groupName) + ':uid:' + data.uid + ':group-leave',
|
||||
path: '/groups/' + slugify(groupName),
|
||||
});
|
||||
const uids = await groups.getOwners(groupName);
|
||||
await notifications.push(notification, uids);
|
||||
|
||||
logGroupEvent(caller, 'group-leave', {
|
||||
groupName: groupName,
|
||||
targetUid: data.uid,
|
||||
});
|
||||
};
|
||||
|
||||
async function isOwner(caller, groupName) {
|
||||
if (typeof groupName !== 'string') {
|
||||
|
||||
@@ -12,6 +12,9 @@ const events = require('../events');
|
||||
const usersAPI = module.exports;
|
||||
|
||||
usersAPI.create = async function (caller, data) {
|
||||
if (!data) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const uid = await user.create(data);
|
||||
return await user.getUserData(uid);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user