-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-prod.js
More file actions
601 lines (444 loc) · 14.4 KB
/
Copy pathapp-prod.js
File metadata and controls
601 lines (444 loc) · 14.4 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// NOTE NOTE NOTE Test case for simple scrape data
const ejs = require('ejs')
const http = require('http');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const Sequelize = require('sequelize')
const passport = require('passport');
const {Client} = require('pg')
const Strategy = require('passport-local').Strategy;
const cookieParser = require('cookie-parser');
const app = express();
const stopFetch = require('./components/stopFetch.js');
const getFeed = require('./components/getFeed.js');
const getFavs = require('./components/getFavTimes.js');
const getService = require('./components/getService.js');
const dotenv = require('dotenv');
const result = dotenv.config();
const flash = require('connect-flash');
app.use(flash());
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static('public'));
const PORT = process.env.PORT || 3000
const Op = Sequelize.Op
const sequelize = new Sequelize(process.env.DATABASE_URL, {
// const sequelize = new Sequelize('barkspace', 'postgres', 'Giraffes94', {
logging: true,
ssl: true,
dialect: 'postgres',
protocol: 'postgres',
operatorsAliases:{
$and: Op.and,
$or: Op.or,
$eq: Op.eq,
$regexp: Op.regexp,
$iRegexp: Op.iRegexp,
$like: Op.like,
$iLike: Op.iLike
}
})
//____________________________________CREATE A TABLE
const User = sequelize.define('user', {
username: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING,
favorites: Sequelize.JSON
});
sequelize.sync()
// ----------------------------------------------------------------------------- PASSPORT JS INIT
app.use(cookieParser());
app.use(require('express-session')({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));
passport.use(new Strategy(
(username, password, cb) => {
// NOTE User / Password confirmation for passportJS login
// use squelize search for first data entry with username feild match
User.findOne({
where: {
username: {
$iLike: `${username}`
}
}
}).then(data => {
if (!data) {
return cb(null, false);
} else if (data.password !== password) {
return cb(null, false);
}
return cb(null, data);
});
}
));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, cb) {
// NOTE?? gets user data from previously defined local strategy, pushes to
// user parameter. first callback param is error throw?
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
User.findById(id).then(data => {
if (!data) {
return cb(null, null);
}
cb(null, data);
});
});
// ----------------------------------------------------------------------------- PASSPORT JS INIT
// mta.stop('A20').then(function (result) {
// console.log(result);
// });
var Mta = require('mta-gtfs');
var mta = new Mta({
key: '59226e4f5d3fa1244428c5b178a32d47', // only needed for mta.schedule() method
feed_id: 1 // optional, default = 1
});
// Static list of posible train lines
let lineNames = ['1', '2', '3', '4', '5', '6', '7', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'J', 'L', 'M', 'N', 'Q', 'R', 'S', 'W', 'Z', 'SIR'];
var dataPull;
// Home route displaying lines and user defined favoried stops
app.get('/', (req, res) => {
tempLogin = ''
if (req.user) {
tempLogin = req.user.username;
// Sets continuous time updates for favorites
dataPull = setInterval(function() {
getFavs.getFavs(req);
}, 15000);
// Session killer after 10 minutes
setTimeout(function() {
clearInterval(dataPull);
}, 600000);
let data = JSON.parse(req.user.favorites);
let favs = data.favorites;
// fetches the time information for user's selected favorites
// and pushes them to a public data file for that user
getFavs.getFavs(req);
getService.status(favs,tempLogin);
return res.render('home', {
favs: favs,
lineNames: lineNames,
tempLogin: tempLogin
});
}
return res.render('home', {
lineNames: lineNames,
tempLogin: tempLogin,
favs: false
});
});
// Line page with designated line as parameter
app.get('/lines/:line', (req, res) => {
var line = req.params.line;
// references a swtch case for parsing the MTA feeds that correspond with
// specific lines.
var feedId = getFeed.getFeedId(line);
if (req.user) {
tempLogin = req.user.username
}
getStops(feedId, line, res);
});
// Reads JSON file with the lists of stopIds and station names.
// On completion renders the line page
var getStops = function(feedId, line, res) {
fs.readFile(`StaticData/FullSimple.json`, 'utf-8', function(err, result) {
let stops = JSON.parse(result);
return res.render('line', {
line: line,
stops: stops[`line${line}`],
feedId: feedId,
tempLogin: tempLogin
});
});
}
app.get('/stops/:stop&:feedId&:stationName&:line', (req, res) => {
if (req.user) {
var username = req.user.username;
} else {
var username = '';
}
let thisLine = req.params.line.toString();
let thisStop = req.params.stop.toString();
let thisFeed = req.params.feedId.toString();
let stationName = req.params.stationName.split('+').join(' ');
// Get the stop and line from req params and use them to plug in the correct datapoint in the stops.json
// data file. Use the data-tester to make a dictionary, use dictionary as href and call the stop.
mta.schedule(thisStop, thisFeed).then(function(result) {
if (Object.keys(result).length === 0) {
console.log('no data');
return res.render('stop', {
errorReport: 'Alas! No times are available',
stationName: stationName,
stop: thisStop,
feed: thisFeed,
line: thisLine,
username: username,
tempLogin: username
});
}
// Parses the MTA data into readable time estimates and collects information
// on the train type. Arbitrarily limited to three results right now.
let station = stopFetch.fetch(thisStop, thisFeed, result);
station.name = stationName;
return res.render('stop', {
errorReport: '',
station: station,
stationName: stationName,
stop: thisStop,
feed: thisFeed,
line: thisLine,
username: username,
tempLogin: username
});
}).catch(x => console.log(x));
});
// route for registration page
app.get('/register', (req, res) => {
tempLogin = '';
res.render('register', {
tempLogin:tempLogin,
errMsg: ''
});
});
app.post('/register', (req, res) => {
// A few small checks for data consistancy and error parseing
let username = req.body.username;
let specialCharacters = /\W|_/g
if (specialCharacters.test(username)) {
return res.render('register',{errMsg:'Special characters and spaces are not allowed in the username.'});
}
if (req.body.password !== req.body.password2) {
return res.render('register',{errMsg:'Password confirmation did not match original'});
}
User.create({
username: req.body.username,
password: req.body.password,
email: req.body.email,
favorites: '{ "favorites": [] }'
}).then(x => {
res.redirect(`/confirmation/${req.body.username}&${req.body.password}`);
})
});
app.get('/confirmation/:username&:password', (req, res) => {
res.render('confirmation', {
username: req.params.username,
password: req.params.password,
tempLogin: tempLogin
});
});
app.post('/confirmation', passport.authenticate('local', { failureRedirect: '/login'}), (req, res) => {
// Confirmation page functions as an intermediate step between new user
// creation and login. I couldn't think of a way to do it on one page, so I
// made it two. Unfortunately it is reletively unsafe without password
// encryption.
res.redirect('/');
});
app.get('/login', (req, res) => {
let tempLogin = ''
let errMsg = ''
// Package for storing cookies quickly?
var errors = req.flash();
if (errors.error) {
errMsg = errors.error[0];
}
res.render('login', {
tempLogin:tempLogin,
errMsg: errMsg
});
});
app.post('/login', passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Invalid username or password.'
}), (req, res) => {
// Credetials are validated in the passport strategy defined earlier in the app.js
res.redirect('/');
});
app.get('/logout', (req, res) => {
tempLogin = req.user.username;
// Delete static data jsons on logout or window close.
if (fs.existsSync(`./public/data/${tempLogin}_favoriteStopTimes.json`)) {
fs.unlinkSync(`./public/data/${tempLogin}_favoriteStopTimes.json`);
}
tempLogin = '';
req.logout();
res.redirect('/');
});
// Route for adding favorites to user accounts where data is stored in a JSON.
// pg database serves as permanent storage, temp jsons are made per user to store
// time data and pass it from persistant server refreshes to ajax calls from the
// client.
app.post('/favorite', (req, res) => {
if (!req.user) {
return res.redirect('/login');
}
User.findOne({
where: {
username: {
$iLike: `${req.body.username}`
}
}
}).then(user => {
console.log('----------------------------------- Creating new Favorite ------------------------------------');
var favs = JSON.parse(user.dataValues.favorites);
if (!Object.keys(favs.favorites).includes(req.body.stopId)) {
let newFav = {};
newFav.stopId = req.body.stopId;
newFav.feedId = req.body.feedId;
newFav.line = req.body.line;
newFav.stationName = req.body.stationName;
favs.favorites.push(newFav);
favs = JSON.stringify(favs);
console.log(favs);
console.log('^ ---------- favs post-join --------- ^');
// write to database for persistance
user.updateAttributes({
favorites: favs
}).then(user => {
// write static file for client reference
fs.writeFile(`./public/data/${tempLogin}_favoriteStopTimes.json`, favs, (err) => {
console.log('-------------------------------------- New Static file for user '+ tempLogin +' for new favoirte ------------------------------------------');
if (err) {
console.log(err);
}
return res.redirect('/');
});
});
}
});
});
// Get index of favorite and splice it out of array of favorites, and then modify
// the user data.
app.post('/remove',(req,res)=>{
tempLogin = req.body.username;
favIndex = req.body.favIndex;
console.log(req.user);
User.findOne({
where: {
username: {
$iLike: req.user.username
}
}
}).then(user=>{
var oldFavorites = JSON.parse(user.dataValues.favorites);
console.log(oldFavorites.favorites[favIndex]);
oldFavorites.favorites.splice(favIndex,1);
var newFavorites = oldFavorites
user.updateAttributes({
favorites: JSON.stringify(newFavorites)
}).then(()=>{
fs.writeFile(`./public/data/${tempLogin}_favoriteStopTimes.json`, JSON.stringify(newFavorites), (err) => {
console.log('-------------------------------------- New Static file for user '+ tempLogin +' without erased favoirte ------------------------------------------');
if (err) {
console.log(err);
}
if (req.headers.referer.split('/').includes('profile')) {
res.redirect(`/profile/${req.user.username}`);
} else {
res.redirect('/');
}
});
})
})
});
app.get('/get-stops',(req,res)=>{
// Full stop data for favorite selector
let stops = fs.readFileSync(`StaticData/FullSimple.json`, 'utf-8', function(err, result) { return result });
res.send(stops);
});
app.post('/favorite-select',(req,res)=>{
let line = req.body.line;
let stopInfo = req.body.stops;
stopInfo = stopInfo.split(',');
let stopId = stopInfo[0];
let stopName = stopInfo[1].split('%20').join(' ');
let username = req.user.username;
let feedId = getFeed.getFeedId(line).toString();
let newFav = {
stopId: stopId,
feedId: feedId,
line: line,
stationName: stopName
}
User.findOne({
where: {
username: {
$iLike: username
}
}
}).then(user=>{
let oldFavorites = user.dataValues.favorites;
newFavorites = JSON.parse(oldFavorites);
newFavorites.favorites.push(newFav);
user.updateAttributes({
favorites: JSON.stringify(newFavorites)
}).then(()=>{
if (req.headers.referer.split('/').includes('profile')) {
res.redirect(`/profile/${req.user.username}`);
} else {
res.redirect('/');
}
})
})
})
// A post that should kill the interval that continuously pulls data for the
// homepage/favorites section
app.post('/stopData', (req, res) => {
console.log('Clear Interval post called');
clearInterval(dataPull);
});
app.get('/profile/:username', require('connect-ensure-login').ensureLoggedIn('/login'), (req,res)=>{
// get favs, list them in a container to be organized.
favorites = JSON.parse(req.user.favorites);
res.render('profile', {tempLogin:req.user.username,lineNames:lineNames, favorites:favorites.favorites});
});
app.post('/edit',(req,res)=>{
// Gets an array with the order of the re-arranged stops and maps a new
// array of favorite values to sortedFavs. Then we find the user's profile in
// postgress and update the order. NOTE need to update the static file.
let order = req.body['order[]'];
let favorites = JSON.parse(req.user.favorites);
let sortedFavs = order.map((x,i)=>{
for (favorite of favorites.favorites) {
if (favorite.stopId === x) {
return favorite;
}
}
});
let newData = {favorites:sortedFavs}
User.findOne({
where:{
username:{
$like:req.user.username
}
}
}).then(user=>{
user.updateAttributes({
favorites: JSON.stringify(newData)
}).then(()=>{
fs.writeFile(`./public/data/${req.user.username}_favoriteStopTimes.json`, JSON.stringify(newData), (err) => {
console.log('-------------------------------------- New Static file for user '+ req.user.username +' after re-order ------------------------------------------');
if (err) {
console.log(err);
}
var response = {
status : 200,
success : 'Updated Successfully'
}
res.end(JSON.stringify(response));
});
})
})
});
// NOTE: Always check the PORT
app.listen(PORT, function(err) {
if (err) throw err;
console.log('Subway++ is here!');
});