fix: #7842, groups.invite works with an array of uids

v1.18.x
Barış Soner Uşaklı 5 years ago
parent fa26855671
commit 1e0190abef

@ -7,7 +7,9 @@ module.exports = function (module) {
if (!Array.isArray(value)) {
value = [value];
}
if (!value.length) {
return;
}
value = value.map(v => helpers.valueToString(v));
await module.client.collection('objects').updateOne({

@ -9,7 +9,9 @@ module.exports = function (module) {
if (!Array.isArray(value)) {
value = [value];
}
if (!value.length) {
return;
}
await module.transaction(async function (client) {
await helpers.ensureLegacyObjectType(client, key, 'set');
await client.query({

@ -51,44 +51,43 @@ module.exports = function (Groups) {
await db.setsRemove(sets, uid);
};
Groups.invite = async function (groupName, uid) {
await inviteOrRequestMembership(groupName, uid, 'invite');
const notification = await notifications.create({
Groups.invite = async function (groupName, uids) {
uids = Array.isArray(uids) ? uids : [uids];
await inviteOrRequestMembership(groupName, uids, 'invite');
const notificationData = await Promise.all(uids.map(uid => notifications.create({
type: 'group-invite',
bodyShort: '[[groups:invited.notification_title, ' + groupName + ']]',
bodyLong: '',
nid: 'group:' + groupName + ':uid:' + uid + ':invite',
path: '/groups/' + utils.slugify(groupName),
});
await notifications.push(notification, [uid]);
};
})));
async function inviteOrRequestMembership(groupName, uid, type) {
if (!(parseInt(uid, 10) > 0)) {
throw new Error('[[error:not-logged-in]]');
}
await Promise.all(uids.map((uid, index) => notifications.push(notificationData[index], uid)));
};
async function inviteOrRequestMembership(groupName, uids, type) {
uids = Array.isArray(uids) ? uids : [uids];
uids = uids.filter(uid => parseInt(uid, 10) > 0);
const [exists, isMember, isPending, isInvited] = await Promise.all([
Groups.exists(groupName),
Groups.isMember(uid, groupName),
Groups.isPending(uid, groupName),
Groups.isInvited(uid, groupName),
Groups.isMembers(uids, groupName),
Groups.isPending(uids, groupName),
Groups.isInvited(uids, groupName),
]);
if (!exists) {
throw new Error('[[error:no-group]]');
} else if (isMember || (type === 'invite' && isInvited)) {
return;
} else if (type === 'request' && isPending) {
throw new Error('[[error:group-already-requested]]');
}
uids = uids.filter((uid, i) => !isMember[i] && ((type === 'invite' && !isInvited[i]) || (type === 'request' && !isPending[i])));
const set = type === 'invite' ? 'group:' + groupName + ':invited' : 'group:' + groupName + ':pending';
await db.setAdd(set, uid);
await db.setAdd(set, uids);
const hookName = type === 'invite' ? 'action:group.inviteMember' : 'action:group.requestMembership';
plugins.fireHook(hookName, {
groupName: groupName,
uid: uid,
uids: uids,
});
}

@ -175,23 +175,18 @@ SocketGroups.issueMassInvite = async (socket, data) => {
if (!data || !data.usernames || !data.groupName) {
throw new Error('[[error:invalid-data]]');
}
var usernames = String(data.usernames).split(',');
usernames = usernames.map(function (username) {
return username && username.trim();
});
let usernames = String(data.usernames).split(',');
usernames = usernames.map(username => username && username.trim());
let uids = await user.getUidsByUsernames(usernames);
uids = uids.filter(function (uid) {
return !!uid && parseInt(uid, 10);
});
uids = uids.filter(uid => !!uid && parseInt(uid, 10));
await groups.invite(data.groupName, uids);
// eslint-disable-next-line guard-for-in
for (const i in uids) {
// eslint-disable-next-line no-await-in-loop
await groups.invite(data.groupName, uids[i]);
for (const uid of uids) {
logGroupEvent(socket, 'group-invite', {
groupName: data.groupName,
targetUid: data.toUid,
targetUid: uid,
});
}
};

@ -22,6 +22,14 @@ describe('Set methods', function () {
done();
});
});
it('should not do anything if values array is empty', async function () {
await db.setAdd('emptyArraySet', []);
const members = await db.getSetMembers('emptyArraySet');
const exists = await db.exists('emptyArraySet');
assert.deepStrictEqual(members, []);
assert(!exists);
});
});
describe('getSetMembers()', function () {

Loading…
Cancel
Save