fix: handle client-side acceptAll and rejectAll

isekai-main
Julian Lam 2 years ago
parent 18c45b4461
commit b53f92fa2e

@ -145,22 +145,22 @@ define('forum/groups/details', [
}).catch(alerts.error); }).catch(alerts.error);
break; break;
// TODO (14/10/2020): rewrite these to use api module and merge with above 2 case blocks case 'acceptAll': // falls throughs
case 'acceptAll': // intentional fall-throughs! case 'rejectAll': {
case 'rejectAll': const listEl = document.querySelector('[component="groups/pending"]');
socket.emit('groups.' + action, { if (!listEl) {
toUid: uid, return;
groupName: groupName, }
}, function (err) {
if (err) { const method = action === 'acceptAll' ? 'put' : 'del';
return alerts.error(err); 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);
if (action === 'rescindInvite' || action === 'accept' || action === 'reject') {
return userRow.remove(); Promise.all(uids.map(async uid => api[method](`/groups/${ajaxify.data.group.slug}/pending/${uid}`))).then(() => {
}
ajaxify.refresh(); ajaxify.refresh();
}); }).catch(alerts.error);
break; 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) => { SocketGroups.issueMassInvite = async (socket, data) => {
await isOwner(socket, data); await isOwner(socket, data);
if (!data || !data.usernames || !data.groupName) { if (!data || !data.usernames || !data.groupName) {

@ -925,18 +925,11 @@ describe('Groups', () => {
assert(isMember); assert(isMember);
}); });
it('should issue invite to user', (done) => { it('should issue invite to user', async () => {
User.create({ username: 'invite1' }, (err, uid) => { const uid = await User.create({ username: 'invite1' });
assert.ifError(err); await apiGroups.issueInvite({ uid: adminUid }, { slug: 'privatecanjoin', uid });
socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, (err) => { const isInvited = await Groups.isInvited(uid, 'PrivateCanJoin');
assert.ifError(err); assert(isInvited);
Groups.isInvited(uid, 'PrivateCanJoin', (err, isInvited) => {
assert.ifError(err);
assert(isInvited);
done();
});
});
});
}); });
it('should fail with invalid data', (done) => { it('should fail with invalid data', (done) => {

Loading…
Cancel
Save