-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFacebook.js
More file actions
196 lines (159 loc) · 4.41 KB
/
Facebook.js
File metadata and controls
196 lines (159 loc) · 4.41 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
190
191
192
193
194
195
196
var Alloy = require('alloy'),
_ = Alloy._,
Backbone = Alloy.Backbone,
fbSDK = require('facebook'),
Acl = require('RebelFrame/Acl');
/**
* Facebook provides a wrapper for the Facebook SDK and Ti Facebook module.
*
* @class Facebook
* @singleton
*/
var FB = {
appId: Alloy.CFG.Facebook.appId,
permissions: Alloy.CFG.Facebook.permissions,
login: function(options) {
// Wrap the success function
// options.success = _.wrap(options.success, function(func, userData) {
// // Call register function on the Cloud API
// Acl.registerUserWithCloud(userData, {
// success: func,
// error: options.error,
// scope: options.scope
// });
// });
// Make the call using the FB SDK
_authorize(options);
},
connect: function(options) {
// Wrap the success function
options.success = _.wrap(options.success, function(func, userData) {
// Call connect function on the Cloud API
Acl.connectSocialMediaToUser(userData, {
success: func,
error: options.error,
scope: options.scope
});
});
// Make the call using the FB SDK
_authorize(options);
},
requestWriteAccess: function(callback) {
if (fbSDK.loggedIn)
_reAuthorize(callback);
else
_authorize({
success: function(userData) {
_reAuthorize(callback);
},
error: function(evt) {
},
scope: FB
});
},
isLoggedin: function() {
return fbSDK.loggedIn;
},
getPermissions: function() {
return fbSDK.permissions;
},
hasWritePermissions: function() {
return _.indexOf(fbSDK.permissions, FB.permissions.write[0]) !== -1;
}
};
function _authorize(options) {
var callbackWrapper = function(evt) {
fbSDK.removeEventListener('login', callbackWrapper);
// Add network to event object
evt.network = 'facebook';
if (evt.success) {
//Write to cloud storage
var userData = {
facebook_id: fbSDK.uid,
facebook_access_token: fbSDK.accessToken,
facebook_expiration: fbSDK.expirationDate,
type: 'facebook'
};
options.success(userData);
// Log this event
// Analytics.recordEvent('facebook', 'connect', 'facebook');
} else if (evt.cancelled) {
options.error(evt);
} else {
handleError(evt);
options.error(evt);
}
};
fbSDK.addEventListener('login', callbackWrapper);
fbSDK.appid = FB.appId;
if (OS_IOS)
fbSDK.permissions = FB.permissions.read;
else
fbSDK.permissions = _.extend(FB.permissions.read, FB.permissions.write);
fbSDK.forceDialogAuth = false;
if (OS_IOS)
fbSDK.initialize();
// If user is loggedin to Facebook, log him/her out first
if (fbSDK.loggedIn) {
Ti.API.info('Already loggedin!');
callbackWrapper({success: true});
} else {
// fix for facbeook login on device, logout first before authorizing
fbSDK.logout();
fbSDK.authorize();
}
}
function _reAuthorize(options) {
var callbackWrapper = function(evt) {
Ti.API.info(evt);
if (evt.success) {
//Write to cloud storage
options.success(evt);
// Log this event
// Analytics.recordEvent('facebook', 'connect', 'facebook');
} else if (evt.cancelled) {
// Do nothing?
Ti.API.info('Cancelled');
} else {
handleError(evt);
options.error(evt);
}
};
if (OS_IOS)
fbSDK.requestNewPublishPermissions(FB.permissions.write, fbSDK.audienceFriends, callbackWrapper);
else {
// Android already has write permissions
options.success();
}
}
function handleError(evt) {
var message;
if (OS_IOS) {
// For all of these errors - assume the user is logged out
// so show your login UI
if (evt.error.indexOf('Go to Settings') === 0) {
message = String.format(L('fb_no_premission', '%1$s needs permission to access your facebook account. You can control this by going to Settings > Facebook on your phone.'), Ti.App.name);
} else if (evt.error.indexOf('Session Login Error') === 0) {
// Session was invalid - e.g. the user deauthorized your app, etc
// alert('Please login again.');
} else if (evt.error.indexOf('OTHER:') !== 0) {
// another error that may require user attention
message = L('lFacebookError') + evt.error;
} else {
// This must be an error message that the user was already notified about
// Due to the automatic error handling in the graph call
// Don't surface it again
}
} else
message = L('lFacebookError') + evt.error;
if (message) {
var dialog = Ti.UI.createAlertDialog({
title: L('tFacebookError'),
message: message,
buttonNames: [L('Okay')],
cancel: 0
});
dialog.show();
}
}
module.exports = FB;