From b53f92fa2e5a51a86aac27ee5fe1d759366f4f76 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 31 Mar 2023 17:48:35 -0400 Subject: [PATCH] fix: handle client-side acceptAll and rejectAll --- public/src/client/groups/details.js | 28 +++++++++++++-------------- src/socket.io/groups.js | 30 ----------------------------- test/groups.js | 17 +++++----------- 3 files changed, 19 insertions(+), 56 deletions(-) diff --git a/public/src/client/groups/details.js b/public/src/client/groups/details.js index b9f36b4a7b..3dc4f79e3f 100644 --- a/public/src/client/groups/details.js +++ b/public/src/client/groups/details.js @@ -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; + } } }); }; diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js index c7ea5ded17..00777d5824 100644 --- a/src/socket.io/groups.js +++ b/src/socket.io/groups.js @@ -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) { diff --git a/test/groups.js b/test/groups.js index e8661cea0b..632f4f037f 100644 --- a/test/groups.js +++ b/test/groups.js @@ -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) => {