-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
320 lines (274 loc) · 9.22 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
var express = require('express');
var app = express.createServer();
var server = app.listen(3000);
var nowjs = require('now'),
everyone = nowjs.initialize(server);
var jqtpl = require('jqtpl');
var manifesto = require('manifesto');
var gzippo = require('gzippo');
var uuid = require('node-uuid');
var Hashids = require("hashids"),
hashids = new Hashids("chocolade salty balls");
var qrCode = require('qrcode-npm');
var passport = require('passport');
var GoogleStrategy = require('passport-google').Strategy;
var mongoose = require('mongoose');
// database
mongoose.connect('YOURCONNECTIONSTRING');
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new GoogleStrategy({
returnURL: 'http://localhost:3000/auth/google/return',
realm: 'http://localhost:3000/'
},
function(identifier, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Google profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Google account with a user record in your database,
// and return that user instead.
profile.identifier = identifier;
return done(null, profile);
});
}
));
app.configure(function(){
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
//app.use(express.static(__dirname + '/public'));
app.use(gzippo.staticGzip(__dirname + '/public'));
app.use(express.session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.set('view options', {
layout: false
});
app.register('html', jqtpl.express);
});
var Schema = mongoose.Schema; //Schema.ObjectId
var Questions = new Schema({
name: {type: String, required: true, index: {unique: true} },
creator: {type: String, default: "Anonymous"},
votes: {type: Number, default: 1}
});
var Presentation = new Schema({
id: {type: Number, required: true },
title: {type: String, required: true },
lat: {type: Number },
lon: {type: Number },
url: {type: String },
qrcode: {type: String },
questions: [Questions],
timestamp: {type: Date, default: Date.now },
});
var PresentationModel = mongoose.model('Presentation', Presentation);
// @i: simple counter
var i = 1000;
everyone.now.serverRoomsList = {"presentationid": "Presentation Title"};
// create presentation
app.post('/api/presentations', function(req, res){
var presentation, hash = hashids.encrypt(i);
// hashid
// url, qrcode
presentation = new PresentationModel({
id: i,
title: req.body.title,
lat: req.body.lat,
lon: req.body.lon,
});
presentation.url = req.headers.host + "/" + presentation._id + "/";
// add presentation to global list
everyone.now.serverRoomsList[presentation._id] = presentation.title;
console.log(everyone.now.serverRoomsList);
presentation.save(function (err) {
if (!err) {
presentation.questions.push();
i++; // increment counter
return console.log("created");
} else {
return console.log(err);
}
});
return res.send(presentation);
});
// list all presentations
app.get('/api/presentations', function (req, res) {
return PresentationModel.find(function (err, presentations) {
if (!err) {
return res.send(presentations);
} else {
return console.log(err);
}
});
});
// remove presentation
app.delete('/api/presentation/:id', function (req, res) {
return PresentationModel.findById(req.params.id, function (err, presentation) {
return presentation.remove(function (err) {
if (!err) {
console.log("removed");
return res.send('');
} else {
console.log(err);
}
});
});
});
// retrieve presentation
app.get('/api/presentation/:id', function(req, res) {
return PresentationModel.findById(req.params.id, function(err, presentation) {
if (!err) {
return res.send(presentation);
} else {
console.log(err);
}
});
});
// add question
app.post('/api/presentation/:id' , function(req, res) {
return PresentationModel.findById(req.params.id, function(err, presentation) {
if(!err) {
console.log(req.body.question + " " + req.body.creator );
var questions = presentation.questions;
questions.push({"name": req.body.question, "creator": req.body.creator});
presentation.save(function(err) {
if (!err) {
console.log("Succesfully added question")
return res.send(presentation);
} else {
console.log(err)
}
});
} else {
return console.log(err);
}
});
});
// increment votes for question
app.put('/api/presentation/:id', function(req, res){
PresentationModel.update( {"_id": req.params.id ,"questions._id": req.body.questionId}, { $inc: { "questions.$.votes": 1 } }, function(err, numAffected ) {
if (err) {
console.log("Error on update");
console.log(err);
} else {
console.log("updated num: " + numAffected);
}
});
});
// most favorite questions
app.get('/api/presentation/:id/favorites', function(req, res) {
return PresentationModel.findById(req.params.id, function(err, presentation) {
if (!err) {
// sort descending
function compare(a,b) {
if (a.votes < b.votes)
return 1;
if (a.votes > b.votes)
return -1;
return 0;
}
var favorites, questions = presentation.questions;
favorites = questions.sort(compare).slice(0,9);
return res.send(favorites);
} else {
console.log(err);
}
});
});
app.get('/api', function(req, res) {
return res.render('api');
});
app.get('/:id/', function(req, res) {
PresentationModel.findById(req.params.id, function(err, presentation) {
if (!err) {
return res.render('app2', {presentation: presentation});
} else {
console.log(err);
}
});
});
app.get('/', function(req, res) {
return res.render('app');
});
app.get('/index.html', function(req, res) {
res.render('index.html', {});
});
app.get("/manifest.appcache", function(req, res){
manifesto.fetch('./public/test-manifest.appcache', '.', function(err, data) {
if(err) {
res.header("Content-Type", "text/plain");
res.send(404, "Something mising!");
return;
}
res.header("Content-Type", "text/cache-manifest");
res.end(data);
});
});
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve redirecting
// the user to google.com. After authenticating, Google will redirect the
// user back to this application at /auth/google/return
app.get('/auth/google',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
// GET /auth/google/return
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/google/return',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
// Send message to everyone in the users group
everyone.now.distributeMessage = function(message){
var group = nowjs.getGroup(this.now.serverRoom);
group.now.receiveMessage(this.now.name, message);
};
everyone.now.changeRoom = function(newRoom) {
var oldRoom = this.now.serverRoom;
console.log('Changed user '+this.now.name + ' from '+oldRoom + ' to '+newRoom);
//if old room is not null; then leave the old room
if(oldRoom){
var oldGroup = nowjs.getGroup(oldRoom);
oldGroup.removeUser(this.user.clientId);
// Tell everyone he left :)
// oldGroup.now.receiveMessage('SERVER@'+oldRoom, this.now.name + ' has left the room and gone to '+newRoom);
}
var newGroup = nowjs.getGroup(newRoom);
newGroup.addUser(this.user.clientId);
// Tell everyone he joined
// newGroup.now.receiveMessage('SERVER@'+newRoom, this.now.name + ' has joined the room');
this.now.serverRoom = newRoom;
};
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}