forked from asalant/Realtime-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (46 loc) · 1.62 KB
/
server.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
/*
Instagram real-time updates demo app.
*/
var url = require('url'),
redis = require('redis'),
settings = require('./settings'),
helpers = require('./helpers'),
subscriptions = require('./subscriptions');
var app = settings.app;
app.get('/callbacks/geo/:geoName', function(request, response){
// The GET callback for each subscription verification.
helpers.debug("GET " + request.url);
var params = url.parse(request.url, true).query;
response.send(params['hub.challenge'] || 'No hub.challenge present');
});
app.post('/callbacks/geo/:geoName', function(request, response){
helpers.debug("PUT /callbacks/geo/" + request.params.geoName);
// The POST callback for Instagram to call every time there's an update
// to one of our subscriptions.
// First, let's verify the payload's integrity
if(!helpers.isValidRequest(request)) {
response.send('FAIL');
return;
}
// Go through and process each update. Note that every update doesn't
// include the updated data - we use the data in the update to query
// the Instagram API to get the data we want.
var updates = request.body;
var geoName = request.params.geoName;
for(index in updates){
var update = updates[index];
if(update['object'] == "geography")
helpers.processGeography(geoName, update);
}
helpers.debug("Processed " + updates.length + " updates");
response.send('OK');
});
// Render the home page
app.get('/', function(request, response){
helpers.getMedia(function(error, media){
response.render('geo.jade', {
locals: { images: media }
});
});
});
app.listen(settings.appPort);