-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
users.js
163 lines (134 loc) · 4.02 KB
/
users.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var thunky = require('thunky');
var once = require('once');
var LRU = require('lru-cache');
var stream = require('stream-wrapper');
var pump = require('pump');
var EventEmitter = require('events').EventEmitter;
var index = require('./search-index');
var getJSON = require('./getJSON');
var level = require('./level');
stream = stream.defaults({objectMode:true});
var cache = new LRU(10000); // TODO: find data-driven configs for caching
var noop = function() {};
var page = function(url, callback) {
var result = [];
var loop = function(i) {
getJSON(url+'?page='+i, function(err, list) {
if (err) return callback(err);
if (!list.length) return callback(null, result);
result = result.concat(list);
if (list.length < 30) return callback(null, result);
loop(i+1);
});
};
loop(1);
};
var fetch = function(username, callback) {
if (!username || username === index.nobody._id) {
return callback(null, {_id:index.nobody._id, following:{}, starred:{}, cached:new Date()});
}
callback = once(callback);
var res = {};
res._id = username;
res.cached = new Date();
page('https://api.github.com/users/'+username+'/following', function(err, following) {
if (err) return callback(err);
res.following = {};
res.following[username] = 1;
following.forEach(function(user) {
res.following[user.login] = 1;
});
if (res.starred) return callback(null, res);
});
page('https://api.github.com/users/'+username+'/starred', function(err, starred) {
if (err) return callback(err);
res.starred = {};
starred.forEach(function(repo) {
if (repo.owner.type !== 'User') return;
var username = repo.full_name.split('/')[0];
if (username === res._id) return;
res.starred[username] = (res.starred[username] || 0)+1;
});
if (res.following) return callback(null, res);
});
};
var fetchAndSave = function(username, callback) {
fetch(username, function(err, user) {
if (err) return callback(err);
level.users.put(user._id, user, function(err) {
if (err) return callback(err);
callback(null, user);
});
});
};
var fetchAndIndex = function(username, callback) {
fetchAndSave(username, function(err, user) {
if (err) return callback(err);
index.update(user, function(err) {
if (err) return callback(err);
callback(null, user);
});
});
};
var updateUser = function(username, callback) {
fetchAndSave(username, function(err, user) {
if (err) return callback(err);
var ready = function() {
var get = thunky(function(callback) {
index.update(user, function(err, updates) {
if (err) return callback(err);
user.updates = updates;
callback(null, user);
});
});
cache.set(username, get);
get(callback);
};
if (!cache.has(username)) return ready();
cache.get(username)(ready);
});
};
exports.update = function(callback) {
var progress = new EventEmitter();
pump(
level.users.createKeyStream(),
stream.transform(function(id, enc, callback) {
updateUser(id, callback);
}),
stream.writable(function(user, enc, callback) {
progress.emit('user', user);
callback();
}),
function(err) {
progress.emit('end', err);
}
);
return progress;
};
exports.get = function(username, callback) {
if (!username) username = index.nobody._id;
if (cache.has(username)) return cache.get(username)(callback);
var get = thunky(function(callback) {
level.users.get(username, function(err, user) {
if (!user) return fetchAndIndex(username, callback);
index.contains(user, function(err, contains) {
if (contains) return callback(null, user);
index.update(user, function(err) {
if (err) return callback(err);
callback(null, user);
});
});
});
});
cache.set(username, get);
get(callback);
};
exports.search = function(username, query, opts, callback) {
if (typeof opts === 'function') return exports.search(username, query, null, opts);
if (!opts) opts = {};
exports.get(username, function(err, user) {
if (err) return callback(err);
if (!user) return callback(new Error('user not found'));
index.search(user, query, opts, callback);
});
};