mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: #7842, groups.invite works with an array of uids
This commit is contained in:
@@ -7,7 +7,9 @@ module.exports = function (module) {
|
|||||||
if (!Array.isArray(value)) {
|
if (!Array.isArray(value)) {
|
||||||
value = [value];
|
value = [value];
|
||||||
}
|
}
|
||||||
|
if (!value.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
value = value.map(v => helpers.valueToString(v));
|
value = value.map(v => helpers.valueToString(v));
|
||||||
|
|
||||||
await module.client.collection('objects').updateOne({
|
await module.client.collection('objects').updateOne({
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ module.exports = function (module) {
|
|||||||
if (!Array.isArray(value)) {
|
if (!Array.isArray(value)) {
|
||||||
value = [value];
|
value = [value];
|
||||||
}
|
}
|
||||||
|
if (!value.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await module.transaction(async function (client) {
|
await module.transaction(async function (client) {
|
||||||
await helpers.ensureLegacyObjectType(client, key, 'set');
|
await helpers.ensureLegacyObjectType(client, key, 'set');
|
||||||
await client.query({
|
await client.query({
|
||||||
|
|||||||
@@ -51,44 +51,43 @@ module.exports = function (Groups) {
|
|||||||
await db.setsRemove(sets, uid);
|
await db.setsRemove(sets, uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.invite = async function (groupName, uid) {
|
Groups.invite = async function (groupName, uids) {
|
||||||
await inviteOrRequestMembership(groupName, uid, 'invite');
|
uids = Array.isArray(uids) ? uids : [uids];
|
||||||
const notification = await notifications.create({
|
await inviteOrRequestMembership(groupName, uids, 'invite');
|
||||||
|
|
||||||
|
const notificationData = await Promise.all(uids.map(uid => notifications.create({
|
||||||
type: 'group-invite',
|
type: 'group-invite',
|
||||||
bodyShort: '[[groups:invited.notification_title, ' + groupName + ']]',
|
bodyShort: '[[groups:invited.notification_title, ' + groupName + ']]',
|
||||||
bodyLong: '',
|
bodyLong: '',
|
||||||
nid: 'group:' + groupName + ':uid:' + uid + ':invite',
|
nid: 'group:' + groupName + ':uid:' + uid + ':invite',
|
||||||
path: '/groups/' + utils.slugify(groupName),
|
path: '/groups/' + utils.slugify(groupName),
|
||||||
});
|
})));
|
||||||
await notifications.push(notification, [uid]);
|
|
||||||
|
await Promise.all(uids.map((uid, index) => notifications.push(notificationData[index], uid)));
|
||||||
};
|
};
|
||||||
|
|
||||||
async function inviteOrRequestMembership(groupName, uid, type) {
|
async function inviteOrRequestMembership(groupName, uids, type) {
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
uids = Array.isArray(uids) ? uids : [uids];
|
||||||
throw new Error('[[error:not-logged-in]]');
|
uids = uids.filter(uid => parseInt(uid, 10) > 0);
|
||||||
}
|
|
||||||
|
|
||||||
const [exists, isMember, isPending, isInvited] = await Promise.all([
|
const [exists, isMember, isPending, isInvited] = await Promise.all([
|
||||||
Groups.exists(groupName),
|
Groups.exists(groupName),
|
||||||
Groups.isMember(uid, groupName),
|
Groups.isMembers(uids, groupName),
|
||||||
Groups.isPending(uid, groupName),
|
Groups.isPending(uids, groupName),
|
||||||
Groups.isInvited(uid, groupName),
|
Groups.isInvited(uids, groupName),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
throw new Error('[[error:no-group]]');
|
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';
|
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';
|
const hookName = type === 'invite' ? 'action:group.inviteMember' : 'action:group.requestMembership';
|
||||||
plugins.fireHook(hookName, {
|
plugins.fireHook(hookName, {
|
||||||
groupName: groupName,
|
groupName: groupName,
|
||||||
uid: uid,
|
uids: uids,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -175,23 +175,18 @@ SocketGroups.issueMassInvite = async (socket, data) => {
|
|||||||
if (!data || !data.usernames || !data.groupName) {
|
if (!data || !data.usernames || !data.groupName) {
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
var usernames = String(data.usernames).split(',');
|
let usernames = String(data.usernames).split(',');
|
||||||
usernames = usernames.map(function (username) {
|
usernames = usernames.map(username => username && username.trim());
|
||||||
return username && username.trim();
|
|
||||||
});
|
|
||||||
|
|
||||||
let uids = await user.getUidsByUsernames(usernames);
|
let uids = await user.getUidsByUsernames(usernames);
|
||||||
uids = uids.filter(function (uid) {
|
uids = uids.filter(uid => !!uid && parseInt(uid, 10));
|
||||||
return !!uid && parseInt(uid, 10);
|
|
||||||
});
|
|
||||||
|
|
||||||
// eslint-disable-next-line guard-for-in
|
await groups.invite(data.groupName, uids);
|
||||||
for (const i in uids) {
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
for (const uid of uids) {
|
||||||
await groups.invite(data.groupName, uids[i]);
|
|
||||||
logGroupEvent(socket, 'group-invite', {
|
logGroupEvent(socket, 'group-invite', {
|
||||||
groupName: data.groupName,
|
groupName: data.groupName,
|
||||||
targetUid: data.toUid,
|
targetUid: uid,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,6 +22,14 @@ describe('Set methods', function () {
|
|||||||
done();
|
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 () {
|
describe('getSetMembers()', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user