forked from heroku/facebook-template-nodejs
/
web.js
131 lines (106 loc) · 4.19 KB
/
web.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
require.paths.unshift(__dirname + '/lib');
var everyauth = require('everyauth');
var express = require('express');
var FacebookClient = require('facebook-client').FacebookClient;
var facebook = new FacebookClient();
var uuid = require('node-uuid');
// configure facebook authentication
everyauth.facebook
.appId(process.env.FACEBOOK_APP_ID)
.appSecret(process.env.FACEBOOK_SECRET)
.scope('publish_actions,user_likes,user_photos,user_photo_video_tags')
.entryPath('/')
.redirectPath('/home')
.findOrCreateUser(function() {
return({});
})
// create an express webserver
var app = express.createServer(
express.logger(),
express.static(__dirname + '/public'),
express.cookieParser(),
// set this to a secret value to encrypt session cookies
express.session({ secret: process.env.SESSION_SECRET || 'secret123' }),
// insert a middleware to set the facebook redirect hostname to http/https dynamically
function(request, response, next) {
var method = request.headers['x-forwarded-proto'] || 'http';
everyauth.facebook.myHostname(method + '://' + request.headers.host);
next();
},
everyauth.middleware(),
require('facebook').Facebook()
);
// listen to the PORT given to us in the environment
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});
// create a socket.io backend for sending facebook graph data
// to the browser as we receive it
var io = require('socket.io').listen(app);
// wrap socket.io with basic identification and message queueing
// code is in lib/socket_manager.js
var socket_manager = require('socket_manager').create(io);
// use xhr-polling as the transport for socket.io
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
// respond to GET /home
app.get('/home', function(request, response) {
// detect the http method uses so we can replicate it on redirects
var method = request.headers['x-forwarded-proto'] || 'http';
// if we have facebook auth credentials
if (request.session.auth) {
// initialize facebook-client with the access token to gain access
// to helper methods for the REST api
var token = request.session.auth.facebook.accessToken;
facebook.getSessionByAccessToken(token)(function(session) {
// generate a uuid for socket association
var socket_id = uuid();
// query 3 friends and send them to the socket for this socket id
session.graphCall('/me/friends&limit=3')(function(result) {
result.data.forEach(function(friend) {
socket_manager.send(socket_id, 'friend', friend);
});
});
// query 2 photos and send them to the socket for this socket id
session.graphCall('/me/photos&limit=2')(function(result) {
result.data.forEach(function(photo) {
socket_manager.send(socket_id, 'photo', photo);
});
});
// query 11 likes and send them to the socket for this socket id
session.graphCall('/me/likes&limit=11')(function(result) {
result.data.forEach(function(like) {
socket_manager.send(socket_id, 'like', like);
});
});
// use fql to get a list of my friends that are using this app
session.restCall('fql.query', {
query: 'SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1',
format: 'json'
})(function(result) {
result.forEach(function(friend) {
socket_manager.send(socket_id, 'friend_using_app', friend);
});
});
// get information about the app itself
session.graphCall('/' + process.env.FACEBOOK_APP_ID)(function(app) {
// render the home page
response.render('home.ejs', {
layout: false,
token: token,
app: app,
user: request.session.auth.facebook.user,
home: method + '://' + request.headers.host + '/',
redirect: method + '://' + request.headers.host + request.url,
socket_id: socket_id
});
});
});
} else {
// not authenticated, redirect to / for everyauth to begin authentication
response.redirect('/');
}
});