diff --git a/src/groupdb.js b/src/groupdb.js index 82690a220..ed802f11f 100644 --- a/src/groupdb.js +++ b/src/groupdb.js @@ -11,6 +11,8 @@ exports = module.exports = { removeMember: removeMember, isMember: isMember, + getGroups: getGroups, + _clear: clear }; @@ -91,6 +93,15 @@ function getMembers(groupId, callback) { }); } +function getGroups(userId, callback) { + database.query('SELECT userId FROM groupMembers WHERE userId=?', [ userId ], function (error, result) { + if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); + // if (result.length === 0) return callback(new DatabaseError(DatabaseError.NOT_FOUND)); // need to differentiate group with no members and invalid groupId + + callback(error, result.map(function (r) { return r.groupId; })); + }); +} + function addMember(groupId, userId, callback) { assert.strictEqual(typeof groupId, 'string'); assert.strictEqual(typeof userId, 'string'); diff --git a/src/groups.js b/src/groups.js index 28fdfb5cb..e8e708594 100644 --- a/src/groups.js +++ b/src/groups.js @@ -14,6 +14,8 @@ exports = module.exports = { removeMember: removeMember, isMember: isMember, + getGroups: getGroups, + ADMIN_GROUP_ID: 'gid:admin' // see db migration code }; @@ -112,6 +114,18 @@ function getMembers(groupId, callback) { }); } +function getGroups(userId, callback) { + assert.strictEqual(typeof userId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + groupdb.getGroups(userId, function (error, result) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new GroupError(GroupError.NOT_FOUND)); + if (error) return callback(new GroupError(GroupError.INTERNAL_ERROR, error)); + + return callback(null, result); + }); +} + function addMember(groupId, userId, callback) { assert.strictEqual(typeof groupId, 'string'); assert.strictEqual(typeof userId, 'string'); diff --git a/src/user.js b/src/user.js index 5ac23c021..5139074eb 100644 --- a/src/user.js +++ b/src/user.js @@ -230,7 +230,13 @@ function getUser(userId, callback) { if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UserError(UserError.NOT_FOUND)); if (error) return callback(new UserError(UserError.INTERNAL_ERROR, error)); - return callback(null, result); + groups.getGroups(userId, function (error, groupIds) { + if (error) return callback(new UserError(UserError.INTERNAL_ERROR, error)); + + result.groupIds = groupIds; + + return callback(null, result); + }); }); }