Skip to content

Node.js based forum software built for the modern web

updating all group lib methods, and their callers -- #1252

Julian Lam7b4f596aba78
diff --git a/src/admin/user.js b/src/admin/user.js
index 387988fa6b..daf6685aca 100644
--- a/src/admin/user.js
+++ b/src/admin/user.js
@@ -21,17 +21,15 @@ var async = require('async'),
UserAdmin.makeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(err, isAdmin) {
if (isAdmin) {
- groups.getGidFromName('administrators', function(err, gid) {
- groups.join(gid, theirid, function(err) {
- if (!err) {
- socket.emit('event:alert', {
- title: 'User Modified',
- message: 'This user is now an administrator!',
- type: 'success',
- timeout: 2000
- });
- }
- });
+ groups.join('administrators', theirid, function(err) {
+ if (!err) {
+ socket.emit('event:alert', {
+ title: 'User Modified',
+ message: 'This user is now an administrator!',
+ type: 'success',
+ timeout: 2000
+ });
+ }
});
} else {
socket.emit('event:alert', {
@@ -47,18 +45,16 @@ var async = require('async'),
UserAdmin.removeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(err, isAdmin) {
if (isAdmin) {
- groups.getGidFromName('administrators', function(err, gid) {
- groups.leave(gid, theirid, function(err) {
- if (!err) {
+ groups.leave('administrators', theirid, function(err) {
+ if (!err) {
- socket.emit('event:alert', {
- title: 'User Modified',
- message: 'This user is no longer an administrator!',
- type: 'success',
- timeout: 2000
- });
- }
- });
+ socket.emit('event:alert', {
+ title: 'User Modified',
+ message: 'This user is no longer an administrator!',
+ type: 'success',
+ timeout: 2000
+ });
+ }
});
}
});
diff --git a/src/categories.js b/src/categories.js
index 368f293794..e04b8c56cc 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -182,7 +182,7 @@ var db = require('./database'),
};
Categories.getModerators = function(cid, callback) {
- Groups.getByGroupName('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
+ Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
if (!err) {
if (groupObj.members && groupObj.members.length) {
user.getMultipleUserFields(groupObj.members, ['uid', 'username', 'userslug', 'picture'], function(err, moderators) {
diff --git a/src/categoryTools.js b/src/categoryTools.js
index 454d87128b..be76b77372 100644
--- a/src/categoryTools.js
+++ b/src/categoryTools.js
@@ -1,3 +1,5 @@
+"use strict";
+
var Groups = require('./groups'),
User = require('./user'),
@@ -16,19 +18,9 @@ CategoryTools.privileges = function(cid, uid, callback) {
var key = 'cid:' + cid + ':privileges:+r';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberByGroupName(uid, key, next);
- },
- isEmpty: function(next) {
- Groups.isEmptyByGroupName(key, next);
- }
- }, next);
+ Groups.isMember(uid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, true);
}
});
},
@@ -36,19 +28,9 @@ CategoryTools.privileges = function(cid, uid, callback) {
var key = 'cid:' + cid + ':privileges:+w';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberByGroupName(uid, key, next);
- },
- isEmpty: function(next) {
- Groups.isEmptyByGroupName(key, next);
- }
- }, next);
+ Groups.isMember(uid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, true);
}
});
},
@@ -56,19 +38,9 @@ CategoryTools.privileges = function(cid, uid, callback) {
var key = 'cid:' + cid + ':privileges:g+r';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberOfGroupAny(uid, key, next);
- },
- isEmpty: function(next) {
- Groups.isEmptyByGroupName(key, next);
- }
- }, next);
+ Groups.isMemberOfGroupList(uid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, true);
}
});
},
@@ -76,19 +48,9 @@ CategoryTools.privileges = function(cid, uid, callback) {
var key = 'cid:' + cid + ':privileges:g+w';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberOfGroupAny(uid, key, next);
- },
- isEmpty: function(next) {
- Groups.isEmptyByGroupName(key, next);
- }
- }, next);
+ Groups.isMemberOfGroupList(uid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, true);
}
});
},
@@ -100,25 +62,17 @@ CategoryTools.privileges = function(cid, uid, callback) {
}
}, function(err, privileges) {
callback(err, !privileges ? null : {
- "+r": privileges['+r'].isMember,
- "+w": privileges['+w'].isMember,
- "g+r": privileges['g+r'].isMember,
- "g+w": privileges['g+w'].isMember,
+ "+r": privileges['+r'],
+ "+w": privileges['+w'],
+ "g+r": privileges['g+r'],
+ "g+w": privileges['g+w'],
read: (
- (
- (privileges['+r'].isMember || privileges['+r'].isEmpty) &&
- (privileges['g+r'].isMember || privileges['g+r'].isEmpty)
- ) ||
- privileges.moderator ||
- privileges.admin
+ privileges['+r'] || privileges['g+r'] ||
+ privileges.moderator || privileges.admin
),
write: (
- (
- (privileges['+w'].isMember || privileges['+w'].isEmpty) &&
- (privileges['g+w'].isMember || privileges['g+w'].isEmpty)
- ) ||
- privileges.moderator ||
- privileges.admin
+ privileges['+w'] || privileges['g+w'] ||
+ privileges.moderator || privileges.admin
),
editable: privileges.moderator || privileges.admin,
view_deleted: privileges.moderator || privileges.admin,
@@ -134,16 +88,9 @@ CategoryTools.groupPrivileges = function(cid, gid, callback) {
var key = 'cid:' + cid + ':privileges:g+r';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberByGroupName(gid, key, next);
- }
- }, next);
+ Groups.isMember(gid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, false);
}
});
},
@@ -151,23 +98,16 @@ CategoryTools.groupPrivileges = function(cid, gid, callback) {
var key = 'cid:' + cid + ':privileges:g+w';
Groups.exists(key, function(err, exists) {
if (exists) {
- async.parallel({
- isMember: function(next) {
- Groups.isMemberByGroupName(gid, key, next);
- }
- }, next);
+ Groups.isMember(gid, key, next);
} else {
- next(null, {
- isMember: false,
- isEmpty: true
- });
+ next(null, false);
}
});
}
}, function(err, privileges) {
callback(err, !privileges ? null : {
- "g+r": privileges['g+r'].isMember,
- "g+w": privileges['g+w'].isMember
+ "g+r": privileges['g+r'],
+ "g+w": privileges['g+w']
});
});
};
diff --git a/src/groups.js b/src/groups.js
index 888c2969d6..0bc16f1df0 100644
--- a/src/groups.js
+++ b/src/groups.js
@@ -3,10 +3,7 @@
(function(Groups) {
/* REMOVED
- Groups.getGidFromName
- Groups.joinByGroupName
- Groups.leaveByGroupName
- Groups.prune
+ group lists need to be updated to contain... groups!
*/
var async = require('async'),
@@ -87,118 +84,30 @@
});
};
- // Not checked
- Groups.getByGroupName = function(groupName, options, callback) {
- Groups.getGidFromName(groupName, function(err, gid) {
- if (err || !gid) {
- callback(new Error('gid-not-found'));
- } else {
- Groups.get(gid, options, callback);
- }
- });
- };
-
- // Not checked
- Groups.getMemberships = function(uid, callback) {
- if (!uid) {
- return callback(new Error('no-uid-specified'));
- }
-
- db.getObjectValues('group:gid', function(err, gids) {
- async.filter(gids, function(gid, next) {
- Groups.isMember(uid, gid, function(err, isMember) {
- next(isMember);
- });
- }, function(gids) {
- async.map(gids, function(gid, next) {
- Groups.get(gid, {}, next);
- }, callback);
- });
- });
- };
-
- // Not checked
- Groups.isDeleted = function(gid, callback) {
- db.getObjectField('gid:' + gid, 'deleted', function(err, deleted) {
- callback(err, parseInt(deleted, 10) === 1);
- });
- };
-
Groups.isMember = function(uid, groupName, callback) {
db.isSetMember('group:' + groupName + ':members', uid, callback);
};
- // Not checked
- Groups.isMemberByGroupName = function(uid, groupName, callback) {
- Groups.getGidFromName(groupName, function(err, gid) {
- if (err || !gid) {
- callback(null, false);
- } else {
+ Groups.isMemberOfGroupList = function(uid, groupListKey, callback) {
+ db.getSetMembers('group:' + groupListKey + ':members', function(err, gids) {
+ async.some(gids, function(gid, next) {
Groups.isMember(uid, gid, function(err, isMember) {
- callback(err, !!isMember);
- });
- }
- });
- };
-
- // Not checked
- Groups.isMemberOfGroupAny = function(uid, groupListKey, callback) {
- Groups.getGidFromName(groupListKey, function(err, gid) {
- if (err || !gid) {
- return callback(new Error('error-checking-group'));
- }
-
- db.getSetMembers('gid:' + gid + ':members', function(err, gids) {
- async.some(gids, function(gid, next) {
- Groups.isMember(uid, gid, function(err, isMember) {
- if (!err && isMember) {
- next(true);
- } else {
- next(false);
- }
- });
- }, function(result) {
- callback(null, result);
+ if (!err && isMember) {
+ next(true);
+ } else {
+ next(false);
+ }
});
+ }, function(result) {
+ callback(null, result);
});
});
};
- // Not checked
- Groups.isEmpty = function(gid, callback) {
- db.setCount('gid:' + gid + ':members', function(err, numMembers) {
- callback(err, numMembers === 0);
- });
- };
-
- // Not checked
- Groups.isEmptyByGroupName = function(groupName, callback) {
- Groups.getGidFromName(groupName, function(err, gid) {
- if (err || !gid) {
- callback(new Error('gid-not-found'));
- } else {
- Groups.isEmpty(gid, callback);
- }
- });
- };
-
- // Not checked
Groups.exists = function(name, callback) {
- async.parallel({
- exists: function(next) {
- db.isObjectField('group:gid', name, next);
- },
- deleted: function(next) {
- Groups.getGidFromName(name, function(err, gid) {
- Groups.isDeleted(gid, next);
- });
- }
- }, function(err, results) {
- callback(err, !results ? null : (results.exists && !results.deleted));
- });
+ db.isSetMember('groups', name, callback);
};
- // Not checked
Groups.create = function(name, description, callback) {
if (name.length === 0) {
return callback(new Error('name-too-short'));
@@ -210,24 +119,16 @@
Groups.exists(name, function (err, exists) {
if (!exists) {
- db.incrObjectField('global', 'nextGid', function (err, gid) {
- db.setObjectField('group:gid', name, gid, function(err) {
-
- var groupData = {
- gid: gid,
- name: name,
- description: description,
- deleted: '0',
- hidden: '0',
- system: system ? '1' : '0'
- };
-
- db.setObject('gid:' + gid, groupData, function(err) {
-
- Groups.get(gid, {}, callback);
-
- });
- });
+ var groupData = {
+ name: name,
+ description: description,
+ deleted: '0',
+ hidden: '0',
+ system: system ? '1' : '0'
+ };
+
+ db.setObject('group:' + name, groupData, function(err) {
+ Groups.get(name, {}, callback);
});
} else {
callback(new Error('group-exists'));
@@ -235,36 +136,34 @@
});
};
- // Not checked
- Groups.hide = function(gid, callback) {
- Groups.update(gid, {
+ Groups.hide = function(groupName, callback) {
+ Groups.update(groupName, {
hidden: '1'
}, callback);
};
- // Not checked
- Groups.update = function(gid, values, callback) {
- db.exists('gid:' + gid, function (err, exists) {
+ Groups.update = function(groupName, values, callback) {
+ db.exists('group:' + groupName, function (err, exists) {
if (!err && exists) {
- // If the group was renamed, check for dupes, fix the assoc. hash
+ // If the group was renamed, check for dupes
if (values.name) {
Groups.exists(values.name, function(err, exists) {
if (!exists) {
- Groups.get(gid, {}, function(err, groupObj) {
+ db.rename('group:' + groupName, 'group:' + values.name, function(err) {
if (err) {
- return callback(new Error('group-not-found'));
+ return callback(new Error('could-not-rename-group'));
}
- db.deleteObjectField('group:gid', groupObj.name);
- db.setObjectField('group:gid', values.name, gid);
- db.setObject('gid:' + gid, values, callback);
+ db.setRemove('groups', groupName);
+ db.setAdd('groups', values.name);
+ db.setObject('group:' + values.name, values, callback);
});
} else {
callback(new Error('group-exists'));
}
});
} else {
- db.setObject('gid:' + gid, values, callback);
+ db.setObject('group:' + groupName, values, callback);
}
} else {
if (callback) {
diff --git a/src/install.js b/src/install.js
index c4dead89cc..94e22f2582 100644
--- a/src/install.js
+++ b/src/install.js
@@ -290,24 +290,14 @@ var async = require('async'),
function (next) {
// Check if an administrator needs to be created
var Groups = require('./groups');
-
- Groups.getGidFromName('administrators', function (err, gid) {
- if (err) {
- return next(err.message);
- }
-
- if (gid) {
- Groups.get(gid, {}, function (err, groupObj) {
- if (groupObj.count > 0) {
- winston.info('Administrator found, skipping Admin setup');
- next();
- } else {
- install.createAdmin(next);
- }
- });
- } else {
- install.createAdmin(next);
- }
+ Groups.get('administrators', {}, function (err, groupObj) {
+ if (groupObj.memberCount > 0) {
+ winston.info('Administrator found, skipping Admin setup');
+ next();
+ } else {
+ install.createAdmin(next);
+ }
+ });
});
},
function (next) {
@@ -417,7 +407,7 @@ var async = require('async'),
return callback(new Error('invalid-values'));
}
- Groups.joinByGroupName('administrators', uid, callback);
+ Groups.join('administrators', uid, callback);
});
},
retryPassword = function (originalResults) {
diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js
index 003ed793d3..2be696c957 100644
--- a/src/socket.io/admin.js
+++ b/src/socket.io/admin.js
@@ -198,16 +198,16 @@ SocketAdmin.categories.setPrivilege = function(socket, data, callback) {
};
if (set) {
- groups.joinByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb);
+ groups.join('cid:' + cid + ':privileges:' + privilege, uid, cb);
} else {
- groups.leaveByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb);
+ groups.leave('cid:' + cid + ':privileges:' + privilege, uid, cb);
}
};
SocketAdmin.categories.getPrivilegeSettings = function(socket, cid, callback) {
async.parallel({
"+r": function(next) {
- groups.getByGroupName('cid:' + cid + ':privileges:+r', { expand: true }, function(err, groupObj) {
+ groups.get('cid:' + cid + ':privileges:+r', { expand: true }, function(err, groupObj) {
if (!err) {
next.apply(this, arguments);
} else {
@@ -218,7 +218,7 @@ SocketAdmin.categories.getPrivilegeSettings = function(socket, cid, callback) {
});
},
"+w": function(next) {
- groups.getByGroupName('cid:' + cid + ':privileges:+w', { expand: true }, function(err, groupObj) {
+ groups.get('cid:' + cid + ':privileges:+w', { expand: true }, function(err, groupObj) {
if (!err) {
next.apply(this, arguments);
} else {
@@ -229,7 +229,7 @@ SocketAdmin.categories.getPrivilegeSettings = function(socket, cid, callback) {
});
},
"mods": function(next) {
- groups.getByGroupName('cid:' + cid + ':privileges:mods', { expand: true }, function(err, groupObj) {
+ groups.get('cid:' + cid + ':privileges:mods', { expand: true }, function(err, groupObj) {
if (!err) {
next.apply(this, arguments);
} else {
@@ -259,9 +259,9 @@ SocketAdmin.categories.setGroupPrivilege = function(socket, data, callback) {
}
if (data.set) {
- groups.joinByGroupName('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
+ groups.join('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
} else {
- groups.leaveByGroupName('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
+ groups.leave('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
}
};
diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js
deleted file mode 100644
index 368a48f709..0000000000
--- a/src/socket.io/groups.js
+++ /dev/null
@@ -1,11 +0,0 @@
-var Groups = require('../groups'),
-
- SocketGroups = {};
-
-SocketGroups.getMemberships = function(socket, data, callback) {
- if (data && data.uid) {
- Groups.getMemberships(data.uid, callback);
- }
-};
-
-module.exports = SocketGroups;
\ No newline at end of file
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index 6723d16684..22539b0d4b 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -256,7 +256,7 @@ SocketPosts.flag = function(socket, pid, callback) {
},
function(topicSlug, next) {
path = nconf.get('relative_path') + '/topic/' + topicSlug + '#' + pid;
- groups.getByGroupName('administrators', {}, next);
+ groups.get('administrators', {}, next);
},
function(adminGroup, next) {
notifications.create({
diff --git a/src/upgrade.js b/src/upgrade.js
index 7045090689..f6db77b5e9 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -334,28 +334,6 @@ Upgrade.upgrade = function(callback) {
next();
}
},
- function(next) {
- thisSchemaDate = Date.UTC(2014, 2, 19);
-
- if (schemaDate < thisSchemaDate) {
- async.map(['administrators', 'registered-users'], Groups.getGidFromName, function(err, gids) {
- async.each(gids, function(gid, next) {
- db.setObjectField('gid:' + gid, 'system', '1', next);
- }, function(err) {
- if (err) {
- winston.error('[2014/3/19] Problem setting "system" flag for system groups.');
- next();
- } else {
- winston.info('[2014/3/19] Setting "system" flag for system groups');
- Upgrade.update(thisSchemaDate, next);
- }
- });
- });
- } else {
- winston.info('[2014/3/19] Setting "system" flag for system groups - skipped');
- next();
- }
- },
function(next) {
thisSchemaDate = Date.UTC(2014, 2, 19, 20);
@@ -400,11 +378,22 @@ Upgrade.upgrade = function(callback) {
}
], next);
}, function(err) {
- // Delete the old mapping key
+ // Clean-up
async.series([
function(next) {
+ // Mapping
db.delete('group:gid', next);
},
+ function(next) {
+ // Incrementor
+ db.deleteObjectField('global', 'nextGid', next);
+ },
+ function(next) {
+ // Set 'administrators' and 'registered-users' as system groups
+ db.setObjectField('group:administrators', 'system', '1');
+ db.setObjectField('group:registered-users', 'system', '1');
+ next();
+ },
function(next) {
// Delete empty groups
Groups.list({ showAllGroups: true }, function(err, groups) {
diff --git a/src/user.js b/src/user.js
index 936fe8b111..6706b1c23f 100644
--- a/src/user.js
+++ b/src/user.js
@@ -295,11 +295,11 @@ var bcrypt = require('bcryptjs'),
};
User.isModerator = function(uid, cid, callback) {
- groups.isMemberByGroupName(uid, 'cid:' + cid + ':privileges:mods', callback);
+ groups.isMember(uid, 'cid:' + cid + ':privileges:mods', callback);
};
User.isAdministrator = function(uid, callback) {
- groups.isMemberByGroupName(uid, 'administrators', callback);
+ groups.isMember(uid, 'administrators', callback);
};
User.isOnline = function(uid, callback) {
diff --git a/src/user/create.js b/src/user/create.js
index c6e4ae4771..73c333d27e 100644
--- a/src/user/create.js
+++ b/src/user/create.js
@@ -117,7 +117,7 @@ module.exports = function(User) {
db.sortedSetAdd('users:postcount', 0, uid);
db.sortedSetAdd('users:reputation', 0, uid);
- groups.joinByGroupName('registered-users', uid);
+ groups.join('registered-users', uid);
if (password) {
User.hashPassword(password, function(err, hash) {