forked from facebookarchive/Realtime-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (49 loc) · 1.89 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
59
60
61
62
63
64
/*
Instagram real-time updates demo app.
*/
var url = require('url'),
settings = require('./settings'),
helpers = require('./helpers'),
crypto = require('crypto'),
subscriptions = require('./subscriptions');
var app = settings.app;
app.get('/callbacks/geo/:geoName', function(request, response){
// The GET callback for each subscription verification.
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){
// 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 by making sure it's
// coming from a trusted source. We use the client secret as the key
// to the HMAC.
var hmac = crypto.createHmac('sha1', settings.CLIENT_SECRET);
hmac.update(request.rawBody);
var providedSignature = request.headers['x-hub-signature'];
var calculatedSignature = hmac.digest(encoding='hex');
// If they don't match up or we don't have any data coming over the
// wire, then bail out early.
if((providedSignature != calculatedSignature) || !request.body)
response.send('FAIL');
// 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);
}
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);