feat: #7743, groups/delete,ownership,posts,user
parent
5e8614e15b
commit
fcd4445a89
@ -1,80 +1,56 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
const utils = require('../utils');
|
||||||
var utils = require('../utils');
|
const db = require('./../database');
|
||||||
var db = require('./../database');
|
const batch = require('../batch');
|
||||||
var batch = require('../batch');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.destroy = function (groupNames, callback) {
|
Groups.destroy = async function (groupNames) {
|
||||||
if (!Array.isArray(groupNames)) {
|
if (!Array.isArray(groupNames)) {
|
||||||
groupNames = [groupNames];
|
groupNames = [groupNames];
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupsData;
|
let groupsData = await Groups.getGroupsData(groupNames);
|
||||||
async.waterfall([
|
groupsData = groupsData.filter(Boolean);
|
||||||
function (next) {
|
if (!groupsData.length) {
|
||||||
Groups.getGroupsData(groupNames, next);
|
return;
|
||||||
},
|
}
|
||||||
function (_groupsData, next) {
|
const keys = [];
|
||||||
groupsData = _groupsData.filter(Boolean);
|
groupNames.forEach(function (groupName) {
|
||||||
if (!groupsData.length) {
|
keys.push('group:' + groupName,
|
||||||
return callback();
|
'group:' + groupName + ':members',
|
||||||
}
|
'group:' + groupName + ':pending',
|
||||||
|
'group:' + groupName + ':invited',
|
||||||
async.parallel([
|
'group:' + groupName + ':owners',
|
||||||
function (next) {
|
'group:' + groupName + ':member:pids'
|
||||||
var keys = [];
|
);
|
||||||
groupNames.forEach(function (groupName) {
|
});
|
||||||
keys.push('group:' + groupName,
|
const sets = groupNames.map(groupName => groupName.toLowerCase() + ':' + groupName);
|
||||||
'group:' + groupName + ':members',
|
const fields = groupNames.map(groupName => utils.slugify(groupName));
|
||||||
'group:' + groupName + ':pending',
|
|
||||||
'group:' + groupName + ':invited',
|
|
||||||
'group:' + groupName + ':owners',
|
|
||||||
'group:' + groupName + ':member:pids'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
db.deleteAll(keys, next);
|
await Promise.all([
|
||||||
},
|
db.deleteAll(keys),
|
||||||
function (next) {
|
db.sortedSetRemove([
|
||||||
db.sortedSetRemove([
|
'groups:createtime',
|
||||||
'groups:createtime',
|
'groups:visible:createtime',
|
||||||
'groups:visible:createtime',
|
'groups:visible:memberCount',
|
||||||
'groups:visible:memberCount',
|
], groupNames),
|
||||||
], groupNames, next);
|
db.sortedSetRemove('groups:visible:name', sets),
|
||||||
},
|
db.deleteObjectFields('groupslug:groupname', fields),
|
||||||
function (next) {
|
removeGroupsFromPrivilegeGroups(groupNames),
|
||||||
const keys = groupNames.map(groupName => groupName.toLowerCase() + ':' + groupName);
|
]);
|
||||||
db.sortedSetRemove('groups:visible:name', keys, next);
|
Groups.resetCache();
|
||||||
},
|
plugins.fireHook('action:groups.destroy', { groups: groupsData });
|
||||||
function (next) {
|
|
||||||
const fields = groupNames.map(groupName => utils.slugify(groupName));
|
|
||||||
db.deleteObjectFields('groupslug:groupname', fields, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
removeGroupsFromPrivilegeGroups(groupNames, next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
next(err);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
Groups.resetCache();
|
|
||||||
plugins.fireHook('action:groups.destroy', { groups: groupsData });
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function removeGroupsFromPrivilegeGroups(groupNames, callback) {
|
async function removeGroupsFromPrivilegeGroups(groupNames) {
|
||||||
batch.processSortedSet('groups:createtime', function (otherGroups, next) {
|
await batch.processSortedSet('groups:createtime', async function (otherGroups) {
|
||||||
const privilegeGroups = otherGroups.filter(group => Groups.isPrivilegeGroup(group));
|
const privilegeGroups = otherGroups.filter(group => Groups.isPrivilegeGroup(group));
|
||||||
const keys = privilegeGroups.map(group => 'group:' + group + ':members');
|
const keys = privilegeGroups.map(group => 'group:' + group + ':members');
|
||||||
db.sortedSetRemove(keys, groupNames, next);
|
await db.sortedSetRemove(keys, groupNames);
|
||||||
}, {
|
}, {
|
||||||
batch: 500,
|
batch: 500,
|
||||||
}, callback);
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,58 +1,40 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
var db = require('../database');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.ownership = {};
|
Groups.ownership = {};
|
||||||
|
|
||||||
Groups.ownership.isOwner = function (uid, groupName, callback) {
|
Groups.ownership.isOwner = async function (uid, groupName) {
|
||||||
if (!(parseInt(uid, 10) > 0)) {
|
if (!(parseInt(uid, 10) > 0)) {
|
||||||
return setImmediate(callback, null, false);
|
return false;
|
||||||
}
|
}
|
||||||
db.isSetMember('group:' + groupName + ':owners', uid, callback);
|
return await db.isSetMember('group:' + groupName + ':owners', uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.ownership.isOwners = function (uids, groupName, callback) {
|
Groups.ownership.isOwners = async function (uids, groupName) {
|
||||||
if (!Array.isArray(uids)) {
|
if (!Array.isArray(uids)) {
|
||||||
return setImmediate(callback, null, []);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
db.isSetMembers('group:' + groupName + ':owners', uids, callback);
|
return await db.isSetMembers('group:' + groupName + ':owners', uids);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.ownership.grant = function (toUid, groupName, callback) {
|
Groups.ownership.grant = async function (toUid, groupName) {
|
||||||
// Note: No ownership checking is done here on purpose!
|
// Note: No ownership checking is done here on purpose!
|
||||||
async.waterfall([
|
await db.setAdd('group:' + groupName + ':owners', toUid);
|
||||||
function (next) {
|
plugins.fireHook('action:group.grantOwnership', { uid: toUid, groupName: groupName });
|
||||||
db.setAdd('group:' + groupName + ':owners', toUid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('action:group.grantOwnership', { uid: toUid, groupName: groupName });
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.ownership.rescind = function (toUid, groupName, callback) {
|
Groups.ownership.rescind = async function (toUid, groupName) {
|
||||||
// Note: No ownership checking is done here on purpose!
|
// Note: No ownership checking is done here on purpose!
|
||||||
|
|
||||||
// If the owners set only contains one member, error out!
|
// If the owners set only contains one member, error out!
|
||||||
async.waterfall([
|
const numOwners = await db.setCount('group:' + groupName + ':owners');
|
||||||
function (next) {
|
if (numOwners <= 1) {
|
||||||
db.setCount('group:' + groupName + ':owners', next);
|
throw new Error('[[error:group-needs-owner]]');
|
||||||
},
|
}
|
||||||
function (numOwners, next) {
|
db.setRemove('group:' + groupName + ':owners', toUid);
|
||||||
if (numOwners <= 1) {
|
plugins.fireHook('action:group.rescindOwnership', { uid: toUid, groupName: groupName });
|
||||||
return next(new Error('[[error:group-needs-owner]]'));
|
|
||||||
}
|
|
||||||
db.setRemove('group:' + groupName + ':owners', toUid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('action:group.rescindOwnership', { uid: toUid, groupName: groupName });
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,65 +1,36 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
|
const privileges = require('../privileges');
|
||||||
var db = require('../database');
|
const posts = require('../posts');
|
||||||
var privileges = require('../privileges');
|
|
||||||
var posts = require('../posts');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.onNewPostMade = function (postData, callback) {
|
Groups.onNewPostMade = async function (postData) {
|
||||||
if (!parseInt(postData.uid, 10)) {
|
if (!parseInt(postData.uid, 10)) {
|
||||||
return setImmediate(callback);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupNames;
|
let groupNames = await Groups.getUserGroupMembership('groups:visible:createtime', [postData.uid]);
|
||||||
async.waterfall([
|
groupNames = groupNames[0];
|
||||||
function (next) {
|
|
||||||
Groups.getUserGroupMembership('groups:visible:createtime', [postData.uid], next);
|
|
||||||
},
|
|
||||||
function (_groupNames, next) {
|
|
||||||
groupNames = _groupNames[0];
|
|
||||||
|
|
||||||
const keys = groupNames.map(groupName => 'group:' + groupName + ':member:pids');
|
const keys = groupNames.map(groupName => 'group:' + groupName + ':member:pids');
|
||||||
db.sortedSetsAdd(keys, postData.timestamp, postData.pid, next);
|
await db.sortedSetsAdd(keys, postData.timestamp, postData.pid);
|
||||||
},
|
await Promise.all(groupNames.map(name => truncateMemberPosts(name)));
|
||||||
function (next) {
|
|
||||||
async.each(groupNames, function (groupName, next) {
|
|
||||||
truncateMemberPosts(groupName, next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function truncateMemberPosts(groupName, callback) {
|
async function truncateMemberPosts(groupName) {
|
||||||
async.waterfall([
|
let lastPid = await db.getSortedSetRevRange('group:' + groupName + ':member:pids', 10, 10);
|
||||||
function (next) {
|
lastPid = lastPid[0];
|
||||||
db.getSortedSetRevRange('group:' + groupName + ':member:pids', 10, 10, next);
|
if (!parseInt(lastPid, 10)) {
|
||||||
},
|
return;
|
||||||
function (lastPid, next) {
|
}
|
||||||
lastPid = lastPid[0];
|
const score = await db.sortedSetScore('group:' + groupName + ':member:pids', lastPid);
|
||||||
if (!parseInt(lastPid, 10)) {
|
await db.sortedSetsRemoveRangeByScore(['group:' + groupName + ':member:pids'], '-inf', score);
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
db.sortedSetScore('group:' + groupName + ':member:pids', lastPid, next);
|
|
||||||
},
|
|
||||||
function (score, next) {
|
|
||||||
db.sortedSetsRemoveRangeByScore(['group:' + groupName + ':member:pids'], '-inf', score, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Groups.getLatestMemberPosts = function (groupName, max, uid, callback) {
|
Groups.getLatestMemberPosts = async function (groupName, max, uid) {
|
||||||
async.waterfall([
|
let pids = await db.getSortedSetRevRange('group:' + groupName + ':member:pids', 0, max - 1);
|
||||||
function (next) {
|
pids = await privileges.posts.filter('topics:read', pids, uid);
|
||||||
db.getSortedSetRevRange('group:' + groupName + ':member:pids', 0, max - 1, next);
|
return await posts.getPostSummaryByPids(pids, uid, { stripTags: false });
|
||||||
},
|
|
||||||
function (pids, next) {
|
|
||||||
privileges.posts.filter('topics:read', pids, uid, next);
|
|
||||||
},
|
|
||||||
function (pids, next) {
|
|
||||||
posts.getPostSummaryByPids(pids, uid, { stripTags: false }, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,71 +1,34 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
|
const user = require('../user');
|
||||||
var db = require('../database');
|
|
||||||
var user = require('../user');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.getUsersFromSet = function (set, fields, callback) {
|
Groups.getUsersFromSet = async function (set, fields) {
|
||||||
if (typeof fields === 'function') {
|
const uids = await db.getSetMembers(set);
|
||||||
callback = fields;
|
|
||||||
fields = null;
|
if (fields) {
|
||||||
|
return await user.getUsersFields(uids, fields);
|
||||||
}
|
}
|
||||||
async.waterfall([
|
return await user.getUsersData(uids);
|
||||||
function (next) {
|
|
||||||
db.getSetMembers(set, next);
|
|
||||||
},
|
|
||||||
function (uids, next) {
|
|
||||||
if (fields) {
|
|
||||||
user.getUsersFields(uids, fields, callback);
|
|
||||||
} else {
|
|
||||||
user.getUsersData(uids, next);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getUserGroups = function (uids, callback) {
|
Groups.getUserGroups = async function (uids) {
|
||||||
Groups.getUserGroupsFromSet('groups:visible:createtime', uids, callback);
|
return await Groups.getUserGroupsFromSet('groups:visible:createtime', uids);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getUserGroupsFromSet = function (set, uids, callback) {
|
Groups.getUserGroupsFromSet = async function (set, uids) {
|
||||||
async.waterfall([
|
const memberOf = await Groups.getUserGroupMembership(set, uids);
|
||||||
function (next) {
|
return await Promise.all(memberOf.map(memberOf => Groups.getGroupsData(memberOf)));
|
||||||
Groups.getUserGroupMembership(set, uids, next);
|
|
||||||
},
|
|
||||||
function (memberOf, next) {
|
|
||||||
async.map(memberOf, function (memberOf, next) {
|
|
||||||
Groups.getGroupsData(memberOf, next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getUserGroupMembership = function (set, uids, callback) {
|
Groups.getUserGroupMembership = async function (set, uids) {
|
||||||
async.waterfall([
|
const groupNames = await db.getSortedSetRevRange(set, 0, -1);
|
||||||
function (next) {
|
return await Promise.all(uids.map(uid => findUserGroups(uid, groupNames)));
|
||||||
db.getSortedSetRevRange(set, 0, -1, next);
|
|
||||||
},
|
|
||||||
function (groupNames, next) {
|
|
||||||
async.map(uids, function (uid, next) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
Groups.isMemberOfGroups(uid, groupNames, next);
|
|
||||||
},
|
|
||||||
function (isMembers, next) {
|
|
||||||
var memberOf = [];
|
|
||||||
isMembers.forEach(function (isMember, index) {
|
|
||||||
if (isMember) {
|
|
||||||
memberOf.push(groupNames[index]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
next(null, memberOf);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function findUserGroups(uid, groupNames) {
|
||||||
|
const isMembers = await Groups.isMemberOfGroups(uid, groupNames);
|
||||||
|
return groupNames.filter((name, i) => isMembers[i]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue