-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
410 lines (312 loc) · 12.3 KB
/
app.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
var express = require('express');
var querystring = require('querystring');
var request = require('request');
var sprintf = require('sprintf').sprintf;
// Create an HTTP server
var app = express.createServer();
require('./config/environment.js')(app, express);
app.set('view engine', 'ejs');
//*************************************
//HOME
//*************************************
app.get('/', function (req, res) {
//console.log(singly.apiBaseUrl);
res.render('splash', {session: req.session});
});
//*************************************
//SELECT SERVICES
//*************************************
app.get('/auth', function (req, res){
app.pipeDB.load();
// Render out views/auth.ejs, passing in the array of links and the session
res.render('auth', {
services: getAuthServices(req),
session: req.session
});
});
//*************************************
//PROCESS AUTH
//*************************************
app.get('/callback', function (req, res) {
//console.log(req);
app.singly.getAccessToken(req.param('code'), function(err, access_token) {
req.session.access_token = access_token;
app.singly.apiCall('/profiles', req.session, function(err, profilesBody) {
try {
//profilesBody = JSON.parse(profilesBody);
console.log("Profiles: " + JSON.stringify(profilesBody));
} catch(parseErr) {
return res.send(parseErr, 500);
}
req.session.profiles = profilesBody;
res.redirect('/auth');
});
});
});
//*************************************
//PIPE
//*************************************
app.get('/pipe', function (req, res) {
//AUTH CHECK
if (!req.session.access_token) return res.redirect('/');
//IF USER NOT LOADED
if (!req.session.userId){
//BUILD USER
var user = {
id: req.session.profiles.id
, profiles: req.session.profiles
};
console.log("USER: " + JSON.stringify(user));
//SAVE USER TO DB
app.pipeDB.saveUser(user);
//STORE USERID IN SESSION
req.session.userId = req.session.profiles.id;
}
//LOAD NEXT CHUM
app.pipeDB.getNextChum(user, function(chum){
if(!chum){
//IF NO LOCAL UNSURFACED CHUMS (none yet downloaded OR all have been touched OR none scheduled, etc...)
//DOWNLOAD NEXT SET
//GET NEXT OFFSET
app.pipeDB.getOffset(req.session.userId, function(offset){
//GET NEXT CONTACTS
app.singly.apiCall('/types/contacts', {limit:20, offset:offset, access_token:req.session.access_token}, function(err, contacts){
var cIndex = 0;
//GO THROUGH ALL RETURNED CONTACTS
for(var contact in contacts){
console.log("contact --> " + JSON.stringify(contacts[contact]));
if (contacts.hasOwnProperty(contact)){
//BUILD CHUM
var newChum = mapChum(contact);
if(!newChum){
//IF CHUM MAPPED SUCCESSFULLY
if(cIndex == 0) {
//IF FIRST, RETURN CHUM
res.render('pipe', {
session: req.session
, chum: newChum
});
//SAVE CHUM TO DB, SET AS SURFACED
app.pipeDB.saveChum(newChum, { surfaced : true });
}
else {
//SAVE CHUM TO DB
app.pipeDB.saveChum(newChum);
}
}
}
cIndex++;
}
//SAVE OFFSET
});
});
}
else {
//FIND ALL RELATED CONTACTS
//SAVE EACH TO DB
//RETURN CHUM
res.render('pipe', {
session: req.session
, chum: chum
});
}
});
});
//*************************************
//CONNECT
//*************************************
app.get('/connect', function (req, res) {
//AUTH CHECK
if (!req.session.access_token) return res.redirect('/');
//IF SUBMIT
//SAVE CONNECT TO DB
//IF LINKEDIN OR TWITTER
//SEND TO SINGLY PROXY
//IF EMAIL
//SEND EMAIL
//IF FACEBOOK
//??
//REDIRECT TO PIPE W/ MESSAGE
res.redirect('/pipe');
//ELSE
//GET CONNECTED ACCOUNTS FROM DB
//RETURN CONNECTION OPTIONS
res.render('connect', {
session: req.session,
chum: chum
});
});
//*************************************
//SKIP
//*************************************
app.get('/skip', function (req, res) {
//AUTH CHECK
if (!req.session.access_token) return res.redirect('/');
//IF SUBMIT
//SAVE SKIP TO DB
//REDIRECT TO PIPE W/ MESSAGE
res.redirect('/pipe');
//ELSE
//GET CONNECTED ACCOUNTS FROM DB
//RETURN CONNECTION OPTIONS
res.render('skip', {
session: req.session,
accounts: accounts
});
});
//*************************************
//USER
//*************************************
app.get('/user', function (req, res) {
if (!req.session.access_token) return res.redirect('/');
//GET USER FROM DB
//RETURN USER
var users = {};
res.render('user', {
session: req.session,
accounts: accounts
});
});
//*************************************
//Google Site Verification
//*************************************
app.get('/googlea06f2c993be83d06.html', function(req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.write("google-site-verification: googlea06f2c993be83d06.html");
res.end();
});
app.listen(app.port);
console.log(sprintf('Listening at %s ', app.hostBaseUrl));
function getAuthServices(req){
var i;
var services = [];
// For each service in usedServices, get a link to authorize it
for (i = 0; i < app.usedServices.length; i++) {
services.push({
name: app.usedServices[i],
link: getLink(app.usedServices[i], req.session.profiles, req.session.access_token)
});
}
return services;
}
// Given the name of a service and the array of profiles, return a link to that
// service that's styled appropriately (i.e. show a link or a checkmark).
function getLink(prettyName, profiles, token){
var service = prettyName.toLowerCase();
// If the user has a profile authorized for this service
if (profiles && profiles[service] !== undefined) {
// Return a unicode checkmark so that the user doesn't try to authorize it again
return sprintf('<a href="%s/services/%s?access_token=%s" class="activated"><img style="margin-right:10px;" src="img/social_networks/%s_blue.png" alt="Pipe" width="46" height="46" /></a>',
app.apiBaseUrl,
service,
token,
prettyName.toLowerCase());
}
// This flow is documented here: http://dev.singly.com/authorization
var queryString = querystring.stringify({
client_id: app.clientId,
redirect_uri: sprintf('%s/callback', app.hostBaseUrl),
service: service
});
return sprintf('<a href="%s/oauth/authorize?flag=dm&%s" class="authorize"><img style="margin-right:10px;" src="img/social_networks/%s_grey.png" alt="Pipe" width="46" height="46" /></a>',
app.apiBaseUrl,
queryString,
prettyName.toLowerCase());
}
function getContactSet(req, callback){
//GET CONTACTS OFFSET FOR USER
//GET NEXT 20 CONTACTS FROM SINGLY
//FOREACH
//SEARCH SINGLY FOR SIMILAR
//AGGREGATE CONTACTS INTO CHUM
//SAVE CHUM
//SAVE OFFSET
}
function getContactCounts(req, next){
//Find out how many contacts are in Singly for connected profiles
var profs = JSON.stringify(req.session.profiles);
var services = [];
var counts = {};
var totalCount = 0, index = 0;
if(profs.indexOf("twitter") != -1)
services[services.length] = "twitter";
if(profs.indexOf("facebook") != -1)
services[services.length] = "facebook";
if(profs.indexOf("gcontacts") != -1)
services[services.length] = "gcontacts";
if(profs.indexOf("linkedin") != -1)
services[services.length] = "linkedin";
console.log(services);
for(var i = 0; i < services.length; i++){
singly.apiCall('/services/' + services[i], req.session, function(err, set){
var count = set.friends ? set.friends : (set.contacts ? set.contacts : set.connections);
counts[services[index]] = count;
//totalCount += parseFloat(count);
console.log(JSON.stringify(set));
console.log("___" + services[index] + "___" + count);
index++;
if(index == services.length) {
counts["length"] = services.length;
next(counts);
}
});
}
}
function mapChum(contact){
var chum = null;
console.log("mapping contact: " + JSON.stringify(contact));
if(contact.idr.indexOf("facebook") != -1) {
chum = {
"id" : contact.idr ? contact.idr : "",
"name" : contact.data.name ? contact.data.name : "",
"profiles" : {"facebook" : contact.data.username ? contact.data.username : ""},
"photos" : {"facebook" : contact.oembed && contact.oembed.thumbnail_url ? contact.oembed.thumbnail_url : ""},
"status" : "",
"meta" : contact.data.bio ? contact.data.bio : "",
"location" : contact.data.location && contact.data.location.name ? contact.data.location.name : "",
"profession" : contact.data.work && contact.data.work[0].employer && contact.data.work[0].employer.name ? contact.data.work[0].employer.name : "",
"tagline" : contact.data.quotes ? contact.data.quotes : ""
};
}
else if(contact.idr.indexOf("twitter") != -1){
chum = {
"id" : contact.idr ? contact.idr : "",
"name" : contact.data.name ? contact.data.name : "",
"profiles" : {"twitter" : contact.data.screen_name ? contact.data.screen_name : ""},
"photos" : {"twitter" : contact.data.profile_image_url ? contact.data.profile_image_url : ""},
"status" : contact.data.status && contact.data.status.text ? contact.data.status.text : "",
"meta" : contact.data.description ? contact.data.description : "",
"location" : contact.data.location ? contact.data.location : "",
"profession" : "",
"tagline" : contact.data.description ? contact.data.description : ""
};
}
else if(contact.idr.indexOf("linkedin") != -1){
chum = {
"id" : contact.idr ? contact.idr : "",
"name" : contact.data.firstName ? contact.data.firstName + " " + (contact.data.lastName ? contact.data.lastName : "") : "",
"profiles" : {"linkedin" : contact.data.id ? contact.data.id : ""},
"photos" : {"linkedin" : contact.data.profile_image_url ? contact.data.profile_image_url : ""},
"status" : contact.data.status && contact.data.status.text ? contact.data.status.text : "",
"meta" : contact.data.description ? contact.data.description : "",
"location" : contact.data.location && contact.data.location.name ? contact.data.location.name : "",
"profession" : contact.data.positions && contact.data.positions.values && contact.data.positions.values[0].company && contact.data.positions.values[0].company.name ? contact.data.positions.values[0].company.name : ""
"tagline" : contact.data.headline ? contact.data.headline : "",
};
}
else if(contact.idr.indexOf("gcontacts") != -1){
chum = {
"id" : contact.idr,
"name" : contact.data.gd$name ? (contact.data.gd$name.gd$fullName ? contact.data.gd$name.gd$fullName : "") : "",
"profiles" : {"gcontacts" : contact.data.gd$email && contact.data.gd$email[0] ? contact.data.gd$email[0].address : ""},
"photos" : "",
"status" : "",
"meta" : "",
"location" : "",
"profession" : "",
"tagline" : ""
//"photo" : contact.oembed && contact.oembed.thumbnail_url,
};
}
return chum;
}