mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: #7743, groups/index, invite, leave,membership
This commit is contained in:
@@ -11,6 +11,7 @@ require('./data')(Groups);
|
|||||||
require('./create')(Groups);
|
require('./create')(Groups);
|
||||||
require('./delete')(Groups);
|
require('./delete')(Groups);
|
||||||
require('./update')(Groups);
|
require('./update')(Groups);
|
||||||
|
require('./invite')(Groups);
|
||||||
require('./membership')(Groups);
|
require('./membership')(Groups);
|
||||||
require('./ownership')(Groups);
|
require('./ownership')(Groups);
|
||||||
require('./search')(Groups);
|
require('./search')(Groups);
|
||||||
|
|||||||
105
src/groups/invite.js
Normal file
105
src/groups/invite.js
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const db = require('../database');
|
||||||
|
const user = require('../user');
|
||||||
|
const utils = require('../utils');
|
||||||
|
const plugins = require('../plugins');
|
||||||
|
const notifications = require('../notifications');
|
||||||
|
|
||||||
|
module.exports = function (Groups) {
|
||||||
|
Groups.requestMembership = async function (groupName, uid) {
|
||||||
|
await inviteOrRequestMembership(groupName, uid, 'request');
|
||||||
|
const username = await user.getUserField(uid, 'username');
|
||||||
|
const [notification, owners] = await Promise.all([
|
||||||
|
notifications.create({
|
||||||
|
type: 'group-request-membership',
|
||||||
|
bodyShort: '[[groups:request.notification_title, ' + username + ']]',
|
||||||
|
bodyLong: '[[groups:request.notification_text, ' + username + ', ' + groupName + ']]',
|
||||||
|
nid: 'group:' + groupName + ':uid:' + uid + ':request',
|
||||||
|
path: '/groups/' + utils.slugify(groupName),
|
||||||
|
from: uid,
|
||||||
|
}),
|
||||||
|
Groups.getOwners(groupName),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await notifications.push(notification, owners);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.acceptMembership = async function (groupName, uid) {
|
||||||
|
await db.setsRemove(['group:' + groupName + ':pending', 'group:' + groupName + ':invited'], uid);
|
||||||
|
await Groups.join(groupName, uid);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.rejectMembership = async function (groupNames, uid) {
|
||||||
|
if (!Array.isArray(groupNames)) {
|
||||||
|
groupNames = [groupNames];
|
||||||
|
}
|
||||||
|
const sets = [];
|
||||||
|
groupNames.forEach(function (groupName) {
|
||||||
|
sets.push('group:' + groupName + ':pending', 'group:' + groupName + ':invited');
|
||||||
|
});
|
||||||
|
await db.setsRemove(sets, uid);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.invite = async function (groupName, uid) {
|
||||||
|
await inviteOrRequestMembership(groupName, uid, 'invite');
|
||||||
|
const notification = await 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]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const [exists, isMember, isPending, isInvited] = await Promise.all([
|
||||||
|
Groups.exists(groupName),
|
||||||
|
Groups.isMember(uid, groupName),
|
||||||
|
Groups.isPending(uid, groupName),
|
||||||
|
Groups.isInvited(uid, 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]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const set = type === 'invite' ? 'group:' + groupName + ':invited' : 'group:' + groupName + ':pending';
|
||||||
|
await db.setAdd(set, uid);
|
||||||
|
const hookName = type === 'invite' ? 'action:group.inviteMember' : 'action:group.requestMembership';
|
||||||
|
plugins.fireHook(hookName, {
|
||||||
|
groupName: groupName,
|
||||||
|
uid: uid,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Groups.isInvited = async function (uid, groupName) {
|
||||||
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return await db.isSetMember('group:' + groupName + ':invited', uid);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.isPending = async function (uid, groupName) {
|
||||||
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return await db.isSetMember('group:' + groupName + ':pending', uid);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.getPending = async function (groupName) {
|
||||||
|
if (!groupName) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return await db.getSetMembers('group:' + groupName + ':pending');
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,123 +1,99 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async = require('async');
|
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.leave = function (groupNames, uid, callback) {
|
Groups.leave = async function (groupNames, uid) {
|
||||||
callback = callback || function () {};
|
|
||||||
|
|
||||||
if (Array.isArray(groupNames) && !groupNames.length) {
|
if (Array.isArray(groupNames) && !groupNames.length) {
|
||||||
return setImmediate(callback);
|
return;
|
||||||
}
|
}
|
||||||
if (!Array.isArray(groupNames)) {
|
if (!Array.isArray(groupNames)) {
|
||||||
groupNames = [groupNames];
|
groupNames = [groupNames];
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const [isMembers, exists] = await Promise.all([
|
||||||
function (next) {
|
Groups.isMemberOfGroups(uid, groupNames),
|
||||||
async.parallel({
|
Groups.exists(groupNames),
|
||||||
isMembers: async.apply(Groups.isMemberOfGroups, uid, groupNames),
|
]);
|
||||||
exists: async.apply(Groups.exists, groupNames),
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
groupNames = groupNames.filter(function (groupName, index) {
|
|
||||||
return result.isMembers[index] && result.exists[index];
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!groupNames.length) {
|
const groupsToLeave = groupNames.filter((groupName, index) => isMembers[index] && exists[index]);
|
||||||
return callback();
|
if (!groupsToLeave.length) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([
|
await Promise.all([
|
||||||
async.apply(db.sortedSetRemove, groupNames.map(groupName => 'group:' + groupName + ':members'), uid),
|
db.sortedSetRemove(groupsToLeave.map(groupName => 'group:' + groupName + ':members'), uid),
|
||||||
async.apply(db.setRemove, groupNames.map(groupName => 'group:' + groupName + ':owners'), uid),
|
db.setRemove(groupsToLeave.map(groupName => 'group:' + groupName + ':owners'), uid),
|
||||||
async.apply(db.decrObjectField, groupNames.map(groupName => 'group:' + groupName), 'memberCount'),
|
db.decrObjectField(groupsToLeave.map(groupName => 'group:' + groupName), 'memberCount'),
|
||||||
], next);
|
]);
|
||||||
},
|
|
||||||
function (results, next) {
|
Groups.clearCache(uid, groupsToLeave);
|
||||||
Groups.clearCache(uid, groupNames);
|
|
||||||
Groups.getGroupsFields(groupNames, ['name', 'hidden', 'memberCount'], next);
|
const groupData = await Groups.getGroupsFields(groupsToLeave, ['name', 'hidden', 'memberCount']);
|
||||||
},
|
|
||||||
function (groupData, next) {
|
|
||||||
if (!groupData) {
|
if (!groupData) {
|
||||||
return callback();
|
return;
|
||||||
}
|
}
|
||||||
var tasks = [];
|
|
||||||
|
|
||||||
var emptyPrivilegeGroups = groupData.filter(function (groupData) {
|
const emptyPrivilegeGroups = groupData.filter(g => g && Groups.isPrivilegeGroup(g.name) && g.memberCount === 0);
|
||||||
return groupData && Groups.isPrivilegeGroup(groupData.name) && groupData.memberCount === 0;
|
const visibleGroups = groupData.filter(g => g && !g.hidden);
|
||||||
});
|
|
||||||
|
const promises = [];
|
||||||
if (emptyPrivilegeGroups.length) {
|
if (emptyPrivilegeGroups.length) {
|
||||||
tasks.push(async.apply(Groups.destroy, emptyPrivilegeGroups));
|
promises.push(Groups.destroy, emptyPrivilegeGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
var visibleGroups = groupData.filter(groupData => groupData && !groupData.hidden);
|
|
||||||
if (visibleGroups.length) {
|
if (visibleGroups.length) {
|
||||||
tasks.push(async.apply(db.sortedSetAdd, 'groups:visible:memberCount', visibleGroups.map(groupData => groupData.memberCount), visibleGroups.map(groupData => groupData.name)));
|
promises.push(db.sortedSetAdd, 'groups:visible:memberCount',
|
||||||
|
visibleGroups.map(groupData => groupData.memberCount),
|
||||||
|
visibleGroups.map(groupData => groupData.name)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel(tasks, function (err) {
|
await Promise.all(promises);
|
||||||
next(err);
|
|
||||||
});
|
await clearGroupTitleIfSet(groupsToLeave, uid);
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
clearGroupTitleIfSet(groupNames, uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('action:group.leave', {
|
plugins.fireHook('action:group.leave', {
|
||||||
groupNames: groupNames,
|
groupNames: groupsToLeave,
|
||||||
uid: uid,
|
uid: uid,
|
||||||
});
|
});
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function clearGroupTitleIfSet(groupNames, uid, callback) {
|
async function clearGroupTitleIfSet(groupNames, uid) {
|
||||||
groupNames = groupNames.filter(function (groupName) {
|
groupNames = groupNames.filter(groupName => groupName !== 'registered-users' && !Groups.isPrivilegeGroup(groupName));
|
||||||
return groupName !== 'registered-users' && !Groups.isPrivilegeGroup(groupName);
|
|
||||||
});
|
|
||||||
if (!groupNames.length) {
|
if (!groupNames.length) {
|
||||||
return callback();
|
return;
|
||||||
|
}
|
||||||
|
const userData = await user.getUserData(uid);
|
||||||
|
if (!userData) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
user.getUserData(uid, next);
|
|
||||||
},
|
|
||||||
function (userData, next) {
|
|
||||||
var newTitleArray = userData.groupTitleArray.filter(function (groupTitle) {
|
|
||||||
return !groupNames.includes(groupTitle);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
const newTitleArray = userData.groupTitleArray.filter(groupTitle => !groupNames.includes(groupTitle));
|
||||||
if (newTitleArray.length) {
|
if (newTitleArray.length) {
|
||||||
db.setObjectField('user:' + uid, 'groupTitle', JSON.stringify(newTitleArray), next);
|
await db.setObjectField('user:' + uid, 'groupTitle', JSON.stringify(newTitleArray));
|
||||||
} else {
|
} else {
|
||||||
db.deleteObjectField('user:' + uid, 'groupTitle', next);
|
await db.deleteObjectField('user:' + uid, 'groupTitle');
|
||||||
}
|
}
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Groups.leaveAllGroups = function (uid, callback) {
|
Groups.leaveAllGroups = async function (uid) {
|
||||||
async.waterfall([
|
const groups = await db.getSortedSetRange('groups:createtime', 0, -1);
|
||||||
function (next) {
|
await Promise.all([
|
||||||
db.getSortedSetRange('groups:createtime', 0, -1, next);
|
Groups.leave(groups, uid),
|
||||||
},
|
Groups.rejectMembership(groups, uid),
|
||||||
function (groups, next) {
|
]);
|
||||||
async.parallel([
|
};
|
||||||
function (next) {
|
|
||||||
Groups.leave(groups, uid, next);
|
Groups.kick = async function (uid, groupName, isOwner) {
|
||||||
},
|
if (isOwner) {
|
||||||
function (next) {
|
// If the owners set only contains one member, error out!
|
||||||
Groups.rejectMembership(groups, uid, next);
|
const numOwners = await db.setCount('group:' + groupName + ':owners');
|
||||||
},
|
if (numOwners <= 1) {
|
||||||
], next);
|
throw new Error('[[error:group-needs-owner]]');
|
||||||
},
|
}
|
||||||
], callback);
|
}
|
||||||
|
await Groups.leave(groupName, uid);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,234 +1,91 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const _ = require('lodash');
|
||||||
var _ = require('lodash');
|
|
||||||
|
|
||||||
var user = require('../user');
|
const db = require('../database');
|
||||||
var utils = require('../utils');
|
const user = require('../user');
|
||||||
var plugins = require('../plugins');
|
|
||||||
var notifications = require('../notifications');
|
|
||||||
var db = require('../database');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.requestMembership = function (groupName, uid, callback) {
|
Groups.getMembers = async function (groupName, start, stop) {
|
||||||
async.waterfall([
|
return await db.getSortedSetRevRange('group:' + groupName + ':members', start, stop);
|
||||||
async.apply(inviteOrRequestMembership, groupName, uid, 'request'),
|
};
|
||||||
function (next) {
|
|
||||||
user.getUserField(uid, 'username', next);
|
Groups.getMemberUsers = async function (groupNames, start, stop) {
|
||||||
},
|
async function get(groupName) {
|
||||||
function (username, next) {
|
const uids = await Groups.getMembers(groupName, start, stop);
|
||||||
async.parallel({
|
return await user.getUsersFields(uids, ['uid', 'username', 'picture', 'userslug']);
|
||||||
notification: function (next) {
|
|
||||||
notifications.create({
|
|
||||||
type: 'group-request-membership',
|
|
||||||
bodyShort: '[[groups:request.notification_title, ' + username + ']]',
|
|
||||||
bodyLong: '[[groups:request.notification_text, ' + username + ', ' + groupName + ']]',
|
|
||||||
nid: 'group:' + groupName + ':uid:' + uid + ':request',
|
|
||||||
path: '/groups/' + utils.slugify(groupName),
|
|
||||||
from: uid,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
owners: function (next) {
|
|
||||||
Groups.getOwners(groupName, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (!results.notification || !results.owners.length) {
|
|
||||||
return next();
|
|
||||||
}
|
}
|
||||||
notifications.push(results.notification, results.owners, next);
|
return await Promise.all(groupNames.map(name => get(name)));
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.acceptMembership = function (groupName, uid, callback) {
|
Groups.getMembersOfGroups = async function (groupNames) {
|
||||||
async.waterfall([
|
return await db.getSortedSetsMembers(groupNames.map(name => 'group:' + name + ':members'));
|
||||||
async.apply(db.setsRemove, ['group:' + groupName + ':pending', 'group:' + groupName + ':invited'], uid),
|
|
||||||
async.apply(Groups.join, groupName, uid),
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.rejectMembership = function (groupNames, uid, callback) {
|
Groups.isMember = async function (uid, groupName) {
|
||||||
if (!Array.isArray(groupNames)) {
|
|
||||||
groupNames = [groupNames];
|
|
||||||
}
|
|
||||||
var sets = [];
|
|
||||||
groupNames.forEach(function (groupName) {
|
|
||||||
sets.push('group:' + groupName + ':pending', 'group:' + groupName + ':invited');
|
|
||||||
});
|
|
||||||
|
|
||||||
db.setsRemove(sets, uid, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.invite = function (groupName, uid, callback) {
|
|
||||||
async.waterfall([
|
|
||||||
async.apply(inviteOrRequestMembership, groupName, uid, 'invite'),
|
|
||||||
async.apply(notifications.create, {
|
|
||||||
type: 'group-invite',
|
|
||||||
bodyShort: '[[groups:invited.notification_title, ' + groupName + ']]',
|
|
||||||
bodyLong: '',
|
|
||||||
nid: 'group:' + groupName + ':uid:' + uid + ':invite',
|
|
||||||
path: '/groups/' + utils.slugify(groupName),
|
|
||||||
}),
|
|
||||||
function (notification, next) {
|
|
||||||
notifications.push(notification, [uid], next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
function inviteOrRequestMembership(groupName, uid, type, callback) {
|
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
|
||||||
return callback(new Error('[[error:not-logged-in]]'));
|
|
||||||
}
|
|
||||||
var hookName = type === 'invite' ? 'action:group.inviteMember' : 'action:group.requestMembership';
|
|
||||||
var set = type === 'invite' ? 'group:' + groupName + ':invited' : 'group:' + groupName + ':pending';
|
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
exists: async.apply(Groups.exists, groupName),
|
|
||||||
isMember: async.apply(Groups.isMember, uid, groupName),
|
|
||||||
isPending: async.apply(Groups.isPending, uid, groupName),
|
|
||||||
isInvited: async.apply(Groups.isInvited, uid, groupName),
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (checks, next) {
|
|
||||||
if (!checks.exists) {
|
|
||||||
return next(new Error('[[error:no-group]]'));
|
|
||||||
} else if (checks.isMember) {
|
|
||||||
return callback();
|
|
||||||
} else if (type === 'invite' && checks.isInvited) {
|
|
||||||
return callback();
|
|
||||||
} else if (type === 'request' && checks.isPending) {
|
|
||||||
return next(new Error('[[error:group-already-requested]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
db.setAdd(set, uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook(hookName, {
|
|
||||||
groupName: groupName,
|
|
||||||
uid: uid,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
Groups.getMembers = function (groupName, start, stop, callback) {
|
|
||||||
db.getSortedSetRevRange('group:' + groupName + ':members', start, stop, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.getMemberUsers = function (groupNames, start, stop, callback) {
|
|
||||||
async.map(groupNames, function (groupName, next) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
Groups.getMembers(groupName, start, stop, next);
|
|
||||||
},
|
|
||||||
function (uids, next) {
|
|
||||||
user.getUsersFields(uids, ['uid', 'username', 'picture', 'userslug'], next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.getMembersOfGroups = function (groupNames, callback) {
|
|
||||||
db.getSortedSetsMembers(groupNames.map(name => 'group:' + name + ':members'), callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.isMember = function (uid, groupName, callback) {
|
|
||||||
if (!uid || parseInt(uid, 10) <= 0 || !groupName) {
|
if (!uid || parseInt(uid, 10) <= 0 || !groupName) {
|
||||||
return setImmediate(callback, null, false);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cacheKey = uid + ':' + groupName;
|
const cacheKey = uid + ':' + groupName;
|
||||||
var isMember = Groups.cache.get(cacheKey);
|
let isMember = Groups.cache.get(cacheKey);
|
||||||
if (isMember !== undefined) {
|
if (isMember !== undefined) {
|
||||||
Groups.cache.hits += 1;
|
Groups.cache.hits += 1;
|
||||||
return setImmediate(callback, null, isMember);
|
return isMember;
|
||||||
}
|
}
|
||||||
Groups.cache.misses += 1;
|
Groups.cache.misses += 1;
|
||||||
async.waterfall([
|
isMember = await db.isSortedSetMember('group:' + groupName + ':members', uid);
|
||||||
function (next) {
|
|
||||||
db.isSortedSetMember('group:' + groupName + ':members', uid, next);
|
|
||||||
},
|
|
||||||
function (isMember, next) {
|
|
||||||
Groups.cache.set(cacheKey, isMember);
|
Groups.cache.set(cacheKey, isMember);
|
||||||
next(null, isMember);
|
return isMember;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMembers = function (uids, groupName, callback) {
|
Groups.isMembers = async function (uids, groupName) {
|
||||||
var cachedData = {};
|
|
||||||
function getFromCache(next) {
|
|
||||||
setImmediate(next, null, uids.map(uid => cachedData[uid + ':' + groupName]));
|
|
||||||
}
|
|
||||||
if (!groupName || !uids.length) {
|
if (!groupName || !uids.length) {
|
||||||
return setImmediate(callback, null, uids.map(() => false));
|
return uids.map(() => false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (groupName === 'guests') {
|
if (groupName === 'guests') {
|
||||||
return setImmediate(callback, null, uids.map(uid => parseInt(uid, 10) === 0));
|
return uids.map(uid => parseInt(uid, 10) === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var nonCachedUids = uids.filter(uid => filterNonCached(cachedData, uid, groupName));
|
const cachedData = {};
|
||||||
|
const nonCachedUids = uids.filter(uid => filterNonCached(cachedData, uid, groupName));
|
||||||
|
|
||||||
if (!nonCachedUids.length) {
|
if (!nonCachedUids.length) {
|
||||||
return getFromCache(callback);
|
return uids.map(uid => cachedData[uid + ':' + groupName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const isMembers = await db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids);
|
||||||
function (next) {
|
|
||||||
db.isSortedSetMembers('group:' + groupName + ':members', nonCachedUids, next);
|
|
||||||
},
|
|
||||||
function (isMembers, next) {
|
|
||||||
nonCachedUids.forEach(function (uid, index) {
|
nonCachedUids.forEach(function (uid, index) {
|
||||||
cachedData[uid + ':' + groupName] = isMembers[index];
|
cachedData[uid + ':' + groupName] = isMembers[index];
|
||||||
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
||||||
});
|
});
|
||||||
|
return uids.map(uid => cachedData[uid + ':' + groupName]);
|
||||||
getFromCache(next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMemberOfGroups = function (uid, groups, callback) {
|
Groups.isMemberOfGroups = async function (uid, groups) {
|
||||||
var cachedData = {};
|
|
||||||
function getFromCache(next) {
|
|
||||||
setImmediate(next, null, groups.map(groupName => cachedData[uid + ':' + groupName]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!uid || parseInt(uid, 10) <= 0 || !groups.length) {
|
if (!uid || parseInt(uid, 10) <= 0 || !groups.length) {
|
||||||
return callback(null, groups.map(groupName => groupName === 'guests'));
|
return groups.map(groupName => groupName === 'guests');
|
||||||
}
|
}
|
||||||
|
const cachedData = {};
|
||||||
var nonCachedGroups = groups.filter(groupName => filterNonCached(cachedData, uid, groupName));
|
const nonCachedGroups = groups.filter(groupName => filterNonCached(cachedData, uid, groupName));
|
||||||
|
|
||||||
if (!nonCachedGroups.length) {
|
if (!nonCachedGroups.length) {
|
||||||
return getFromCache(callback);
|
return groups.map(groupName => cachedData[uid + ':' + groupName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
const nonCachedGroupsMemberSets = nonCachedGroups.map(groupName => 'group:' + groupName + ':members');
|
const nonCachedGroupsMemberSets = nonCachedGroups.map(groupName => 'group:' + groupName + ':members');
|
||||||
db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, next);
|
const isMembers = await db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid);
|
||||||
},
|
|
||||||
function (isMembers, next) {
|
|
||||||
nonCachedGroups.forEach(function (groupName, index) {
|
nonCachedGroups.forEach(function (groupName, index) {
|
||||||
cachedData[uid + ':' + groupName] = isMembers[index];
|
cachedData[uid + ':' + groupName] = isMembers[index];
|
||||||
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
||||||
});
|
});
|
||||||
|
|
||||||
getFromCache(next);
|
return groups.map(groupName => cachedData[uid + ':' + groupName]);
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function filterNonCached(cachedData, uid, groupName) {
|
function filterNonCached(cachedData, uid, groupName) {
|
||||||
var isMember = Groups.cache.get(uid + ':' + groupName);
|
const isMember = Groups.cache.get(uid + ':' + groupName);
|
||||||
var isInCache = isMember !== undefined;
|
const isInCache = isMember !== undefined;
|
||||||
if (isInCache) {
|
if (isInCache) {
|
||||||
Groups.cache.hits += 1;
|
Groups.cache.hits += 1;
|
||||||
cachedData[uid + ':' + groupName] = isMember;
|
cachedData[uid + ':' + groupName] = isMember;
|
||||||
@@ -238,106 +95,55 @@ module.exports = function (Groups) {
|
|||||||
return !isInCache;
|
return !isInCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
Groups.isMemberOfAny = function (uid, groups, callback) {
|
Groups.isMemberOfAny = async function (uid, groups) {
|
||||||
if (!groups.length) {
|
if (!groups.length) {
|
||||||
return setImmediate(callback, null, false);
|
return false;
|
||||||
}
|
}
|
||||||
async.waterfall([
|
const isMembers = await Groups.isMemberOfGroups(uid, groups);
|
||||||
function (next) {
|
return isMembers.includes(true);
|
||||||
Groups.isMemberOfGroups(uid, groups, next);
|
|
||||||
},
|
|
||||||
function (isMembers, next) {
|
|
||||||
next(null, isMembers.some(isMember => !!isMember));
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getMemberCount = function (groupName, callback) {
|
Groups.getMemberCount = async function (groupName) {
|
||||||
async.waterfall([
|
const count = await db.getObjectField('group:' + groupName, 'memberCount');
|
||||||
function (next) {
|
return parseInt(count, 10);
|
||||||
db.getObjectField('group:' + groupName, 'memberCount', next);
|
|
||||||
},
|
|
||||||
function (count, next) {
|
|
||||||
next(null, parseInt(count, 10));
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMemberOfGroupList = function (uid, groupListKey, callback) {
|
Groups.isMemberOfGroupList = async function (uid, groupListKey) {
|
||||||
async.waterfall([
|
let groupNames = await db.getSortedSetRange('group:' + groupListKey + ':members', 0, -1);
|
||||||
function (next) {
|
|
||||||
db.getSortedSetRange('group:' + groupListKey + ':members', 0, -1, next);
|
|
||||||
},
|
|
||||||
function (groupNames, next) {
|
|
||||||
groupNames = Groups.removeEphemeralGroups(groupNames);
|
groupNames = Groups.removeEphemeralGroups(groupNames);
|
||||||
if (!groupNames.length) {
|
if (!groupNames.length) {
|
||||||
return callback(null, false);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Groups.isMemberOfGroups(uid, groupNames, next);
|
const isMembers = await Groups.isMemberOfGroups(uid, groupNames);
|
||||||
},
|
return isMembers.includes(true);
|
||||||
function (isMembers, next) {
|
|
||||||
next(null, isMembers.includes(true));
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMemberOfGroupsList = function (uid, groupListKeys, callback) {
|
Groups.isMemberOfGroupsList = async function (uid, groupListKeys) {
|
||||||
var uniqueGroups;
|
|
||||||
var members;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
const sets = groupListKeys.map(groupName => 'group:' + groupName + ':members');
|
const sets = groupListKeys.map(groupName => 'group:' + groupName + ':members');
|
||||||
db.getSortedSetsMembers(sets, next);
|
const members = await db.getSortedSetsMembers(sets);
|
||||||
},
|
|
||||||
function (_members, next) {
|
let uniqueGroups = _.uniq(_.flatten(members));
|
||||||
members = _members;
|
|
||||||
uniqueGroups = _.uniq(_.flatten(members));
|
|
||||||
uniqueGroups = Groups.removeEphemeralGroups(uniqueGroups);
|
uniqueGroups = Groups.removeEphemeralGroups(uniqueGroups);
|
||||||
|
|
||||||
Groups.isMemberOfGroups(uid, uniqueGroups, next);
|
const isMembers = await Groups.isMemberOfGroups(uid, uniqueGroups);
|
||||||
},
|
const isGroupMember = _.zipObject(uniqueGroups, isMembers);
|
||||||
function (isMembers, next) {
|
|
||||||
var map = {};
|
|
||||||
|
|
||||||
uniqueGroups.forEach(function (groupName, index) {
|
return members.map(function (groupNames) {
|
||||||
map[groupName] = isMembers[index];
|
return !!groupNames.find(name => isGroupMember[name]);
|
||||||
});
|
});
|
||||||
|
|
||||||
var result = members.map(function (groupNames) {
|
|
||||||
for (var i = 0; i < groupNames.length; i += 1) {
|
|
||||||
if (map[groupNames[i]]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
next(null, result);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMembersOfGroupList = function (uids, groupListKey, callback) {
|
Groups.isMembersOfGroupList = async function (uids, groupListKey) {
|
||||||
var groupNames;
|
const results = uids.map(() => false);
|
||||||
var results = uids.map(() => false);
|
|
||||||
|
|
||||||
async.waterfall([
|
let groupNames = await db.getSortedSetRange('group:' + groupListKey + ':members', 0, -1);
|
||||||
function (next) {
|
groupNames = Groups.removeEphemeralGroups(groupNames);
|
||||||
db.getSortedSetRange('group:' + groupListKey + ':members', 0, -1, next);
|
if (!groupNames.length) {
|
||||||
},
|
return results;
|
||||||
function (_groupNames, next) {
|
|
||||||
groupNames = Groups.removeEphemeralGroups(_groupNames);
|
|
||||||
|
|
||||||
if (groupNames.length === 0) {
|
|
||||||
return callback(null, results);
|
|
||||||
}
|
}
|
||||||
|
const isGroupMembers = await Promise.all(groupNames.map(name => Groups.isMembers(uids, name)));
|
||||||
|
|
||||||
async.map(groupNames, function (groupName, next) {
|
|
||||||
Groups.isMembers(uids, groupName, next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (isGroupMembers, next) {
|
|
||||||
isGroupMembers.forEach(function (isMembers) {
|
isGroupMembers.forEach(function (isMembers) {
|
||||||
results.forEach(function (isMember, index) {
|
results.forEach(function (isMember, index) {
|
||||||
if (!isMember && isMembers[index]) {
|
if (!isMember && isMembers[index]) {
|
||||||
@@ -345,48 +151,6 @@ module.exports = function (Groups) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
next(null, results);
|
return results;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.isInvited = function (uid, groupName, callback) {
|
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
|
||||||
return setImmediate(callback, null, false);
|
|
||||||
}
|
|
||||||
db.isSetMember('group:' + groupName + ':invited', uid, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.isPending = function (uid, groupName, callback) {
|
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
|
||||||
return setImmediate(callback, null, false);
|
|
||||||
}
|
|
||||||
db.isSetMember('group:' + groupName + ':pending', uid, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.getPending = function (groupName, callback) {
|
|
||||||
if (!groupName) {
|
|
||||||
return setImmediate(callback, null, []);
|
|
||||||
}
|
|
||||||
db.getSetMembers('group:' + groupName + ':pending', callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.kick = function (uid, groupName, isOwner, callback) {
|
|
||||||
if (isOwner) {
|
|
||||||
// If the owners set only contains one member, error out!
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
db.setCount('group:' + groupName + ':owners', next);
|
|
||||||
},
|
|
||||||
function (numOwners, next) {
|
|
||||||
if (numOwners <= 1) {
|
|
||||||
return next(new Error('[[error:group-needs-owner]]'));
|
|
||||||
}
|
|
||||||
Groups.leave(groupName, uid, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
} else {
|
|
||||||
Groups.leave(groupName, uid, callback);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user