-
Notifications
You must be signed in to change notification settings - Fork 1
/
socialprovider.js
100 lines (86 loc) · 3.2 KB
/
socialprovider.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
/*jslint white:true,sloppy:true */
/*global window:true,freedom:true,setTimeout,console,VCardStore,global */
/**
* Implementation of a Social provider for freedom.js that
* uses sockets to make xmpp connections to chat networks.
**/
// Global declarations for node.js
if (typeof global !== 'undefined') {
if (typeof window === 'undefined') {
console.log("making global.window");
global.window = {};
}
} else {
if (typeof window === 'undefined') {
console.log("making window");
var window = {};
window.XMLHttpRequest = XMLHttpRequest;
}
}
var XMPPSocialProvider = function(dispatchEvent) {
console.log("connecting to firebase");
Firebase.INTERNAL.forceWebSockets();
// TODO: multiple instance (client support)
// var g_instance = prompt('Instance name', 'Laptop');
var baseUrl = "https://popping-heat-4874.firebaseio.com";
var g_authData;
function login() {
return new Promise(function(F, R) {
console.log('creating baseRef');
var baseRef = new Firebase(baseUrl);
console.log('baseRef created');
baseRef.authWithOAuthToken("facebook", 'CAACTRbmvZCKUBAP66Sf5s6BZC6JThs2vTcYfvvktAbZCfvZBZAFXOIatqLpEXiOsda78QQ2rJAWuQWKoXRyVVx4VcezuAwnyFEDydmhU44u3nyCnHpKzg6Y9yELN5bAYWflrZCzFFrvUa6k6zErIZAQcEMit7xZAF1EAYeZCdO75iihuQXjmjNK6NDWET5YX7dKcaW4JD9a9xZAQZDZD',
function(error, authData) {
if (error) {
R("Login Failed! " + error);
} else {
console.log("Authenticated successfully with payload:", authData, authData.facebook);
g_authData = authData;
// $('#myName').text(authData.facebook.displayName);
F();
}
});
});
}
function loadFriends() {
console.log('entering loadFriends');
return facebookGet('me/friends')
.then(function(friendsData) {
console.log('got friends', friendsData);
var friendsRef = new Firebase(baseUrl + '/users/' + g_authData.uid + '/friends');
for (var i = 0; i < friendsData.data.length; ++i) {
var friend = friendsData.data[i];
console.log('pushing friend', friend);
// TODO: this is going to clobber messages each time the friend signs in.. Do we want that?
friendsRef.child('facebook:' + friend.id).set(friend.name);
}
});
}
function facebookGet(resourceStr) {
return new Promise(function(F,R) {
if (!g_authData) {
R('Not signed in');
}
// Create url from resourceStr and accessToken.
var hasArgs = resourceStr.indexOf('?') >= 0;
var url = ('https://graph.facebook.com/v2.1/' + resourceStr) +
(hasArgs ? '&' : '?') +
'access_token=' + g_authData.facebook.accessToken;
$.getJSON(url, F);
});
}
function sendMessage(dest, message) {
var friendId = 'facebook:' + dest;
var friendMessagesRef = new Firebase(
baseUrl + '/users/' + friendId + '/friends/' + g_authData.uid +
'/messages');
friendMessagesRef.push(message);
}
login()
.then(loadFriends)
.catch(function(e) { console.error(e); });
};
// Register provider when in a module context.
if (typeof freedom !== 'undefined') {
freedom.social().provideAsynchronous(XMPPSocialProvider);
}