-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathapi.js
More file actions
189 lines (177 loc) · 6.65 KB
/
api.js
File metadata and controls
189 lines (177 loc) · 6.65 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var dataStore = require('./dataStore');
var path = require('path');
var fs = require('fs');
var locker = require('locker');
var logger = require('logger');
var crypto = require('crypto');
var request = require('request');
var url = require('url');
module.exports = function(app, lockerInfo) {
locker.initClient(lockerInfo);
locker.connectToMongo(function(mongo) {
// initialize all our libs
dataStore.init(mongo, locker);
// TODO: this is a race condition. The routes will be added before the dataStore is initialized
// callback();
});
app.get('/state', function(req, res) {
dataStore.getTotalCount(function(err, countInfo) {
if(err) return res.send(err, 500);
dataStore.getLastObjectID(function(err, lastObject) {
if(err) return res.send(err, 500);
var objId = '000000000000000000000000';
if (lastObject) objId = lastObject._id.toHexString();
var updated = Date.now();
try {
var js = JSON.parse(path.join(lockerInfo.workingDirectory, fs.readFileSync('state.json')));
if(js && js.updated) updated = js.updated;
} catch(E) {}
res.send({ready:1, count:countInfo, updated:updated, lastId:objId});
});
});
});
app.get('/since', function(req, res) {
if (!req.query.id) return res.send([]);
var results = [];
dataStore.getSince(req.query.id, function(link) {
results.push(link);
}, function() {
async.forEachSeries(results, function(link, callback) {
if (!link) return;
link.encounters = [];
dataStore.getEncounters({'link':link.link}, function(encounter) {
link.encounters.push(encounter);
}, function() {
callback();
});
}, function() {
var sorted = results.sort(function(lh, rh) {
return rh.at - lh.at;
});
res.send(sorted);
});
});
});
// expose way to get raw links and encounters
app.get('', function(req, res) {
var full = isFull(req.query.full);
var fullResults = [];
var results = [];
var options = {sort: {at: -1}};
if(!req.query.all) options.limit = 20; // default 20 unless all is set
if (req.query.limit) options.limit = parseInt(req.query.limit);
if (req.query.offset) options.offset = parseInt(req.query.offset);
var deleteLink = false;
if (req.query.fields) {
try {
options.fields = JSON.parse(req.query.fields);
// we need the link field for merging encounters and links objects
// so get it, but flag it for deletion if not requested
if(!options.fields.hasOwnProperty('link') || options.fields.link == 0) {
options.fields.link = 1;
deleteLink = true;
}
} catch(E) {}
}
var ndx = {};
if(req.query.stream == 'true') {
res.writeHead(200, {'content-type' : 'application/jsonstream'});
options = {}; // exclusive
}
dataStore.getLinks(options, function(item) {
if(req.query.stream == 'true') return res.write(JSON.stringify(item)+'\n');
if(full) item.encounters = [];
ndx[item.link] = item;
// delete the link field if it wasn't requested
if(deleteLink) delete item.link;
results.push(item);
}, function(err) {
if(err) logger.error(err);
if(req.query.stream == 'true') return res.end();
if(full) {
var arg = {link: {$in: Object.keys(ndx)}};
if(options.fields) {
arg.fields = {};
// extract only encounter.* fields
for(var i in options.fields) {
if(i.length > 11 && i.substring(0,11) === 'encounters.') arg.fields[i.substring(11)] = options.fields[i];
}
// we need the link field for merging encounters and links objects
// so get it, but flag it for deletion if not requested
if(!arg.fields.hasOwnProperty('link') || arg.fields.link == 0) {
arg.fields.link = 1;
deleteLink = true;
}
}
dataStore.getEncounters(arg, function(encounter) {
ndx[encounter.link].encounters.push(encounter);
// delete the link field if it wasn't requested
if(deleteLink) delete encounter.link;
}, function() {
res.send(results);
});
} else {
return res.send(results);
}
});
});
app.get('/id/:id', function(req, res, next) {
dataStore.get(req.param('id'), function(err, doc) {
if(err || !doc) return res.send(err, 500);
if(!doc.id) doc.id = crypto.createHash('md5').update(doc.link).digest('hex'); // older links are missing id, meh
if(!isFull(req.query.full)) return res.send(doc);
doc.encounters = [];
dataStore.getEncounters({link: doc.link}, function(e){ doc.encounters.push(e); }, function(err) { res.send(doc); });
});
});
app.get('/search', function(req, res) {
if (!req.query.q) return res.send([]);
var u = url.parse(locker.lockerBase + '/Me/search/query');
u.query = req.query;
u.query.type = 'link';
u.query.sort = 'true'; // default sorted
// pretty much just dumb proxy it at this point
request({url:url.format(u)}, function(err, resp, body){
if(err || !body) return res.send(err, 500);
res.writeHead(200, {'content-type' : 'application/json'});
res.write(body);
res.end();
});
});
// expose way to get the list of encounters from a link id
app.get('/encounters/:id', function(req, res) {
var encounters = [];
dataStore.get(req.param('id'), function(err, doc) {
if(err || !doc || !doc.link) return res.send(encounters);
dataStore.getEncounters({link: doc.link}, function(e){ encounters.push(e); }, function(err){ res.send(encounters); });
});
});
// get just encounters raw!
// expose way to get raw links and encounters
app.get('/encounters', function(req, res) {
var options = {sort:{"_id":-1}};
if(!req.query["all"]) options.limit = 20; // default 20 unless all is set
if (req.query.limit) {
options.limit = parseInt(req.query.limit);
}
if (req.query.offset) {
options.offset = parseInt(req.query.offset);
}
if(req.query['stream'] == "true")
{
res.writeHead(200, {'content-type' : 'application/jsonstream'});
}
var results = [];
dataStore.getEncounters(options, function(item) {
if(req.query['stream'] == "true") return res.write(JSON.stringify(item)+'\n');
results.push(item);
}, function(err){
if(err) logger.error(err);
if(req.query['stream'] == "true") return res.end();
return res.send(results);
});
});
}
function isFull(full) {
return (full === true || full === "true" || full == 1);
}