forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacebook.js
More file actions
189 lines (163 loc) · 4.44 KB
/
facebook.js
File metadata and controls
189 lines (163 loc) · 4.44 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
//
// Facebook
//
(function(hello){
function formatUser(o){
if(o.id){
o.thumbnail = o.picture = 'http://graph.facebook.com/'+o.id+'/picture';
}
return o;
}
function formatFriends(o){
if("data" in o){
for(var i=0;i<o.data.length;i++){
formatUser(o.data[i]);
}
}
return o;
}
function format(o){
if("data" in o){
var token = hello.getAuthResponse('facebook').access_token;
for(var i=0;i<o.data.length;i++){
var d = o.data[i];
if(d.picture){
d.thumbnail = d.picture;
}
if(d.cover_photo){
d.thumbnail = base + d.cover_photo+'/picture?access_token='+token;
}
if(d.type==='album'){
d.files = d.photos = base + d.id+'/photos';
}
if(d.can_upload){
d.upload_location = base + d.id+'/photos';
}
}
}
return o;
}
var base = 'https://graph.facebook.com/';
hello.init({
facebook : {
name : 'Facebook',
login : function(p){
// The facebook login window is a different size.
p.options.window.width = 580;
p.options.window.height = 400;
},
// https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.1
oauth : {
version : 2,
auth : 'https://www.facebook.com/dialog/oauth/',
grant : 'https://graph.facebook.com/oauth/access_token'
},
// Refresh the access_token
refresh : true,
logout : function(callback){
// Assign callback to a global handler
var callbackID = hello.utils.globalEvent( callback );
var redirect = encodeURIComponent( hello.settings.redirect_uri + "?" + hello.utils.param( { callback:callbackID, result : JSON.stringify({force:true}), state : '{}' } ) );
var token = (hello.utils.store('facebook')||{}).access_token;
hello.utils.iframe( 'https://www.facebook.com/logout.php?next='+ redirect +'&access_token='+ token );
// Possible responses
// String URL - hello.logout should handle the logout
// undefined - this function will handle the callback
// true - throw a success, this callback isn't handling the callback
// false - throw a error
if(!token){
// if there isn't a token, the above wont return a response, so lets trigger a response
return false;
}
},
// Authorization scopes
scope : {
basic : 'public_profile',
email : 'email',
birthday : 'user_birthday',
events : 'user_events',
photos : 'user_photos,user_videos',
videos : 'user_photos,user_videos',
friends : 'user_friends',
files : 'user_photos,user_videos',
publish_files : 'user_photos,user_videos,publish_actions',
publish : 'publish_actions',
// Deprecated in v2.0
// create_event : 'create_event',
offline_access : 'offline_access'
},
// API Base URL
base : 'https://graph.facebook.com/',
// Map GET requests
get : {
'me' : 'me',
'me/friends' : 'me/friends',
'me/following' : 'me/friends',
'me/followers' : 'me/friends',
'me/share' : 'me/feed',
'me/files' : 'me/albums',
'me/albums' : 'me/albums',
'me/album' : '@{id}/photos',
'me/photos' : 'me/photos',
'me/photo' : '@{id}'
// PAGINATION
// https://developers.facebook.com/docs/reference/api/pagination/
},
// Map POST requests
post : {
'me/share' : 'me/feed',
'me/albums' : 'me/albums',
'me/album' : '@{id}/photos'
},
// Map DELETE requests
del : {
/*
// Can't delete an album
// http://stackoverflow.com/questions/8747181/how-to-delete-an-album
'me/album' : '@{id}'
*/
'me/photo' : '@{id}'
},
wrap : {
me : formatUser,
'me/friends' : formatFriends,
'me/following' : formatFriends,
'me/followers' : formatFriends,
'me/albums' : format,
'me/files' : format,
'default' : format
},
// special requirements for handling XHR
xhr : function(p,qs){
if(p.method==='get'||p.method==='post'){
qs.suppress_response_codes = true;
}
// Is this a post with a data-uri?
if( p.method==='post' && p.data && typeof(p.data.file) === 'string'){
// Convert the Data-URI to a Blob
p.data.file = hello.utils.toBlob(p.data.file);
}
return true;
},
// Special requirements for handling JSONP fallback
jsonp : function(p,qs){
var m = p.method.toLowerCase();
if( m !== 'get' && !hello.utils.hasBinary(p.data) ){
p.data.method = m;
p.method = 'get';
}
else if(p.method === "delete"){
qs.method = 'delete';
p.method = "post";
}
},
// Special requirements for iframe form hack
form : function(p){
return {
// fire the callback onload
callbackonload : true
};
}
}
});
})(hello);