mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: handle client-side acceptAll and rejectAll
This commit is contained in:
@@ -145,22 +145,22 @@ define('forum/groups/details', [
|
||||
}).catch(alerts.error);
|
||||
break;
|
||||
|
||||
// TODO (14/10/2020): rewrite these to use api module and merge with above 2 case blocks
|
||||
case 'acceptAll': // intentional fall-throughs!
|
||||
case 'rejectAll':
|
||||
socket.emit('groups.' + action, {
|
||||
toUid: uid,
|
||||
groupName: groupName,
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return alerts.error(err);
|
||||
}
|
||||
if (action === 'rescindInvite' || action === 'accept' || action === 'reject') {
|
||||
return userRow.remove();
|
||||
}
|
||||
case 'acceptAll': // falls throughs
|
||||
case 'rejectAll': {
|
||||
const listEl = document.querySelector('[component="groups/pending"]');
|
||||
if (!listEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const method = action === 'acceptAll' ? 'put' : 'del';
|
||||
let uids = Array.prototype.map.call(listEl.querySelectorAll('[data-uid]'), el => parseInt(el.getAttribute('data-uid'), 10));
|
||||
uids = uids.filter((uid, idx) => uids.indexOf(uid) === idx);
|
||||
|
||||
Promise.all(uids.map(async uid => api[method](`/groups/${ajaxify.data.group.slug}/pending/${uid}`))).then(() => {
|
||||
ajaxify.refresh();
|
||||
});
|
||||
}).catch(alerts.error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -56,36 +56,6 @@ async function isOwner(socket, data) {
|
||||
}
|
||||
}
|
||||
|
||||
SocketGroups.acceptAll = async (socket, data) => {
|
||||
await isOwner(socket, data);
|
||||
await acceptRejectAll(SocketGroups.accept, socket, data);
|
||||
};
|
||||
|
||||
SocketGroups.rejectAll = async (socket, data) => {
|
||||
await isOwner(socket, data);
|
||||
await acceptRejectAll(SocketGroups.reject, socket, data);
|
||||
};
|
||||
|
||||
async function acceptRejectAll(method, socket, data) {
|
||||
if (typeof data.groupName !== 'string') {
|
||||
throw new Error('[[error:invalid-group-name]]');
|
||||
}
|
||||
const users = await groups.getPending(data.groupName);
|
||||
const uids = users.map(u => u.uid);
|
||||
await Promise.all(uids.map(async (uid) => {
|
||||
await method(socket, { groupName: data.groupName, toUid: uid });
|
||||
}));
|
||||
}
|
||||
|
||||
SocketGroups.issueInvite = async (socket, data) => {
|
||||
await isOwner(socket, data);
|
||||
await groups.invite(data.groupName, data.toUid);
|
||||
logGroupEvent(socket, 'group-invite', {
|
||||
groupName: data.groupName,
|
||||
targetUid: data.toUid,
|
||||
});
|
||||
};
|
||||
|
||||
SocketGroups.issueMassInvite = async (socket, data) => {
|
||||
await isOwner(socket, data);
|
||||
if (!data || !data.usernames || !data.groupName) {
|
||||
|
||||
@@ -925,18 +925,11 @@ describe('Groups', () => {
|
||||
assert(isMember);
|
||||
});
|
||||
|
||||
it('should issue invite to user', (done) => {
|
||||
User.create({ username: 'invite1' }, (err, uid) => {
|
||||
assert.ifError(err);
|
||||
socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, (err) => {
|
||||
assert.ifError(err);
|
||||
Groups.isInvited(uid, 'PrivateCanJoin', (err, isInvited) => {
|
||||
assert.ifError(err);
|
||||
assert(isInvited);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should issue invite to user', async () => {
|
||||
const uid = await User.create({ username: 'invite1' });
|
||||
await apiGroups.issueInvite({ uid: adminUid }, { slug: 'privatecanjoin', uid });
|
||||
const isInvited = await Groups.isInvited(uid, 'PrivateCanJoin');
|
||||
assert(isInvited);
|
||||
});
|
||||
|
||||
it('should fail with invalid data', (done) => {
|
||||
|
||||
Reference in New Issue
Block a user