Skip to content

Instantly share code, notes, and snippets.

@jcspencer
Created January 3, 2014 00:12
Show Gist options
  • Save jcspencer/8229916 to your computer and use it in GitHub Desktop.
Save jcspencer/8229916 to your computer and use it in GitHub Desktop.
Redis social graph in node.js, with following and blocking
var redis = require('redis').createClient(),
f = require('util').format;
Array.prototype.diff = function(a) { // use to remove blocked users
return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};
var methods = {
follow: function(a, b, cb) {
db.sadd(f("node:%s:following", a), b, function(err, res){
db.sadd(f("node:%s:followers", b), a, function(e, re) {
if(err || e) {
return cb(err || e);
} else {
return cb(null);
}
});
});
},
unfollow: function(a, b, cb) {
db.srem(f("node:%s:following", a), b, function(err, res){
db.srem(f("node:%s:followers", b), a, function(e, re) {
if(err || e) {
return cb(err || e);
} else {
return cb(null);
}
});
});
},
block: function(a, b, cb) {
db.sadd(f("node:%s:blocked", a), b, function(err, res) {
db.saad(f("node:%s:blockers", b), a, function(e, re) {
if(err || e) {
return cb(err || e);
} else {
return cb(null);
}
});
});
},
unblock: function(a, b, cb) {
db.srem(f("node:%s:blocked", a), b, function(err, res) {
db.srem(f("node:%s:blockers", b), a, function(e, re) {
if(err || e) {
return cb(err || e);
} else {
return cb(null);
}
});
});
},
following: function(a,l,cb,u) {
db.sort([f("node:%s:following", a), "ALPHA", "LIMIT", l.offset || l.o || l.off || 0, l.limit || l.l || l.lim || 25], function(err, res) {
if(u) {
this.blocks(u, {o:0,l:-1}, function(e,r) {
return cb(err, res.diff(r))
});
} else {
return cb(err, res);
}
});
},
followers: function(a,l,cb,u) {
db.sort([f("node:%s:followers", a), "ALPHA", "LIMIT", l.offset || l.o || l.off || 0, l.limit || l.l || l.lim || 25], function(err, res) {
//console.log([f("node:%s:followers", a), "ALPHA", limitStr])
if(u) {
this.blocks(u, {o:0,l:-1}, function(e,r) {
return cb(err, res.diff(r))
});
} else {
return cb(err, res);
}
});
},
blocks: function(a,l,cb,u) {
db.sort([f("node:%s:blocks", a), "ALPHA", "LIMIT", l.offset || l.o || l.off || 0, l.limit || l.l || l.lim || 25], function(err, res) {
return cb(err, res);
});
},
blockers: function(a,l,cb,u) {
db.sort([f("node:%s:blockers", a), "ALPHA", "LIMIT", l.offset || l.o || l.off || 0, l.limit || l.l || l.lim || 25], function(err, res) {
return cb(err, res);
});
},
friends: function(a,cb,u) {
db.sinter([f("node:%s:following", a), f("node:%s:followers", a)], function(err, res) {
if(u) {
this.blocks(u, {o:0,l:-1}, function(e,r) {
return cb(err, res.diff(r))
});
} else {
return cb(err, res);
}
});
},
followingCount: function(a,cb) {
db.scard(f("node:%s:following", a), function(err, res) {
return cb(err, res);
});
},
followerCount: function(a,cb) {
db.scard(f("node:%s:followers", a), function(err, res) {
return cb(err, res);
});
},
friendsCount: function(a,cb) {
this.friends(a, function(err, r) {
return cb(err, r.length);
});
},
isFollowing: function(a,b,cb) {
db.sismember([f("node:%s:following", a), b], function(err, res) {
return cb(err, res);
});
}
};
module.exports = methods;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment