-
Notifications
You must be signed in to change notification settings - Fork 0
/
behavior.js
84 lines (72 loc) · 2.75 KB
/
behavior.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
/*
* SocialConnect behavior
*/
const ALREADY_CONNECTED = "already_connected";
const INVALID_TOKEN = "invalid_token";
const CLAIM_FIRST = "claim_first";
const AUTH_PROBLEM = "facebook_auth_error";
// token is uid
behavior('/facebook', {
open: function(event){
if(!event.token){
event.deny(INVALID_TOKEN);
}
event.domain.get("facebook:"+event.token, function(err, value) {
if (!err && value != null && event.connection.id === value) {
event.connection.set("facebook-uid", event.token);
event.channel.emit(JSON.stringify({type:'joined', data:event.token}));
event.allow();
}
event.deny(CLAIM_FIRST);
});
},
close: function(event){
event.connection.get("facebook-uid", function(err, value){
if(!err && value != null){
event.channel.emit(JSON.stringify({type:'left', data: value}));
}
});
}
});
// token is facebook token
behavior('/facebook/{uid}', {
open: function(event) {
event.domain.get("facebook:"+event.params.uid, function(err, value){
if(!err && value != null){
event.connection.get("facebook-uid", function(err, value){
if(!err && value != null && value == event.params.uid){
event.deny(ALREADY_CONNECTED);
}
event.channel.emit(JSON.stringify({type:'ping', data: value}));
event.allow();
});
}
if(!event.token){
event.deny(INVALID_TOKEN);
}
http.get('https://graph.facebook.com/'+event.params.uid+'?access_token='+event.token, function(err, body){
if(!err){
var response = null;
try{
response = JSON.parse(body);
}catch(e){}
if(response != null){
if(!response.error){
event.domain.set("facebook:"+event.params.uid, event.connection.id);
event.allow();
}
}
}
event.deny(AUTH_PROBLEM);
});
});
},
close: function(event){
event.domain.get("facebook:"+event.params.uid, function(err, value){
if(!err && value != null && value === event.connection.id){
event.domain.del("facebook:"+event.params.uid);
event.channel.emit(JSON.stringify({'close': event.params.uid}));
}
});
}
});