feat: optimize copy privileges

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 8aef689f05
commit 0dca13e9c2

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var _ = require('lodash');
var db = require('../database'); var db = require('../database');
var groups = require('../groups'); var groups = require('../groups');
@ -222,63 +223,52 @@ module.exports = function (Categories) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
plugins.fireHook('filter:categories.copyPrivilegesFrom', { plugins.fireHook('filter:categories.copyPrivilegesFrom', {
privileges: privileges.privilegeList.slice(), privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(),
fromCid: fromCid, fromCid: fromCid,
toCid: toCid, toCid: toCid,
group: group, group: group,
}, next); }, next);
}, },
function (data, next) { function (data, next) {
async.each(data.privileges, function (privilege, next) { if (group) {
if (group) { copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group, next);
copyPrivilegeByGroup(privilege, data.fromCid, data.toCid, group, next); } else {
} else { copyPrivileges(data.privileges, data.fromCid, data.toCid, next);
copyPrivilege(privilege, data.fromCid, data.toCid, next); }
}
}, next);
}, },
], callback); ], callback);
}; };
function copyPrivilege(privilege, fromCid, toCid, callback) { function copyPrivileges(privileges, fromCid, toCid, callback) {
const toGroups = privileges.map(privilege => 'group:cid:' + toCid + ':privileges:' + privilege + ':members');
const fromGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
async.waterfall([ async.waterfall([
function (next) { function (next) {
db.getSortedSetRange('group:cid:' + toCid + ':privileges:' + privilege + ':members', 0, -1, next); db.getSortedSetsMembers(toGroups.concat(fromGroups), next);
}, },
function (currentMembers, next) { function (currentMembers, next) {
async.eachSeries(currentMembers, function (member, next) { const copyGroups = _.uniq(_.flatten(currentMembers));
groups.leave('cid:' + toCid + ':privileges:' + privilege, member, next); async.each(copyGroups, function (group, next) {
}, next); copyPrivilegesByGroup(privileges, fromCid, toCid, group, next);
},
function (next) {
db.getSortedSetRange('group:cid:' + fromCid + ':privileges:' + privilege + ':members', 0, -1, next);
},
function (members, next) {
if (!members || !members.length) {
return callback();
}
async.eachSeries(members, function (member, next) {
groups.join('cid:' + toCid + ':privileges:' + privilege, member, next);
}, next); }, next);
}, },
], callback); ], callback);
} }
function copyPrivilegeByGroup(privilege, fromCid, toCid, group, callback) { function copyPrivilegesByGroup(privileges, fromCid, toCid, group, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
groups.leave('cid:' + toCid + ':privileges:' + privilege, group, next); const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
groups.leave(leaveGroups, group, next);
}, },
function (next) { function (next) {
db.isSortedSetMember('group:cid:' + fromCid + ':privileges:' + privilege + ':members', group, next); const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
db.isMemberOfSortedSets(checkGroups, group, next);
}, },
function (isMember, next) { function (isMembers, next) {
if (!isMember) { privileges = privileges.filter((priv, index) => isMembers[index]);
return callback(); const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
} groups.join(joinGroups, group, next);
groups.join('cid:' + toCid + ':privileges:' + privilege, group, next);
}, },
], callback); ], callback);
} }

@ -14,7 +14,9 @@ module.exports = function (Groups) {
if (!groupNames) { if (!groupNames) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
if (Array.isArray(groupNames) && !groupNames.length) {
return setImmediate(callback);
}
if (!Array.isArray(groupNames)) { if (!Array.isArray(groupNames)) {
groupNames = [groupNames]; groupNames = [groupNames];
} }

@ -10,6 +10,9 @@ module.exports = function (Groups) {
Groups.leave = function (groupNames, uid, callback) { Groups.leave = function (groupNames, uid, callback) {
callback = callback || function () {}; callback = callback || function () {};
if (Array.isArray(groupNames) && !groupNames.length) {
return setImmediate(callback);
}
if (!Array.isArray(groupNames)) { if (!Array.isArray(groupNames)) {
groupNames = [groupNames]; groupNames = [groupNames];
} }

Loading…
Cancel
Save