-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrt.js
More file actions
327 lines (317 loc) · 9.25 KB
/
Copy pathrt.js
File metadata and controls
327 lines (317 loc) · 9.25 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
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
var connect = require('connect'),
dispatch = require('dispatch'),
passport = require('passport'),
quip = require('quip'),
usergrid = require('usergrid'),
swig = require('swig'),
keys = require(__dirname + '/keys'),
PATH = require('path'),
fs = require('fs'),
tnr = require('2nr13');
swig.init({
cache: false,
encoding: 'utf8',
filters: {},
root: __dirname + '/tpl',
extensions: {}
});
var client = new usergrid.client({
orgName: keys['apigee-orgName'],
appName: keys['apigee-appName'],
authType: usergrid.AUTH_CLIENT_ID,
clientId: keys['apigee_clientId'],
clientSecret: keys['apigee_clientSecret']
});
function view(file, ctx){
var tpl = swig.compileFile(file);
return tpl.render(ctx);
}
function getDeckBySessionId(id, cb){
client.request({
method: 'GET',
endpoint: 'deckSessions',
uuid: id
},function(err,data){
if(err){ cb('not a real deck session'); return; }
var deckId = data.entities[0].relatedDeck;
client.request({
method: 'GET',
endpoint: 'decks',
uuid: deckId
},function(error,data){
if(error){ cb('not a real deck'); return; }
var deck = data.entities[0];
var deckFile = deck.fileName;
fs.readFile(__dirname+'/ups/'+deckFile, 'utf8', function(error2,data){
if(error2){ cb('unable to read deck file'); return; }
cb(false, deck, data);
});
});
});
}
function logDeckSessionEvent(id, eventData){
var ts = Date.NOW,
ropt = {
method: 'PUT',
endpoint: 'deckSessions',
uuid: id,
body:{}
};
ropt.body['evt-'+ts] = eventData;
client.request(ropt,function(err,deckSession){
if(err){
return console.log('unable to log deck session details');
}
});
}
var roomsWithControllers = new Array();
var io;
var app = connect(quip())
.use(connect.multipart({ uploadDir: __dirname + '/ups' }))
.use(connect.static(__dirname + '/public'))
.use(connect.cookieParser('Archimedes'))
.use(connect.session({ secret: 'Merlin had an Owl, hoot hoot!'}))
.use(connect.bodyParser())
.use(function(req,res,next){
req.ctx = {
user: req.session && req.session.user? req.session.user : false
};
next();
})
.use(dispatch({
'/auth':{
'GET /register': function(req,res,next){
res.ok(view('register.html'));
},
'GET /login': function(req,res,next){
res.ok(view('login.html'));
},
'GET /logout': function(req,res,next){
req.session.user = false;
client.logout();
client.authType = usergrid.AUTH_CLIENT_ID;
res.redirect('/')
},
'POST /register': function(req,res,next){
var formBody = req.body;
// email, password, passwordConfirm
if(formBody.email && formBody.password && formBody.passwordConfirm && formBody.password == formBody.passwordConfirm){
client.request({
method:'POST',
endpoint:'users',
body:{ username: formBody.email, password: formBody.password }
}, function(err, data){
if(err){
req.ctx.errors = [{msg: JSON.stringify(err)}];
res.ok(view('register.html', req.ctx));
}else{
if(formBody.email && formBody.password){
client.login(formBody.email, formBody.password, function(err){
if(err){
req.ctx.errors = [{msg: 'No such user'}]
res.ok(view('login.html', req.ctx));
return;
}
client.authType = usergrid.AUTH_APP_USER;
client.getLoggedInUser(function(err, data, user){
if(err){
console.log('could not get logged in user');
} else {
//you can then get info from the user entity object:
req.session.user = user;
res.redirect('/');
}
});
});
}
else{
req.ctx.errors = [{msg: 'Form is incomplete'}]
res.ok(view('register.html', req.ctx));
}
}
});
}
else{
req.ctx.errors = [{msg: 'Form is incomplete'}]
res.ok(view('register.html', req.ctx));
}
},
'POST': function(req,res,next){
var formBody = req.body;
// email, password
if(formBody.email && formBody.password){
client.login(formBody.email, formBody.password, function(err){
if(err){
req.ctx.errors = [{msg: 'No such user'}]
res.ok(view('login.html', req.ctx));
return;
}
client.authType = usergrid.AUTH_APP_USER;
client.getLoggedInUser(function(err, data, user){
if(err){
console.log('could not get logged in user');
} else {
//you can then get info from the user entity object:
req.session.user = data.entities[0];
res.redirect('/presentations');
}
});
});
}
else{
req.ctx.errors = [{msg: 'Form is incomplete'}]
res.ok(view('register.html', req.ctx));
}
}
},
'/presentations': {
'GET': function(req,res,next){
client.createCollection({
type: 'decks',
ql: 'where fromUser = \'' + req.session.user.uuid + '\' order by index'
}, function(err, decks){
if(err){
res.error('unable to get presentations');
return;
}
req.ctx.presentations = new Array();
while(decks.hasNextEntity()) {
var deck = decks.getNextEntity()._data;
req.ctx.presentations.push(deck);
}
res.ok(view('my-profile.html', req.ctx));
});
},
'POST': function(req,res,next){
var tup = req.files.file;
var upa = tup.path.split(PATH.sep),
ufn = upa[upa.length - 1];
var deck = {
type: 'decks',
name: tup.name,
fromUser: req.session.user.uuid,
fileName: ufn
};
// fire and forget, create deck
client.createEntity(deck, function(err,data){});
res.redirect('/presentations');
}
},
'/set-the-table/:id': function(req,res,next,id){
client.request({
method: 'GET',
endpoint: 'decks',
uuid: id
}, function(err, data){
if(err){
res.error('failed to load presentation ' + id)
}
// there should be exactly 1 entities
var deck = data.entities[0];
req.session.currentDeck = deck;
var deckSession = {
type: 'deckSessions',
relatedDeck: deck.uuid
};
// fire and forget, create deck
client.createEntity(deckSession, function(err,data){
if(err){ res.error('Could not set the table'); return; }
var roomId = data._data.uuid;
req.session.currentDeckRoom = roomId
req.session.presentSecret = tnr.encode(roomId);
res.redirect('/big-screen/'+roomId);
//setup the socket.io namespace
io
.of('/'+roomId)
.on('connection', function(socket){
socket.on('show-connect-data', function(){
socket.broadcast.emit('toggle-connect-data');
});
socket.on('go-to-next', function(){
socket.broadcast.emit('next');
socket.emit('next');
logDeckSessionEvent(roomId, {
evtName: 'next'
});
});
socket.on('go-to-prev', function(){
socket.broadcast.emit('prev');
socket.emit('prev');
logDeckSessionEvent(roomId, {
evtName: 'prev'
});
});
socket.on('interrupt', function(data){
socket.broadcast.emit('interrupt', data);
logDeckSessionEvent(roomId, {
evtName: 'interrupt',
evtData: data
});
});
socket.on('go-interrupt', function(data){
console.log('sending interrupt to the front end');
socket.broadcast.emit('show-interrupt', data);
logDeckSessionEvent(roomId, {
evtName: 'go-interrupt',
evtData: data
});
});
socket.on('check-for-controller', function(){
if(roomsWithControllers.indexOf(roomId) < 0){
socket.emit('no-controller');
}
else{
socket.emit('yes-controller');
}
});
socket.on('controller-connect', function(){
socket.broadcast.emit('yes-controller');
roomsWithControllers.push(roomId);
})
});
});
});
},
'/present/:id': function(req,res,next,id){
getDeckBySessionId(id, function(err,deck,deckFile){
req.ctx.deckContents = deckFile;
req.ctx.deck = deck;
req.ctx.room = id;
res.ok(view('presenter.html',req.ctx))
});
},
'/big-screen/:id': function(req,res,next,id){
getDeckBySessionId(id, function(err,deck,deckFile){
req.ctx.deckContents = deckFile;
req.ctx.deck = deck;
req.ctx.room = id;
res.ok(view('presentation.html',req.ctx))
});
},
'/view/:id': function(req,res,next,id){
getDeckBySessionId(id, function(err,deck,deckFile){
req.ctx.deckContents = deckFile;
req.ctx.deck = deck;
req.ctx.room = id;
res.ok(view('viewer.html',req.ctx))
});
},
'.+': function(req,res,next){
res.ok(view('index.html', req.ctx));
}
}));
var server = app.listen(4295);
io = require('socket.io').listen(server);
console.log('Round table listening on http://localhost:4295')
// socket.io config
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('log level', 0);
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);