psychobunny 11 years ago
parent 62e2aa67d7
commit 46d6d7637e

@ -1,11 +1,11 @@
(function () { (function(Groups) {
"use strict"; "use strict";
var async = require('async'), var async = require('async'),
User = require('./user'), User = require('./user'),
RDB = RDB || require('./redis'), RDB = RDB || require('./redis');
Groups = {
list: function (options, callback) { Groups.list = function(options, callback) {
RDB.hvals('group:gid', function (err, gids) { RDB.hvals('group:gid', function (err, gids) {
if (gids.length > 0) { if (gids.length > 0) {
async.map(gids, function (gid, next) { async.map(gids, function (gid, next) {
@ -25,8 +25,9 @@
callback(null, []); callback(null, []);
} }
}); });
}, };
get: function (gid, options, callback) {
Groups.get = function(gid, options, callback) {
async.parallel({ async.parallel({
base: function (next) { base: function (next) {
RDB.hgetall('gid:' + gid, next); RDB.hgetall('gid:' + gid, next);
@ -60,17 +61,21 @@
callback(err, results.base); callback(err, results.base);
}); });
}, };
getGidFromName: function (name, callback) {
Groups.getGidFromName = function(name, callback) {
RDB.hget('group:gid', name, callback); RDB.hget('group:gid', name, callback);
}, };
isMember: function (uid, gid, callback) {
Groups.isMember = function(uid, gid, callback) {
RDB.sismember('gid:' + gid + ':members', uid, callback); RDB.sismember('gid:' + gid + ':members', uid, callback);
}, };
exists: function (name, callback) {
Groups.exists = function(name, callback) {
RDB.hexists('group:gid', name, callback); RDB.hexists('group:gid', name, callback);
}, };
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'));
} }
@ -94,8 +99,9 @@
callback(new Error('group-exists')); callback(new Error('group-exists'));
} }
}); });
}, };
update: function (gid, values, callback) {
Groups.update = function(gid, values, callback) {
RDB.exists('gid:' + gid, function (err, exists) { RDB.exists('gid:' + gid, function (err, exists) {
if (!err && exists) { if (!err && exists) {
RDB.hmset('gid:' + gid, values, callback); RDB.hmset('gid:' + gid, values, callback);
@ -103,19 +109,20 @@
callback(new Error('gid-not-found')); callback(new Error('gid-not-found'));
} }
}); });
}, };
destroy: function (gid, callback) {
Groups.destroy = function(gid, callback) {
if (gid !== 1) { if (gid !== 1) {
RDB.hset('gid:' + gid, 'deleted', '1', callback); RDB.hset('gid:' + gid, 'deleted', '1', callback);
} }
}, };
join: function (gid, uid, callback) {
Groups.join = function(gid, uid, callback) {
RDB.sadd('gid:' + gid + ':members', uid, callback); RDB.sadd('gid:' + gid + ':members', uid, callback);
}, };
leave: function (gid, uid, callback) {
Groups.leave = function(gid, uid, callback) {
RDB.srem('gid:' + gid + ':members', uid, callback); RDB.srem('gid:' + gid + ':members', uid, callback);
}
}; };
module.exports = Groups; }(module.exports));
}());

Loading…
Cancel
Save