Skip to content

Commit

Permalink
fix: handle client-side acceptAll and rejectAll
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Apr 6, 2023
1 parent 18c45b4 commit b53f92f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 56 deletions.
28 changes: 14 additions & 14 deletions public/src/client/groups/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
};
Expand Down
30 changes: 0 additions & 30 deletions src/socket.io/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 5 additions & 12 deletions test/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit b53f92f

Please sign in to comment.