|
|
|
@ -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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|