mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-05-07 14:16:25 +02:00
closes #6937
This commit is contained in:
@@ -73,6 +73,18 @@ Groups.getGroupsFromSet = function (set, uid, start, stop, callback) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
Groups.getNonPrivilegeGroups = function (set, start, stop, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRevRange(set, start, stop, next);
|
||||
},
|
||||
function (groupNames, next) {
|
||||
groupNames = groupNames.concat(Groups.ephemeralGroups).filter(groupName => !Groups.isPrivilegeGroup(groupName));
|
||||
Groups.getGroupsData(groupNames, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Groups.getGroups = function (set, start, stop, callback) {
|
||||
db.getSortedSetRevRange(set, start, stop, callback);
|
||||
};
|
||||
|
||||
@@ -164,18 +164,17 @@ module.exports = function (Groups) {
|
||||
Groups.isMembers = function (uids, groupName, callback) {
|
||||
var cachedData = {};
|
||||
function getFromCache() {
|
||||
setImmediate(callback, null, uids.map(function (uid) {
|
||||
return cachedData[uid + ':' + groupName];
|
||||
}));
|
||||
setImmediate(callback, null, uids.map(uid => cachedData[uid + ':' + groupName]));
|
||||
}
|
||||
|
||||
if (!groupName || !uids.length) {
|
||||
return callback(null, uids.map(function () { return false; }));
|
||||
return setImmediate(callback, null, uids.map(() => false));
|
||||
}
|
||||
|
||||
var nonCachedUids = uids.filter(function (uid) {
|
||||
return filterNonCached(cachedData, uid, groupName);
|
||||
});
|
||||
if (groupName === 'guests') {
|
||||
return setImmediate(callback, null, uids.map(uid => parseInt(uid, 10) === 0));
|
||||
}
|
||||
|
||||
var nonCachedUids = uids.filter(uid => filterNonCached(cachedData, uid, groupName));
|
||||
|
||||
if (!nonCachedUids.length) {
|
||||
return getFromCache(callback);
|
||||
@@ -196,6 +195,38 @@ module.exports = function (Groups) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
Groups.isMemberOfGroups = function (uid, groups, callback) {
|
||||
var cachedData = {};
|
||||
function getFromCache(next) {
|
||||
setImmediate(next, null, groups.map(groupName => cachedData[uid + ':' + groupName]));
|
||||
}
|
||||
|
||||
if (!uid || parseInt(uid, 10) <= 0 || !groups.length) {
|
||||
return callback(null, groups.map(groupName => groupName === 'guests'));
|
||||
}
|
||||
|
||||
var nonCachedGroups = groups.filter(groupName => filterNonCached(cachedData, uid, groupName));
|
||||
|
||||
if (!nonCachedGroups.length) {
|
||||
return getFromCache(callback);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
const nonCachedGroupsMemberSets = nonCachedGroups.map(groupName => 'group:' + groupName + ':members');
|
||||
db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, next);
|
||||
},
|
||||
function (isMembers, next) {
|
||||
nonCachedGroups.forEach(function (groupName, index) {
|
||||
cachedData[uid + ':' + groupName] = isMembers[index];
|
||||
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
||||
});
|
||||
|
||||
getFromCache(next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
function filterNonCached(cachedData, uid, groupName) {
|
||||
var isMember = Groups.cache.get(uid + ':' + groupName);
|
||||
var isInCache = isMember !== undefined;
|
||||
@@ -208,41 +239,16 @@ module.exports = function (Groups) {
|
||||
return !isInCache;
|
||||
}
|
||||
|
||||
Groups.isMemberOfGroups = function (uid, groups, callback) {
|
||||
var cachedData = {};
|
||||
function getFromCache(next) {
|
||||
setImmediate(next, null, groups.map(function (groupName) {
|
||||
return cachedData[uid + ':' + groupName];
|
||||
}));
|
||||
Groups.isMemberOfAny = function (uid, groups, callback) {
|
||||
if (!groups.length) {
|
||||
return setImmediate(callback, null, false);
|
||||
}
|
||||
|
||||
if (!uid || parseInt(uid, 10) <= 0 || !groups.length) {
|
||||
return callback(null, groups.map(function () { return false; }));
|
||||
}
|
||||
|
||||
var nonCachedGroups = groups.filter(function (groupName) {
|
||||
return filterNonCached(cachedData, uid, groupName);
|
||||
});
|
||||
|
||||
if (!nonCachedGroups.length) {
|
||||
return getFromCache(callback);
|
||||
}
|
||||
|
||||
var nonCachedGroupsMemberSets = nonCachedGroups.map(function (groupName) {
|
||||
return 'group:' + groupName + ':members';
|
||||
});
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.isMemberOfSortedSets(nonCachedGroupsMemberSets, uid, next);
|
||||
Groups.isMemberOfGroups(uid, groups, next);
|
||||
},
|
||||
function (isMembers, next) {
|
||||
nonCachedGroups.forEach(function (groupName, index) {
|
||||
cachedData[uid + ':' + groupName] = isMembers[index];
|
||||
Groups.cache.set(uid + ':' + groupName, isMembers[index]);
|
||||
});
|
||||
|
||||
getFromCache(next);
|
||||
next(null, isMembers.some(isMember => !!isMember));
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
@@ -318,10 +324,7 @@ module.exports = function (Groups) {
|
||||
|
||||
Groups.isMembersOfGroupList = function (uids, groupListKey, callback) {
|
||||
var groupNames;
|
||||
var results = [];
|
||||
uids.forEach(function () {
|
||||
results.push(false);
|
||||
});
|
||||
var results = uids.map(() => false);
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
|
||||
Reference in New Issue
Block a user