Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created August 10, 2010 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurokikaze/853bd6e4d3f40e942588 to your computer and use it in GitHub Desktop.
Save kurokikaze/853bd6e4d3f40e942588 to your computer and use it in GitHub Desktop.
Твиттер-клон с использованием Node.js и Redis
var sys = require('sys'),
client = require('./redis-client').createClient(),
hash = require('./hashlib');
// set globals
/*client.set('global:nextUserId', 1000, function() {
client.set('global:nextPostId', 1000, function(a) {
sys.puts('Global variables ready: ' + a);
});
});*/
var ritter = {
registerUser: function(username, password, callback) {
// check username availability
client.incr('global:nextUserId', function(err, uid) {
if (uid) {
sys.puts('Uid is ' + uid);
client.get('username:' + username + ':id', function(err, id) {
if (!id) {
client.set('uid:' + uid + ':username', username, function(err) {
client.set('uid:' + uid + ':password', hash.md5(password), function(err) {
client.set('username:' + username + ':id', uid, function(err) {
callback(true, parseInt(uid.toString()));
});
});
});
} else {
sys.puts('Username already taken!');
callback(true, parseInt(id.toString()));
}
});
} else {
sys.puts('Cannot get UID: ' + err);
callback(true);
}
});
},
login: function(username, password, callback) {
client.get('username:' + username + ':uid', function(err, uid) {
if (uid) {
client.get('uid:' + uid + ':password', function(err, password){
if (hash.md5(password) == password) {
callback(false, parseInt(uid.toString()));
} else {
sys.puts('Wrong password');
callback(true);
}
});
} else {
sys.puts('No such user');
callback(true);
}
});
},
follow: function(uid, fid, callback) {
client.sadd('uid:' + uid + ':following', fid, function(err) {
if (err) {
sys.puts('Error following');
callback(true);
} else {
client.sadd('uid:' + fid + ':followers', uid, function(err) {
if (err) {
callback(true);
sys.puts('Error following');
} else {
callback(false);
}
});
}
});
},
unfollow: function(uid, fid, callback) {
client.srem('uid:' + uid + ':following', fid, function() {
client.srem('uid:' + fid + ':followers', uid, function() {
callback(false);
});
});
},
post: function(uid, body, callback) {
client.incr('global:nextPostId', function(err, pid) {
client.smembers('uid:' + uid + ':followers', function(err, followers) {
if (err) {
sys.puts('Error writing to followers');
}
client.get('uid:' + uid + ':username', function(err, username) {
if (!err) {
client.lpush('uid:' + uid + ':posts', JSON.stringify({'author':uid, 'body':body}), function() {
callback(false, pid);
});
for (var fid in followers) {
sys.puts('To ' + followers[fid] + ' timeline');
client.lpush('uid:' + followers[fid] + ':posts', JSON.stringify({'author':uid, 'body':body}), function() {});
}
} else {
sys.puts('Post from non-existing username');
callback(true);
}
});
})
});
},
getPosts: function(uid, callback) {
client.lrange('uid:' + uid + ':posts', 0, 10, function(err, data) {
if (err) {
callback(err);
} else {
callback(false, data);
}
});
},
getFollowing: function(uid, callback) {
client.smembers('uid:' + uid + ':following', function(err, data) {
if (err) {
sys.puts('Error getting followers');
callback(true);
} else {
callback(false, data);
}
});
}
};
exports.ritter = ritter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment