-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
232 lines (212 loc) · 8.8 KB
/
main.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function () {
var HOSTED = window.location.protocol !== "file:";
// Easier than comparing string literals
var protocol = {
CHANNEL: "get-my-file2",
OFFER: "offer",
ANSWER: "answer",
REQUEST: "req-chunk",
DATA: "data",
DONE: "done",
ERR_REJECT: "err-reject",
CANCEL: "cancel"
};
var USING_GOOGLE = false;
function createFSClient() {
var CONTACT_API_URL = "https://www.google.com/m8/feeds";
var pubnub;
function FSClient() {
this.connections = {};
this.contactEmails = {};
};
FSClient.prototype = {
localLogin: function (name) {
this.uuid = name;
pubnub = PUBNUB.init({
publish_key: 'pub-c-b2d901ee-2a0f-4d89-8cd3-63039aa6dd90',
subscribe_key: 'sub-c-c74c7cd8-cc8b-11e2-a2ac-02ee2ddab7fe',
uuid: this.uuid
});
$(".my-email").html(this.uuid);
pubnub.subscribe({
channel: protocol.CHANNEL,
callback: this.handleSignal.bind(this),
presence: this.handlePresence.bind(this)
});
},
obtainGoogleToken: function () {
var params = {
response_type: "token",
client_id: "999382287610.apps.googleusercontent.com",
redirect_uri: window.location.origin + window.location.pathname,
scope: CONTACT_API_URL
};
var query = [];
for (var p in params) {
query.push(p + "=" + encodeURIComponent(params[p]));
}
query = query.join("&");
window.open("https://accounts.google.com/o/oauth2/auth?" + query, "_self");
},
getContacts: function (token) {
this.token = token;
var self = this;
var req = {
url: CONTACT_API_URL + "/contacts/default/full",
data: {
access_token: this.token,
v: 3.0,
alt: "json",
"max-results": 10000
}
};
var handleRes = function (res) {
self.uuid = res.feed.author[0].email["$t"].toLowerCase();
$(".my-email").html(self.uuid);
pubnub = PUBNUB.init({
publish_key: 'pub-c-b2d901ee-2a0f-4d89-8cd3-63039aa6dd90',
subscribe_key: 'sub-c-c74c7cd8-cc8b-11e2-a2ac-02ee2ddab7fe',
uuid: self.uuid
});
pubnub.subscribe({
channel: protocol.CHANNEL,
callback: self.handleSignal.bind(self),
presence: self.handlePresence.bind(self)
});
var contacts = res.feed.entry;
var email, c, numShown = 0;
var list = $(".contact-list");
var template = _.template($("#contact-template").html().trim());
console.log(res);
contacts.forEach(function (e) {
if (!e["gd$email"]) {
return;
}
email = e["gd$email"][0].address.toLowerCase();
// Don't consider ourselves a contact
if (self.uuid === email) {
return;
}
self.contactEmails[email] = true;
if (numShown < 25) {
c = template({ email: email, available: false });
list.append($(c));
self.connections[email] = new Connection(email,
document.getElementById("contact-" + email),
self.uuid, pubnub);
numShown++;
}
});
$(".contact-list").animate({ marginTop: "35px" }, 700);
if (res.next) {
req.url = res.next;
$.ajax(req).done(handleRes);
}
}
$.ajax(req).done(handleRes);
},
handleSignal: function (msg) {
var self = this;
// Don't care about messages we send
if (msg.uuid !== this.uuid && msg.target === this.uuid) {
var targetConnection = self.connections[msg.uuid];
targetConnection.handleSignal(msg);
}
},
handlePresence: function (msg) {
// Only care about presence messages from people in our Google contacts (if HOSTED)
var conn = this.connections[msg.uuid];
if (conn) {
conn.handlePresence(msg);
}
else if (this.contactEmails[msg.uuid] && msg.action === "join") {
var email = msg.uuid;
var list = $(".contact-list");
var template = _.template($("#contact-template").html().trim());
list.prepend($(template({ email: email, available: true })));
this.connections[email] = new Connection(email,
document.getElementById("contact-" + email),
this.uuid, pubnub);
this.connections[email].handlePresence(msg);
}
else if (!USING_GOOGLE && msg.uuid !== this.uuid && msg.uuid.indexOf("@") == -1 && msg.action === "join") {
var template = _.template($("#contact-template").html().trim());
var email = msg.uuid;
console.log(msg.action == "join");
$(".contact-list").append(
$(template({ email: email, available: true }))
);
this.connections[email] = new Connection(email,
document.getElementById("contact-" + email),
this.uuid, pubnub);
this.connections[email].handlePresence(msg);
$(".contact-list").animate({ marginTop: "35px" }, 700);
}
}
};
return new FSClient();
};
var client = createFSClient();
var confirm = $(".confirm-name");
var confirmArea = $(".confirm-name-area");
var input = $(".name-input");
var googleLogin = $("#google-login-button");
confirm.hover(function () {
confirm.stop();
confirm.animate({ color: "#669999" }, 300);
}, function () {
confirm.stop();
confirm.animate({ color: "white" }, 300);
});
confirm.click(function () {
$(".login-area").fadeOut();
client.localLogin(input.val().replace(/^\s\s*/, '').replace(/\s\s*$/, ''));
});
input.on("keyup", function (e) {
if (e.which === 13) {
confirm.click();
}
});
input.on("input", function () {
var curr = $(this).val(),
split = curr.split(/\s/);
for (var i = 0, len = split.length; i < len; i++) {
split[i] = split[i].replace(/\W/g, "");
}
curr = split.join(" ");
$(this).val(curr);
if (curr.length > 2 && curr.length < 20) {
if (curr.length >= 3 || curr.length <= 19) {
if (confirmArea.height() != 38) {
confirm.fadeIn();
confirmArea.animate({ height: "38px" }, 300);
}
}
}
else {
if (confirmArea.height() != 0) {
confirm.fadeOut();
confirmArea.animate({ height: "0px" }, 300);
}
}
});
googleLogin.click(function (event) {
client.obtainGoogleToken();
USING_GOOGLE = true;
});
// First, parse the query string
var params = {}, queryString = location.hash.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
if (params.access_token) {
window.location.hash = "";
USING_GOOGLE = true;
client.getContacts(params.access_token);
return;
}
else {
$(".login-area").fadeIn();
}
})();