-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (162 loc) · 5.15 KB
/
index.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
const express = require('express');
const haversine = require('haversine-distance');
const session = require('express-session');
const app = express();
const port = 8080;
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/views/scripts'));
const bodyParser = require('body-parser');
// Rellit favicon
const favicon = require('serve-favicon');
app.use(favicon(__dirname + '/public/images/favicon.ico'));
// Imports the Google Cloud client library
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const insertRow = require('./lib/insertRow');
// Allow express to read the POST request body
app.use(express.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(session({
secret: 'secret-key',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 30*60*1000 }, // 30-minute sessions
}));
// Starts a session and directs the user to the questions page
app.post('/start-session', async (req, res) => {
const sesh = req.session;
if (sesh) {
sesh.long = req.body.long;
sesh.lat = req.body.lat;
}
else {
console.log("session unavailable. Are you logged in?");
}
// return res.redirect('/');
res.redirect('back');
});
app.get('/', async (req, res) => {
const sesh = req.session;
let sessionStatus;
if (sesh) {
sessionStatus = "Session Started!"
} else {
sessionStatus = "Session is not active";
}
const queryGetQuestions = datastore
.createQuery("Question")
.order("time", {
descending: true
});
const [questions] = await datastore.runQuery(queryGetQuestions);
if (sesh && sesh.lat && sesh.long) {
const viewerCoords = { latitude: sesh.lat, longitude: sesh.long };
function getShorterDistance(a, b) {
if (a.lat && a.long && b.lat && b.long) {
const aCoords = { latitude: a.lat, longitude: a.long };
const bCoords = { latitude: b.lat, longitude: b.long };
if (haversine(viewerCoords, aCoords) < haversine(viewerCoords, bCoords))
return -1;
return 1;
}
return 0;
}
questions.sort(getShorterDistance);
}
else {
console.log("Viewer location unavailable. Sorted by time");
}
const questionsAndReplies = await Promise.all(questions.map(async q => {
const questionId = q[datastore.KEY].id;
const queryGetReplies = datastore.createQuery("Reply")
.filter("questionId", "=", questionId)
.order("time", {
descending: true
});
const [replies] = await datastore.runQuery(queryGetReplies);
return {
"questionId": questionId,
"text": q.text,
"time": q.time,
"replies": replies,
};
}));
res.render('index', { data: questionsAndReplies, sessionStuff: sessionStatus});
});
app.post('/new-question', async (req, res) => {
const body = {
text: req.body.question,
time: new Date(),
long: req.body.long,
lat: req.body.lat,
};
await insertRow.insert("Question", body);
return res.status(200).redirect("/");
});
app.post('/new-meetup', async (req, res) => {
try {
const meetup = req.body.meetup;
const body = {
text: meetup,
time: new Date(),
};
await insertRow.insert("MeetUp", body);
res.status(200).render('index');
}
catch {
res.status(500).json({ message: "internal server error 500" });
}
});
app.post('/reply', async (req, res) => {
let body = req.body;
body["time"] = new Date();
await insertRow.insert("Reply", body);
return res.redirect(`/?question=${body.questionId}`);
});
app.post('/login', async (req, res) => {
const body = {
email: req.body.email,
firstname: req.body.given_name,
lastname: req.body.family_name,
profilePicture: req.body.picture,
long: req.body.long_question,
lat: req.body.lat_question,
online: true
};
await insertRow.insert("Users", body);
return res.render("index");
});
app.get("/meet", (req, res) => {
var sessionStatus;
if (req.session) {
sessionStatus = "session active"
} else {
sessionStatus = "session not active";
}
return res.render("meet", {sessionStuff: sessionStatus });
});
app.get("/faq", (req, res) => {
var sesh = "";
return res.render("faq", {sessionStuff: sesh});
});
app.get("/questions", async (req, res) => {
const queryGetQuestions = datastore.createQuery("Question");
const [questions] = await datastore.runQuery(queryGetQuestions);
const questionsWithIds = await Promise.all(questions.map(async q => {
const questionId = q[datastore.KEY].id;
return {
"questionId": questionId,
"text": q.text,
"time": q.time,
"lat": q.lat,
"long": q.long
};
}));
res.json(questionsWithIds);
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});