mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
WIP commit -- groups refactoring + upgrade scripts
This commit is contained in:
119
src/groups.js
119
src/groups.js
@@ -2,22 +2,25 @@
|
|||||||
|
|
||||||
(function(Groups) {
|
(function(Groups) {
|
||||||
|
|
||||||
|
/* REMOVED
|
||||||
|
Groups.getGidFromName
|
||||||
|
Groups.joinByGroupName
|
||||||
|
Groups.leaveByGroupName
|
||||||
|
*/
|
||||||
|
|
||||||
var async = require('async'),
|
var async = require('async'),
|
||||||
winston = require('winston'),
|
winston = require('winston'),
|
||||||
user = require('./user'),
|
user = require('./user'),
|
||||||
db = require('./database');
|
db = require('./database');
|
||||||
|
|
||||||
Groups.getGroupIds = function (callback) {
|
|
||||||
db.getObjectValues('group:gid', callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.list = function(options, callback) {
|
Groups.list = function(options, callback) {
|
||||||
db.getObjectValues('group:gid', function (err, gids) {
|
db.getSetMembers('groups', function (err, groupNames) {
|
||||||
if (gids.length > 0) {
|
if (groupNames.length > 0) {
|
||||||
async.map(gids, function (gid, next) {
|
async.map(groupNames, function (groupName, next) {
|
||||||
Groups.get(gid, options, next);
|
Groups.get(groupName, options, next);
|
||||||
}, function (err, groups) {
|
}, function (err, groups) {
|
||||||
// Remove system, hidden, or deleted groups from this list
|
// Remove system, hidden, or deleted groups from this list
|
||||||
|
if (!options.showAllGroups) {
|
||||||
groups = groups.filter(function (group) {
|
groups = groups.filter(function (group) {
|
||||||
if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) {
|
if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -25,6 +28,7 @@
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
callback(err, groups);
|
callback(err, groups);
|
||||||
});
|
});
|
||||||
@@ -34,16 +38,16 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.get = function(gid, options, callback) {
|
Groups.get = function(groupName, options, callback) {
|
||||||
var truncated = false,
|
var truncated = false,
|
||||||
numUsers;
|
numUsers;
|
||||||
|
|
||||||
async.parallel({
|
async.parallel({
|
||||||
base: function (next) {
|
base: function (next) {
|
||||||
db.getObject('gid:' + gid, next);
|
db.getObject('group:' + groupName, next);
|
||||||
},
|
},
|
||||||
users: function (next) {
|
users: function (next) {
|
||||||
db.getSetMembers('gid:' + gid + ':members', function (err, uids) {
|
db.getSetMembers('group:' + groupName + ':members', function (err, uids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@@ -82,6 +86,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.getByGroupName = function(groupName, options, callback) {
|
Groups.getByGroupName = function(groupName, options, callback) {
|
||||||
Groups.getGidFromName(groupName, function(err, gid) {
|
Groups.getGidFromName(groupName, function(err, gid) {
|
||||||
if (err || !gid) {
|
if (err || !gid) {
|
||||||
@@ -92,6 +97,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.getMemberships = function(uid, callback) {
|
Groups.getMemberships = function(uid, callback) {
|
||||||
if (!uid) {
|
if (!uid) {
|
||||||
return callback(new Error('no-uid-specified'));
|
return callback(new Error('no-uid-specified'));
|
||||||
@@ -110,26 +116,18 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.isDeleted = function(gid, callback) {
|
Groups.isDeleted = function(gid, callback) {
|
||||||
db.getObjectField('gid:' + gid, 'deleted', function(err, deleted) {
|
db.getObjectField('gid:' + gid, 'deleted', function(err, deleted) {
|
||||||
callback(err, parseInt(deleted, 10) === 1);
|
callback(err, parseInt(deleted, 10) === 1);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getGidFromName = function(name, callback) {
|
Groups.isMember = function(uid, groupName, callback) {
|
||||||
db.getObjectField('group:gid', name, callback);
|
db.isSetMember('group:' + groupName + ':members', uid, callback);
|
||||||
};
|
|
||||||
|
|
||||||
Groups.isMember = function(uid, gid, callback) {
|
|
||||||
Groups.isDeleted(gid, function(err, deleted) {
|
|
||||||
if (!deleted) {
|
|
||||||
db.isSetMember('gid:' + gid + ':members', uid, callback);
|
|
||||||
} else {
|
|
||||||
callback(err, false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.isMemberByGroupName = function(uid, groupName, callback) {
|
Groups.isMemberByGroupName = function(uid, groupName, callback) {
|
||||||
Groups.getGidFromName(groupName, function(err, gid) {
|
Groups.getGidFromName(groupName, function(err, gid) {
|
||||||
if (err || !gid) {
|
if (err || !gid) {
|
||||||
@@ -142,6 +140,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.isMemberOfGroupAny = function(uid, groupListKey, callback) {
|
Groups.isMemberOfGroupAny = function(uid, groupListKey, callback) {
|
||||||
Groups.getGidFromName(groupListKey, function(err, gid) {
|
Groups.getGidFromName(groupListKey, function(err, gid) {
|
||||||
if (err || !gid) {
|
if (err || !gid) {
|
||||||
@@ -164,12 +163,14 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.isEmpty = function(gid, callback) {
|
Groups.isEmpty = function(gid, callback) {
|
||||||
db.setCount('gid:' + gid + ':members', function(err, numMembers) {
|
db.setCount('gid:' + gid + ':members', function(err, numMembers) {
|
||||||
callback(err, numMembers === 0);
|
callback(err, numMembers === 0);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.isEmptyByGroupName = function(groupName, callback) {
|
Groups.isEmptyByGroupName = function(groupName, callback) {
|
||||||
Groups.getGidFromName(groupName, function(err, gid) {
|
Groups.getGidFromName(groupName, function(err, gid) {
|
||||||
if (err || !gid) {
|
if (err || !gid) {
|
||||||
@@ -180,6 +181,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.exists = function(name, callback) {
|
Groups.exists = function(name, callback) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
exists: function(next) {
|
exists: function(next) {
|
||||||
@@ -195,6 +197,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.create = function(name, description, callback) {
|
Groups.create = function(name, description, callback) {
|
||||||
if (name.length === 0) {
|
if (name.length === 0) {
|
||||||
return callback(new Error('name-too-short'));
|
return callback(new Error('name-too-short'));
|
||||||
@@ -231,12 +234,14 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.hide = function(gid, callback) {
|
Groups.hide = function(gid, callback) {
|
||||||
Groups.update(gid, {
|
Groups.update(gid, {
|
||||||
hidden: '1'
|
hidden: '1'
|
||||||
}, callback);
|
}, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.update = function(gid, values, callback) {
|
Groups.update = function(gid, values, callback) {
|
||||||
db.exists('gid:' + gid, function (err, exists) {
|
db.exists('gid:' + gid, function (err, exists) {
|
||||||
if (!err && exists) {
|
if (!err && exists) {
|
||||||
@@ -268,47 +273,57 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.destroy = function(gid, callback) {
|
Groups.destroy = function(groupName, callback) {
|
||||||
db.setObjectField('gid:' + gid, 'deleted', '1', callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.join = function(gid, uid, callback) {
|
|
||||||
db.setAdd('gid:' + gid + ':members', uid, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.joinByGroupName = function(groupName, uid, callback) {
|
|
||||||
Groups.getGidFromName(groupName, function(err, gid) {
|
|
||||||
if (err || !gid) {
|
|
||||||
Groups.create(groupName, '', function(err, groupObj) {
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(next) {
|
function(next) {
|
||||||
Groups.hide(groupObj.gid, next);
|
db.delete('group:' + groupName, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
Groups.join(groupObj.gid, uid, next);
|
db.setRemove('groups', groupName, next);
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
});
|
};
|
||||||
|
|
||||||
|
Groups.join = function(groupName, uid, callback) {
|
||||||
|
db.setAdd('group:' + groupName + ':members', uid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Groups.leave = function(groupName, uid, callback) {
|
||||||
|
db.setRemove('group:' + groupName + ':members', uid, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a system group, and it is now empty, delete it
|
||||||
|
Groups.get(groupName, function(err, group) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (group.system && group.memberCount === 0) {
|
||||||
|
Groups.destroy(groupName, callback);
|
||||||
} else {
|
} else {
|
||||||
Groups.join(gid, uid, callback);
|
return callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
Groups.leave = function(gid, uid, callback) {
|
|
||||||
db.setRemove('gid:' + gid + ':members', uid, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Groups.leaveByGroupName = function(groupName, uid, callback) {
|
|
||||||
Groups.getGidFromName(groupName, function(err, gid) {
|
|
||||||
if (err || !gid) {
|
|
||||||
callback(new Error('gid-not-found'));
|
|
||||||
} else {
|
|
||||||
Groups.leave(gid, uid, callback);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Groups.leaveAllGroups = function(uid, callback) {
|
||||||
|
db.getSetMembers('groups', function(err, groups) {
|
||||||
|
async.each(groups, function(groupName, next) {
|
||||||
|
Groups.isMember(uid, groupName, function(err, isMember) {
|
||||||
|
if (!err && isMember) {
|
||||||
|
Groups.leave(groupName, uid, next);
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Not checked
|
||||||
Groups.prune = function(callback) {
|
Groups.prune = function(callback) {
|
||||||
// Actually deletes groups (with the deleted flag) from the redis database
|
// Actually deletes groups (with the deleted flag) from the redis database
|
||||||
db.getObjectValues('group:gid', function (err, gids) {
|
db.getObjectValues('group:gid', function (err, gids) {
|
||||||
|
|||||||
@@ -54,7 +54,14 @@ module.exports = function(app, middleware, controllers) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/test', function(req, res) {
|
app.get('/test', function(req, res) {
|
||||||
res.send(200);
|
var groups = require('../groups');
|
||||||
|
|
||||||
|
groups.list({
|
||||||
|
showAllGroups: true
|
||||||
|
}, function(err, groups) {
|
||||||
|
res.json(200, groups);
|
||||||
|
});
|
||||||
|
// res.send(200);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -19,7 +19,7 @@ var db = require('./database'),
|
|||||||
schemaDate, thisSchemaDate,
|
schemaDate, thisSchemaDate,
|
||||||
|
|
||||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
||||||
latestSchema = Date.UTC(2014, 2, 19);
|
latestSchema = Date.UTC(2014, 2, 19, 20);
|
||||||
|
|
||||||
Upgrade.check = function(callback) {
|
Upgrade.check = function(callback) {
|
||||||
db.get('schemaDate', function(err, value) {
|
db.get('schemaDate', function(err, value) {
|
||||||
@@ -355,6 +355,79 @@ Upgrade.upgrade = function(callback) {
|
|||||||
winston.info('[2014/3/19] Setting "system" flag for system groups - skipped');
|
winston.info('[2014/3/19] Setting "system" flag for system groups - skipped');
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
thisSchemaDate = Date.UTC(2014, 2, 19, 20);
|
||||||
|
|
||||||
|
if (schemaDate < thisSchemaDate) {
|
||||||
|
db.getObject('group:gid', function(err, mapping) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err && !mapping) {
|
||||||
|
// Done already, skip
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
var names = Object.keys(mapping);
|
||||||
|
async.each(names, function(name, next) {
|
||||||
|
async.series([
|
||||||
|
function(next) {
|
||||||
|
// Remove the gid from the hash
|
||||||
|
db.deleteObjectField('gid:' + mapping[name], 'gid', next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
db.rename('gid:' + mapping[name], 'group:' + name, next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
db.exists('gid:' + mapping[name] + ':members', function(err, exists) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
db.rename('gid:' + mapping[name] + ':members', 'group:' + name + ':members', next);
|
||||||
|
} else {
|
||||||
|
// No members, do nothing
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
// Add groups to a directory (set)
|
||||||
|
db.setAdd('groups', name, next);
|
||||||
|
}
|
||||||
|
], next);
|
||||||
|
}, function(err) {
|
||||||
|
// Delete the old mapping key
|
||||||
|
async.series([
|
||||||
|
function(next) {
|
||||||
|
db.delete('group:gid', next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
// Delete empty groups
|
||||||
|
Groups.list({ showAllGroups: true }, function(err, groups) {
|
||||||
|
async.each(groups, function(group, next) {
|
||||||
|
if (group.members.length === 0) {
|
||||||
|
// Delete the group
|
||||||
|
Groups.destroy(group.name, next);
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}, next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
], function(err) {
|
||||||
|
console.log('so far so good');
|
||||||
|
process.exit();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
winston.info('[2014/3/19] Removing gids and pruning groups - skipped');
|
||||||
|
next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Add new schema updates here
|
// Add new schema updates here
|
||||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!
|
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ module.exports = function(User) {
|
|||||||
deleteUserFromFollowers(uid, next);
|
deleteUserFromFollowers(uid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
deleteUserFromGroups(uid, next);
|
groups.leaveAllGroups(uid, next);
|
||||||
}
|
}
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -320,12 +320,4 @@ module.exports = function(User) {
|
|||||||
}, callback);
|
}, callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteUserFromGroups(uid, callback) {
|
|
||||||
groups.getGroupIds(function(err, gids) {
|
|
||||||
async.each(gids, function(gid, next) {
|
|
||||||
groups.leave(gid, uid, next);
|
|
||||||
}, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user