-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
1055 lines (896 loc) · 33.1 KB
/
server.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// start app with 'npm run dev' in a terminal window
// go to http://localhost:port/ to view your deployment!
// every time you change something in server.js and save, your deployment will automatically reload
// to exit, type 'ctrl + c', then press the enter key in a terminal window
// if you're prompted with 'terminate batch job (y/n)?', type 'y', then press the enter key in the same terminal
// standard modules, loaded from node_modules
const path = require('path');
require("dotenv").config({ path: path.join(process.env.HOME, '.cs304env') });
const express = require('express');
const morgan = require('morgan');
const serveStatic = require('serve-static');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const flash = require('express-flash');
const multer = require('multer');
const bcrypt = require('bcrypt');
const ROUNDS = 15;
const fs = require('node:fs/promises');
// our modules loaded from cwd
const { Connection } = require('./connection');
const cs304 = require('./cs304');
const { devNull } = require('os');
// Create and configure the app
const app = express();
// Morgan reports the final status code of a request's response
app.use(morgan('tiny'));
app.use(cs304.logStartRequest);
// This handles POST data
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cs304.logRequestData); // tell the user about any request data
app.use(flash());
app.use(serveStatic('public'));
app.use(serveStatic('assets'));
app.set('view engine', 'ejs');
const mongoUri = cs304.getMongoUri();
app.use(cookieSession({
name: 'session',
keys: ['horsebattery'],
// Cookie Options
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));
// ================================================================
// database variables
const DB = 'newwithtags';
const POSTS = 'posts';
const LIKES = 'likes';
const USERS = 'users';
const COUNTERS = 'counters';
const CITIES = ['wellesley', 'boston', 'tokyo', 'jakarta', 'delhi', 'mumbai', 'shanghai',
'são paulo', 'seoul', 'mexico city', 'cairo', 'new york city', 'beijing',
'bangkok', 'moscow', 'buenos aires', 'lagos', 'istanbul', 'milan',
'bangalore', 'osaka', 'chengdu', 'tehran', 'rio de jane', 'toronto', 'athens',
'chennai', 'los angeles', 'london', 'paris', 'rome', 'prague', 'sydney', 'lima',
'wuhan', 'nanyang', 'hangzhou', 'amsterdam', 'dubai', 'dublin', 'stockholm', 'cairo',
'nagoya', 'taipei', 'berlin', 'washington D.C.', 'vienna', 'lisbon', 'edinburgh',
'chicago', 'nanjing', 'fuyang', 'montreal', 'vilnius', 'frankfurt', 'vancouver',
'johannesburg', 'bogotá', 'melbourne', 'venice', 'hong kong', 'santiago', 'las vegas',
'madrid', 'baghdad', 'singapore', 'san francisco', 'honolulu', 'munich',
'houston', 'barcelona', 'copenhagen', 'miami'];
const numButtons = 5;
// ================================================================
// file upload functions and variables
/**
* Returns formatted time as a string
* @param {date object} dateObj
* @returns formatted time string
*/
function timeString(dateObj) {
if (!dateObj) {
dateObj = new Date();
};
d2 = (val) => val < 10 ? '0' + val : '' + val;
let hh = d2(dateObj.getHours());
let mm = d2(dateObj.getMinutes());
let ss = d2(dateObj.getSeconds());
return hh + mm + ss;
}
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/assets/uploads/')
},
filename: function (req, file, cb) {
let parts = file.originalname.split('.');
let ext = parts[parts.length - 1];
let hhmmss = timeString();
cb(null, file.fieldname + '-' + hhmmss + '.' + ext);
}
});
var upload = multer({
storage: storage,
// max fileSize in bytes
limits: { fileSize: 100_000_000 }
});
// ================================================================
// helper functions
/**
* Increases the counter document in the counters collection
* associated with the given key by 1.
* @param {collection} counters
* @param {string} key
* @returns the document after the update
*/
async function incrCounter(counters, key) {
let result = await counters.findOneAndUpdate({ collection: key },
{ $inc: { counter: 1 } },
{ returnDocument: "after" });
return result.counter;
};
/**
* Function to return the first n most popular cities and tags.
* @returns an array of cities and an array of tags, both of length n.
*/
async function getNumCitiesAndTags(n) {
let sortedCities = await sortCitiesByNumPosts(n);
let sortedTags = await sortTagsByNumPosts(n);
console.log("sortedCities", sortedCities)
console.log("sortedTags", sortedTags)
return [sortedCities, sortedTags];
};
/**
* Function to sort all posts in database by likes in decreasing order
* @returns an array of sorted posts
*/
async function sortPostsByLikes () {
const db = await Connection.open(mongoUri, DB); // connects to newwithtags database
const posts = db.collection(POSTS);
let sortedPosts = await posts.aggregate([
{
$lookup: {
from: "likes",
localField: "postID",
foreignField: "postID",
as: "likes"
}
},
{
$addFields: {
numLikes: { $size: "$likes" }
}
},
{
$sort: {numLikes: -1} // sort posts by num of likes in decr. order
}
]).toArray();
return sortedPosts;
};
/**
* Function to sort all posts by date created, in descending order from most liked to least liked
* @returns an array
*/
async function sortPostsByNewest () {
const db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
let sortedPosts = await posts.aggregate([
{
$sort: {date: -1} // sort posts by num of likes in decr. order
}
]).toArray();
return sortedPosts;
};
/**
* Function to sort all the cities that are both used and not
* used in the database by most used to least used.
* @returns an array of sorted cities as strings
*/
async function sortCitiesByNumPosts(n) {
const db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
let sortedCityDicts = await posts.aggregate([
{
$group: { _id: "$city", count: { $sum: 1 } }
},
{
$sort: { count: -1 }
},
{
$project: { city: 1 }
}
]).toArray();
let sortedPresentCities = [];
sortedCityDicts.forEach((elt) => {
sortedPresentCities.push(elt._id);
});
let absentCities = CITIES.filter((elt) => {
return !sortedPresentCities.includes(elt);
});
let sortedCities = sortedPresentCities.concat(absentCities);
return capitalizeCities(sortedCities.slice(0, n));
};
/**
* Function to sort all the tags used in the database by most used to least used.
* @returns an array of sorted tags and the number of times they're used.
*/
async function sortTagsByNumPosts(n) {
const db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
let sortedTagDicts = await posts.aggregate([
{
$unwind: "$tags"
},
{
$group: { _id: "$tags", count: { $sum: 1 } }
},
{
$sort: { count: -1 }
},
{
$project: { tags: 1, count: 1 }
}
]).limit(n).toArray();
let sortedTags = [];
sortedTagDicts.forEach((elt) => {
sortedTags.push(elt._id);
});
return sortedTags;
};
/**
* Function to capitalize all words in a single city string
* @param {string} city
* @returns properly-capitalized string city
*/
function capitalizeCity(city) {
let words = city.split(' ');
newWords = words.map((word) => {
return word.charAt(0).toUpperCase() + word.slice(1)
});
return newWords.join(' ');
}
/**
* Function to capitalize all words in all cities in an array
* @param {array} cities
* @returns a new array of newly-capitalized cities
*/
function capitalizeCities(cities) {
let capitalizedCities = [];
cities.forEach((city) => {
capitalizedCities.push(capitalizeCity(city));
});
return capitalizedCities;
}
/**
* Function to get the current date and time as a string in the format "YY-MM-DD HH-MM-SS"
* @returns the current date and time as a formatted string
*/
function getDateAndTime() {
let dateObj = new Date();
let the_day = dateObj.getDate();
let the_month = dateObj.getMonth() + 1; // Add 1 because Jan is 0, etc.
let the_year = dateObj.getFullYear();
let the_hour = dateObj.getHours();
let the_minute = dateObj.getMinutes();
let the_second = dateObj.getSeconds();
let date = the_year + "-" + the_month + "-" + the_day + " " + the_hour + ":" + the_minute + ":" + the_second;
return date;
};
// helper function to create a new user
async function insertUser(username, firstName, lastName, email, password) {
const db = await Connection.open(mongoUri, DB);
let dateObj = new Date();
let the_day = dateObj.getDate();
let the_month = dateObj.getMonth() + 1; // Add 1 because Jan is 0, etc.
let the_year = dateObj.getFullYear();
let date = the_year + "-" + the_month + "-" + the_day
// insert if username does not exist
let users = db.collection(USERS)
await users.updateOne({
userID: username
},
{
$setOnInsert: {
userID: username,
userFirstName: firstName,
userLastName: lastName,
password: password,
email: email,
dateCreated: date
}
},
{ upsert: true });
};
// ================================================================
// custom routes here
// Main page
app.get('/', async (req, res) => {
let uid = req.session.uid || false;
let logged_in = req.session.logged_in || false;
console.log('uid', uid);
let sortedPostsByLiked = await sortPostsByLikes();
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
let capitalizedSortedCities = capitalizeCities(sortedCities);
console.log("capitalizedSortedCities", capitalizedSortedCities)
return res.render('index.ejs', {
uid: uid,
logged_in: logged_in,
posts: sortedPostsByLiked,
cities: capitalizedSortedCities,
tags: sortedTags
});
});
/**
* Action Get from Search Bar Form for Filtering Tags or Cities
* Redirects URL to filters image gallery with certain tags/cities,
* Flashes error message if none found.
*/
app.get('/search/', async (req, res) => {
const search = req.query.search;
// Handles Error when Search is null or undefined
if (!search) {
console.log("Search is missing from query string; ignoring this request to /search");
return res.redirect('/');
};
const searched = search.toLowerCase();
const db = await Connection.open(mongoUri, DB);
const postsDB = db.collection(POSTS);
// Recognizes if search contained multiple items
if (searched.includes(",")) {
console.log("You are searching multiple tags!")
var searchList = searched.split(",");
if (searchList[0].includes("#")) { //filters multiple tags
var tagList = [];
searchList.forEach((elt) => {
tagList.push(elt.split("#")[1])
})
let findTags = await postsDB.find({ tags: { $in: tagList } }).toArray();
if (findTags.length == 0) {
req.flash("error", "Sorry, these tags do not exist.")
return res.redirect('/');
} else {
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
return res.render('index.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: findTags,
cities: sortedCities,
tags: sortedTags
});
}
} else {
console.log("You are searching a city and tags!")
const searchedCity = searchList[0].toLowerCase();
var tagList = [];
var restSearch = searchList.slice(1);
restSearch.forEach((elt) => {
tagList.push(elt.split("#")[1])
})
// Look ups posts that have city and tags of either in tagList
let findPosts = await postsDB.find(
{ $and: [{ city: searchedCity }, { tags: { $in: tagList } }] }
).toArray();
if (findPosts.length == 0) {
req.flash("error", "Sorry, these tags do not exist.")
return res.redirect('/');
} else {
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
return res.render('index.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: findPosts,
cities: sortedCities,
tags: sortedTags
});
}
}
}
// Handles individual searches of city or tag
if (searched.charAt(0) == "#") {
console.log(`You submitted ${searched}`);
const styleTag = searched.split("#")[1];
let findTag = await postsDB.find({ tags: styleTag }).toArray();
if (findTag.length == 0) {
req.flash("error", "Sorry, this tag does not exist.")
return res.redirect('/');
} else {
let redirectURL = "/tag/" + styleTag;
res.redirect(redirectURL);
}
} else {
console.log(`You submitted ${searched}`);
let findCity = await postsDB.find({ city: searched }).toArray();
if (findCity.length == 0) {
req.flash('error', "Sorry, this city does not exist.");
return res.redirect('/');
} else {
let redirectURL = "/city/" + searched;
res.redirect(redirectURL);
};
}
});
/**
* Renders Filtered Image City Gallery
* Recalculates the top 5 cities when rendered
*/
app.get('/city/:city', async (req, res) => {
let city = req.params.city.toLowerCase();
const db = await Connection.open(mongoUri, DB);
const postsDB = db.collection(POSTS);
let findCity = await postsDB.find({ city: city }).toArray();
if (findCity.length == 0) {
req.flash("error", "Sorry, this city does not exist.")
return res.redirect('/');
} else {
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
return res.render('index.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: findCity,
cities: sortedCities,
tags: sortedTags
});
}
});
/**
* Renders Filtered Tagged Image Gallery
* Recalculates the top 5 tags when rendered
* have it be query string instead of limiting to one tag
*/
app.get('/tag/:tag', async (req, res) => {
const styleTag = req.params.tag;
const db = await Connection.open(mongoUri, DB);
const postsDB = db.collection(POSTS);
let findTag = await postsDB.find({ tags: styleTag }).toArray();
if (findTag.length == 0) {
req.flash("error", "Sorry, this tag does not exist.")
return res.redirect('/');
} else {
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
return res.render('index.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: findTag,
cities: sortedCities,
tags: sortedTags
});
};
});
// Get to sort posts on the home page by either newest or most liked.
app.get('/sort', async (req,res) => {
let sortBy = req.query.sortBy;
console.log(sortBy);
let uid = req.session.uid || false;
let logged_in = req.session.logged_in || false;
console.log('uid', uid);
let [sortedCities, sortedTags] = await getNumCitiesAndTags(numButtons);
// Sort by Newest
if (sortBy == "Newest") {
console.log('newest');
let sortedPostsByNewest = await sortPostsByNewest();
return res.render('index.ejs', {uid: uid,
logged_in: logged_in,
posts: sortedPostsByNewest,
cities: sortedCities,
tags: sortedTags});
// Sort by Liked
} else if (sortBy == "Liked") {
console.log('liked');
let sortedPostsByLiked = await sortPostsByLikes();
return res.render('index.ejs', {uid: uid,
logged_in: logged_in,
posts: sortedPostsByLiked,
cities: sortedCities,
tags: sortedTags});
} else {
req.flash("error", "Please select a sorting method.");
return res.redirect('/');
};
});
// get for /post-single/:id for a specific post
app.get('/post-single/:id', async (req, res) => {
const postID = parseInt(req.params.id);
const db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
let findPost = await posts.findOne({ postID: postID });
// calculate the number of likes
const likes = db.collection(LIKES);
var matched = await likes.find({ postID: postID }).toArray();
var numLikes = matched.length
let likeString = "Likes";
if (numLikes == 1) {
likeString = "Like";
};
let uid = req.session.uid || false;
let logged_in = req.session.logged_in || false;
console.log("CURRENT LOGIN: ", uid, logged_in)
if (logged_in) {
// check if this user has already liked the post
var alreadyLiked = true;
var userLiked = await likes.findOne({ postID: postID, userID: uid, });
if (userLiked == null) {
// if the current user has not liked the post, it is not alreadyLiked
alreadyLiked = false;
}
} else {
alreadyLiked = false;
}
return res.render('post-single.ejs', {
findPost,
uid: uid,
logged_in: logged_in,
postID: postID,
numLikes: numLikes,
likeString: likeString,
alreadyLiked: alreadyLiked,
capitalizeCity: capitalizeCity
});
});
// Ajax route to like a post. Returns a json to update the page.
app.post('/likeAjax/:id', async (req,res) => {
var uid = req.session.uid || false;
var logged_in = req.session.logged_in || false;
var postID = parseInt(req.params.id);
if (!logged_in) {
return res.json({error: true, action: "error"});
} else {
var numLikes = await likePost(uid, postID);
return res.json({error: false, likes: numLikes, postID: postID, uid: uid, action: "like"});
}
});
/* Asynchronous helper function to update the database with a new like */
async function likePost(uid, postID) {
const db = await Connection.open(mongoUri, DB);
const likeDoc = await db.collection(LIKES).insertOne(
{ postID: postID, userID: uid },
{ upsert: false });
console.log("User with ID", uid, "liked post with ID", postID);
console.log(likeDoc);
var matched = await db.collection(LIKES).find({ postID: postID }).toArray();
var numLikes = matched.length;
return numLikes;
}
// Ajax route to unlike a post. Returns a json to update the page.
app.post('/unlikeAjax/:id', async (req,res) => {
var uid = req.session.uid || false;
var logged_in = req.session.logged_in || false;
var postID = parseInt(req.params.id);
if (!logged_in) {
return res.json({error: true, action: "error"});
} else {
var numLikes = await unlikePost(uid, postID);
return res.json({error: false, likes: numLikes, postID: postID, uid: uid, action: "unlike"});
}
});
/* Asynchronous helper function to delete a like from the database */
async function unlikePost(uid, postID) {
const db = await Connection.open(mongoUri, DB);
var unlikeDoc = await db.collection(LIKES).deleteOne(
{ postID: postID, userID: uid });
console.log("User with ID", uid, "unliked post with ID", postID);
console.log(unlikeDoc);
var matched = await db.collection(LIKES).find({ postID: postID }).toArray();
var numLikes = matched.length;
return numLikes;
}
/**
* Renders the create page to make a new post.
* If the user is not logged in, redirects to the home and flashes an error message.
*/
app.get('/create', async (req, res) => {
if (req.session.logged_in == true) {
let sortedCities = await sortCitiesByNumPosts(CITIES.length);
console.log(sortedCities)
return res.render('create.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
cityList: sortedCities
});
} else {
req.flash('error', "You are not logged in. Please log in to create a post.");
return res.redirect("/login");
};
});
/**
* Post request to update the database with a new post created with the create page.
* Handles file upload and flashes an error if the upload fails.
*/
app.post('/create', upload.single('imageUpload'), async (req, res) => {
const uid = req.session.uid;
let db = await Connection.open(mongoUri, DB);
let counters = db.collection(COUNTERS);
let postID = await incrCounter(counters, POSTS);
let posts = db.collection(POSTS);
let city = req.body.city.toLowerCase();
console.log(req.body)
console.log(city)
let tagsInitial = req.body.tags.split(" ");
let description = req.body.description;
console.log("description", description)
let alttext = req.body.alttext;
let imageUpload = './public/assets/uploads/' + req.file.filename;
// change the permissions of the file to be world-readable
let val = await fs.chmod(imageUpload, 0o664);
console.log('chmod val', val);
console.log('file', req.file);
// filter out any tags that don't start with # or are just #
let tagsWithHash = tagsInitial.filter((elt) => elt[0] == '#' && elt != '#');
// find all the unique tags
let uniqueTags = [];
tagsWithHash.forEach((elt) => {
if (!uniqueTags.includes(elt)) {
uniqueTags.push(elt);
};
});
// get rid of # in front of tags
let tags = uniqueTags.map((elt) => { return elt.slice(1) });
let date = getDateAndTime();
let insertPost = await posts.insertOne({
postID: postID,
userID: uid,
imageURL: "../assets/uploads/" + req.file.filename,
comments: [],
tags: tags,
city: city,
date: date,
caption: description,
alttext: alttext
});
console.log(insertPost);
if (insertPost.acknowledged) { // if successfully inserted, redirect to the new post's post-single page
console.log('successfully inserted post');
req.flash("info", "Successfully posted.");
return res.redirect('/post-single/' + postID);
} else {
console.log('failed to insert post');
req.flash("error", "Post failed. Please try again.");
return res.redirect('/create');
};
});
// Get for /edit/:id to render the edit page.
app.get('/edit/:id', async (req, res) => {
let postID = parseInt(req.params.id);
let db = await Connection.open(mongoUri, DB);
let posts = db.collection(POSTS);
let post = await posts.findOne({ postID: postID });
let sortedCities = await sortCitiesByNumPosts(CITIES.length);
if (req.session.logged_in == true && req.session.uid == post.userID) {
return res.render('edit.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
cityList: sortedCities,
post: post
});
} else {
if (req.session.logged_in != true) {
req.flash('error', "You are not logged in. Please log in to edit this post.");
return res.redirect("/login");
} else if (req.session.uid != post.userID) {
req.flash('error', "You do not have permission to edit this post.");
return res.redirect("/");
};
};
});
// Post for /edit/:id to get form data and actually edit the post.
app.post('/edit/:id', async (req, res) => {
const postID = parseInt(req.params.id);
let db = await Connection.open(mongoUri, DB);
let posts = db.collection(POSTS);
let oldPost = await posts.findOne({ postID: postID });
if (!oldPost) {
req.flash('error', 'Post not found.');
res.redirect('/');
};
let city = req.body.city.toLowerCase();
let tagsInitial = req.body.tags.split(" ");
let caption = req.body.description;
let tagsWithHash = tagsInitial.filter((elt) => elt[0] == '#' && elt != '#');
let tagsWithoutHash = tagsWithHash.map((elt) => { return elt.slice(1) });
let tags = [];
tagsWithoutHash.forEach((elt) => { // get rid of duplicate tags
if (!tags.includes(elt)) {
tags.push(elt);
};
});
let editPost = await posts.updateOne({ postID: postID },
{
$set: {
tags: tags,
city: city,
caption: caption
}
});
if (editPost.acknowledged) {
// if successfully edited, redirect to the new post's post-single page
req.flash("info", "Successfully edited.");
return res.redirect('/post-single/' + postID);
} else {
req.flash("error", "Edit failed. Please try again.");
return res.redirect('/edit/' + postID);
};
});
/* Confirmation page to delete a post */
app.get("/delete/:id", async (req, res) => {
const db = await Connection.open(mongoUri, DB);
var post = await db.collection(POSTS).findOne({ postID: parseInt(req.params.id) });
console.log(post);
return res.render('delete.ejs', {
uid: req.session.uid,
logged_in: req.session.logged_in,
post: post
})
})
/* Functionality to delete a post. Takes the ID of the post to delete.
Removes it from the database and flashes a confirmation message on the home page. */
app.post("/delete/:id", async (req, res) => {
var postID = parseInt(req.params.id);
let db = await Connection.open(mongoUri, DB);
let deleted = await db.collection(POSTS).deleteOne({ postID: postID });
console.log("Deleted", deleted.deletedCount, "post with ID", postID);
req.flash("info", "Deleted post.");
return res.redirect("/");
})
/**
* Post for deleting a comment. Takes the ID of the post to delete and gets the
* text, userID, and date of the comment in the body of the request. Flashes confirmation.
*/
app.post('/delete-comment/:id', async (req, res) => {
const postID = parseInt(req.params.id);
const text = req.body.commentText;
const userID = req.body.userID;
const date = req.body.date;
let db = await Connection.open(mongoUri, DB);
let posts = db.collection(POSTS);
let deleteComment = await posts.updateOne(
{ postID: postID },
{ $pull: { comments: {text: text, userID: userID, date: date} } }
);
if (deleteComment.modifiedCount == true) {
req.flash("info", "Deleted comment.");
} else {
req.flash("error", "Failed to delete comment. Please try again.");
};
return res.redirect("/post-single/" + postID);
})
/**
* Renders the profile page, which shows the logged in user's information and the user's posts.
* If the user is not logged in, redirects to the home and flashes an error message.
*/
app.get('/profile/posted', async (req, res) => {
if (req.session.logged_in) {
const db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
const currentUser = await db.collection(USERS).findOne({ userID: req.session.uid });
console.log("CURRENT USER", currentUser);
let userPosts = await posts.find({ userID: currentUser.userID }).sort({ date: -1 }).toArray();
console.log(userPosts)
return res.render('profile.ejs', {
user: currentUser,
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: userPosts,
action: "Posted"
});
} else {
req.flash('error', "Please log in to view your profile.");
return res.redirect("/login");
};
});
/* On the profile page, get the list of posts that the user has liked.
Then, render the profile page (profile.ejs) with a gallery of liked posts. */
app.get('/profile/liked', async (req, res) => {
const db = await Connection.open(mongoUri, DB);
const likes = db.collection(LIKES);
const posts = db.collection(POSTS);
const currentUser = await db.collection(USERS).findOne({ userID: req.session.uid });
let userLikedPosts = await likes.aggregate([
{
$match: {
userID: req.session.uid
}
},
{
$lookup: {
from: "posts",
localField: "postID",
foreignField: "postID",
as: "likedPosts"
},
},
{
$unwind: "$likedPosts"
},
{
$replaceRoot: {
newRoot: "$likedPosts"
}
}
]).toArray();
console.log(userLikedPosts);
return res.render('profile.ejs', {
user: currentUser,
uid: req.session.uid,
logged_in: req.session.logged_in,
posts: userLikedPosts,
action: "Liked"
});
});
/**
* Post request to handle commenting.
* Adds the comment to the database of comments if the user is logged in.
* If the user is not logged in, redirects to the home and flashes an error message.
*/
app.post('/comment/:postID', async (req, res) => {
if (req.session.logged_in) {
let commentText = req.body.comment;
let user = req.session.uid;
let postID = parseInt(req.params.postID);
let date = getDateAndTime();
let comment = { text: commentText, userID: user, date: date }
let db = await Connection.open(mongoUri, DB);
const posts = db.collection(POSTS);
let addComment = await posts.updateOne(
{ postID: postID },
{ $push: { comments: comment } }
);
console.log(addComment);
return res.redirect("/post-single/" + postID);
} else {
let postID = parseInt(req.params.postID);
req.flash("error", "Please log in to leave a comment.");
return res.redirect("/post-single/" + postID);
}
});
// render login page
app.get("/login", (req, res) => {
let uid = req.session.uid || false;
let logged_in = req.session.logged_in || false;
return res.render("login.ejs", { uid: uid, logged_in: logged_in });
})
/**
* Processes a user login.
* If the user enters an incorrect username or password, flashes an error message.
*/
app.post("/login", async (req, res) => {
username = req.body.username;
password = req.body.password;
const db = await Connection.open(mongoUri, DB);
let userdict = await db.collection(USERS).findOne(
{ userID: username }, { projection: { password: 1 } }
);
if (userdict == null) {
console.log("failed login for", username);
req.session.uid = false;
req.session.logged_in = false;
req.flash("error", "Login failed. Check your username and password and try again.")
return res.redirect("/login");
} else {
let correctPassword = userdict.password;
result = await bcrypt.compare(password, correctPassword);
console.log('login status:', "\t", result);