-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
131 lines (115 loc) · 3.48 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var couch_url = process.env['COUCH_URL'] || ('http://localhost:5984')
, nano = require('nano')(couch_url)
, couch = nano.db.use('github-messaging')
, request = require('request')
, util = require('util')
;
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) {
var that = this
, docname = util.format(that.userDocname, profile.id)
;
couch.get(docname, function(error, doc) {
if (error) {
couch.insert({'profile': that.getUserFromProfile(profile)}, docname, function(error, doc) {
if (error) {
return done(error);
}
process.nextTick(function() {
that.addFollowing(profile, done);
});
})
} else {
done(null, doc);
}
})
}
Db.prototype.addFollowing = function(profile, done) {
var that = this
, url = util.format(that.followingUrl, profile.username)
, docname = util.format(that.followingDocname, profile.id)
;
request(url, function (error, response, body) {
if (error || response.statusCode != 200) {
done(error);
}
var following = JSON.stringify(body);
couch.insert({'following': following}, docname, function(error, doc) {
if (error) {
return done(error);
}
done(null, doc);
});
})
}
Db.prototype.getFollowing = function(id, cb) {
var docname = util.format(this.followingDocname, id)
;
couch.get(docname, function(error, doc) {
if (error) {
return cb(error);
}
return cb(null, doc);
});
}
// 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) {
var u = {
'github_id': profile.id
, 'display_name': profile.displayName
, 'username': profile.username
, 'profile_url': profile.profileUrl
, 'emails': profile.emails
}
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();
exports.version = '0.0.1';
exports.Db = Db;