Skip to content

Commit

Permalink
Changes to get new users from github
Browse files Browse the repository at this point in the history
  • Loading branch information
caulagi committed Nov 11, 2012
1 parent 8f6ee12 commit bbcf7bd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 14 deletions.
61 changes: 50 additions & 11 deletions db.js
Expand Up @@ -9,6 +9,7 @@ function Db() {
this.userDocname = 'user-%s';
this.followingDocname = 'user-%s-following';
this.followingUrl = 'https://api.github.com/users/%s/following';
this.profileUrl = 'https://api.github.com/users/%s';
}

Db.prototype.findUserOrCreate = function(profile, done) {
Expand All @@ -24,7 +25,7 @@ Db.prototype.findUserOrCreate = function(profile, done) {
return done(error);
}
process.nextTick(function() {
that.addFollowing(doc, done, profile);
that.addFollowing(profile, done);
});
})
} else {
Expand All @@ -33,7 +34,7 @@ Db.prototype.findUserOrCreate = function(profile, done) {
})
}

Db.prototype.addFollowing = function(user, done, profile) {
Db.prototype.addFollowing = function(profile, done) {

var that = this
, url = util.format(that.followingUrl, profile.username)
Expand Down Expand Up @@ -67,24 +68,62 @@ Db.prototype.getFollowing = function(id, cb) {
});
}

// If we have the details about the user, return that.
// Else get the details from github, save and return the result
Db.prototype.findUserOrRetrieve = function(githubId, githubLogin, cb) {
var docname = util.format(this.userDocname, githubId)
, that = this
;

couch.get(docname, function(error, doc) {
if (error) {
var url = util.format(that.profileUrl, githubLogin);
request(url, function (error, response, body) {
if (error || response.statusCode != 200) {
return cb(error);
}
couch.insert({'profile': that.getUserFromProfile(body)}, docname, function(error, doc) {
return cb(error, doc);
});
})
} else {
return cb(null, doc);
}
});
}

// Get a user object that we will store in couch
// from a passport github profile object
Db.prototype.getUserFromProfile = function(profile) {
return {
var u = {
'github_id': profile.id
, 'display_name': profile.displayName
, 'username': profile.username
, 'profile_url': profile.profileUrl
, 'emails': profile.emails
, 'public_repos': profile._json.public_repos
, 'company': profile._json.company
, 'followers': profile._json.followers
, 'avatar_url': profile._json.avatar_url
, 'bio': profile._json.bio
, 'following': profile._json.following
, 'email': profile._json.email
, 'login': profile._json.login
}

if (profile._json) {
u['public_repos'] = profile._json.public_repos
u['company'] = profile._json.company
u['followers'] = profile._json.followers
u['avatar_url'] = profile._json.avatar_url
u['bio'] = profile._json.bio
u['following'] = profile._json.following
u['email'] = profile._json.email
u['login'] = profile._json.login
} else {
u['public_repos'] = profile.public_repos
u['company'] = profile.company
u['followers'] = profile.followers
u['avatar_url'] = profile.avatar_url
u['bio'] = profile.bio
u['following'] = profile.following
u['email'] = profile.email
u['login'] = profile.login
}

return u;
}

exports = module.exports = new Db();
Expand Down
37 changes: 37 additions & 0 deletions server.js
Expand Up @@ -78,6 +78,43 @@ app.get('/messages', login.ensureAuthenticated, function(req, res){
});
});

// TODO: login.ensureAuthenticaated
app.get('/message/add', function(req, res){
var toName = req.query['to_name']
, toId = req.query['to_id']
, message = req.query['message']
, user = req.user
;

if (user === undefined) {
var out = JSON.stringify ({ status: 'error', message: 'You are not logged in' })
;
res.writeHead(200, {"Content-Type": "application/json"});
res.write(out);
}

async.series({
toUser: function(cb) {
db.findUserOrRetrieve(toId, toName, cb);
}
}, function(error, results) {
async.parallel({
sentMessage: function(cb) {
db.saveMessage(user, results.toUser, message, cb);
},
ignore: function(cb) {
db.addFollowing(results.toUser.profile, cb);
}
}, function(error, dummy) {
res.writeHead(200, {"Content-Type": "application/json"});
res.write( JSON.stringify ({
'status': 'ok', 'message': results.sentMessage
}));
});
});

});

app.get('*', function(req, res){
res.status(404);
res.render('404');
Expand Down
8 changes: 5 additions & 3 deletions static/js/github-messaging.js
@@ -1,5 +1,6 @@
$(document).ready(function() {
var messageTo = undefined
var toId = undefined
, toName = undefined
;

$('#friends ul li').on('click', function(e) {
Expand All @@ -11,7 +12,8 @@ $(document).ready(function() {
// Get the github id of this user
for (var i = 0; i < GM.following.length; i++) {
if ( GM.following[i].login === messageTo ) {
messageTo = GM.following[i].id;
toId = GM.following[i].id;
toName = GM.following[i].login;
}
}
$(this).toggleClass('active');
Expand All @@ -31,7 +33,7 @@ $(document).ready(function() {
$.ajax({
type: "GET",
url: "/message/add",
data: { from: GM.user.id, to: messageTo, message: message },
data: { to_id: toId, to_name: toName, message: message },
success: function(data) {
console.log(data);
}
Expand Down

0 comments on commit bbcf7bd

Please sign in to comment.