forked from cs4241-20a/final-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
361 lines (295 loc) · 10.9 KB
/
Copy pathapp.js
File metadata and controls
361 lines (295 loc) · 10.9 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
const express = require('express')
const exphbs = require('express-handlebars')
const {MongoClient} = require('mongodb');
const passport = require("passport");
const GitHubStrategy = require('passport-github').Strategy
const cookieSession = require('cookie-session')
//require('dotenv').config()
const favicon = require("serve-favicon");
const bodyParser = require("body-parser");
const http = require("http");
const ws = require("ws");
const IPinfo = require("node-ipinfo");
const PORT = 3000
const app = express()
const server = http.createServer( app )
let date = new Date()
let formattedDate = date.toLocaleDateString('en-US')
/////////////////////////////// General Middleware ///////////////////////////////
// serve favicon
app.use(favicon(__dirname + "/public/images/favicon.ico"));
// set template rendering engine to use handlebars
app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')
// server static files from dir 'public'
app.use(express.static('public'))
////////////////////////////////// OAuth things //////////////////////////////////
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.GITHUB_CALLBACK_URL
},
async function(accessToken, refreshToken, profile, cb) {
return cb(null, profile)
}
))
passport.serializeUser((user, done)=> {
done(null, user.username) // put username in cookie
})
passport.deserializeUser((username, done)=> {
done(null, getUser(username)) // attach user property to request object
})
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 10000, // login cookies expire after one day
keys: [process.env.COOKIE_KEY] // encrypt cookie based on env variable: COOKIEKEY
}))
app.use(passport.initialize())
app.use(passport.session())
////////////////////////////////// Routes //////////////////////////////////
// ================ GET ================
// home route
app.get('/', (req, res) => {
if (req.user !== undefined && req.user !== null) { // if user has logged in
req.user.then(user => {
// send user data back
res.sendFile(__dirname + "/views/index.html");
})
}
else {
res.sendFile(__dirname + "/views/login.html");
}
})
// in case index.html specified
app.get("/index.html", (req, res) => {
if (req.user !== undefined && req.user !== null) { // if user has logged in
req.user.then(user => {
// send user data back
res.sendFile(__dirname + "/views/index.html");
})
}
else {
res.sendFile(__dirname + "/views/login.html");
}
})
app.get("/gallery.html", (req, res) => {
res.sendFile(__dirname + "/views/gallery.html");
})
app.get('/mydata', (req, res) => {
if (req.user !== undefined && req.user !== null) { // if user has logged in
req.user.then(user => {
// send user data back
res.json(user)
})
}
})
app.get("/inbox", (req, res) => {
if (req.user !== undefined && req.user !== null) { // if user has logged in
req.user.then(user => {
// send user's inbox back
getInbox(user.username).then(drawings => {
let drawingArray = []
drawings.forEach(drawing=>{
drawingArray.push(drawing)
}).then(()=>{
// send drawing data back
res.json(drawingArray)
})
})
})
}
})
app.get('/auth/github', passport.authenticate('github'));
app.get('/auth/github/callback',
passport.authenticate('github', {failureRedirect: '/'}),
function (req, res) {
getUser(req.user.username).then(result => {
if (result === null) {
//get country code for flag
ipToFlagPath(req).then((flagPath) => {
// Create new entry in DB
upsertUser({
username: req.user.username,
avatar: '/images/placeholder_avatar.png', // Some placeholder image here (maybe github icon?)
flag: flagPath,
friends: ["noahvolson", "csmbrad", "excitinglyplain", "carlypereira"]
}).then(res.redirect('/'))
})
} else { // User found
res.redirect('/')
}
})
})
app.get("/logout", (req, res) => {
if (req.user !== undefined) {
req.logOut();
}
res.redirect('/');
})
// ================ POST ================
app.post('/friend', bodyParser.json(), (req, res) => {
getUser(req.body.friendUsername).then(friendData => {
// send friend data back
res.json(friendData)
})
})
app.post('/drawings', bodyParser.json(), (req, res) => {
req.user.then(user => {
getConversation(req.body.artist, user.username).then(drawings => {
let drawingArray = []
drawings.forEach(drawing=>{
drawingArray.push(drawing)
}).then(()=>{
// send drawing data back
res.json(drawingArray)
})
})
})
})
app.post('/uploadDrawing', bodyParser.json(), (req, res)=> {
insertDrawing(req.body).then(()=>{
console.log(`Added ${req.body}`)
})
}
)
app.post('/send', bodyParser.json(), (req, res) => {
console.log(req.body)
})
app.post('/pfp', bodyParser.json(), (req, res) => {
//console.log(req)
if (req.user !== undefined && req.user !== null) { // if user has logged in
req.user.then(user => {
let updatedUser = {
username: user.username,
avatar: req.body.avatar, // Some placeholder image here (maybe github icon?)
flag: user.flag, // Grab flag from IP???
friends: user.friends
}
console.log(updatedUser)
upsertUser(updatedUser)
.then(res.json(updatedUser))
})
}
})
// start listening on PORT
app.listen(PORT, () => {
console.log(`App listening on port: ${PORT}`)
})
server.listen(2000, () =>{
console.log(`server listening on port: 2000`);
})
////////////////////////////////// Database things //////////////////////////////////
let DBclient = null;
async function initConnection() {
const uri = `mongodb+srv://PixelTalk:${process.env.PASSWORD}@cluster0.aaowb.mongodb.net/<dbname>?retryWrites=true&w=majority`
DBclient = new MongoClient(uri, { useUnifiedTopology: true, useNewUrlParser: true })
await DBclient.connect()
}
async function getUser(username) {
if (DBclient === null) {await initConnection()}
let collection = DBclient.db("WebwareFinal").collection("UserData")
return await collection.findOne({username: username})
}
async function getConversation(artist, receiver) {
if (DBclient === null) {await initConnection()}
let collection = DBclient.db("WebwareFinal").collection("Drawings")
return await collection.find({artist: artist, receiver:receiver})
}
async function getInbox(receiver) {
if (DBclient === null) {await initConnection()}
let collection = DBclient.db("WebwareFinal").collection("Drawings")
return await collection.find({receiver:receiver})
}
async function upsertUser(userData) {
if (DBclient === null) {await initConnection()}
let collection = DBclient.db("WebwareFinal").collection("UserData")
collection.updateOne(
{ username: userData.username },
{ $set: userData },
{ upsert: true });
}
async function insertDrawing(drawing) {
if (DBclient === null) {await initConnection()}
let collection = DBclient.db("WebwareFinal").collection("Drawings")
await collection.insertOne(drawing)
}
///////////////////////////////////// Geolocation ///////////////////////////////////////
const token = process.env.IPINFO_TOKEN
const ipinfo = new IPinfo(token);
async function ipToCountryCode(req) {
let clientIP
try {
clientIP = req.headers['x-forwarded-for'].split(',')[0]
} catch (TypeError) {
console.log('no ip')
return null
}
return ipinfo.lookupIp(clientIP)
}
async function ipToFlagPath(req) {
let cc = await ipToCountryCode(req)
if (cc === null) {
return `images/flags/PF.png`
} else {
return `images/flags/${cc._countryCode}.png`
}
}
////////////////////////////////// Communication Socket //////////////////////////////////
const socketServer = new ws.Server({ server })
const clients = [] //has usernames attached to client objects.
const clientObjects = []; //stores client objects for before we have a username
socketServer.on( 'connection', client => {
// add client to client list and send first message
clientObjects.push(client)
client.send(
JSON.stringify({
value:'you have connected'
// we only initiate p2p if this is the second client connected
//initiator:++count % 2 === 0
})
)
// when the server receives a message from this client...
client.on( 'message', msg => {
console.log(msg);
let msgJson = JSON.parse(msg);
if(msgJson.type === "sendingUsername") {
// See if someone already exists with this username. if so, replace their client
// object with the new one.
let userExists = false;
for(let i = 0; i < clients.length; i++) {
if(clients[i].user === msgJson.username) {
clients[i].client = client;
userExists = true;
break;
}
}
if(!userExists) {
clients.push({user:msgJson.username, client:client});
}
} else if (msgJson.type === "notification") {
//notify the client that is the target of the message that they have a new message.
//client might not currently be connected, in that case we don't send to them because bad.
// loop through the list of active clients to try to find them
// if the user isn't found, just don't send and move on with life
for(let i = 0; i < clients.length; i++) {
if(clients[i].user === msgJson.receiver) {
try {
clients[i].client.send(JSON.stringify({sender:msgJson.sender}))
console.log("notification sent!")
} catch {
//the user we tried to send to isn't online, remove them.
console.log("user " + clients[i].user + " is offline");
clients.splice(i, 1);
}
}
}
}
})
})
////////////////////////////////// Graceful Termination //////////////////////////////////
function cleanup() {
console.log("Cleaning up...")
if (DBclient) DBclient.close()
process.exit(0)
}
process.on('SIGTERM', cleanup)
process.on('SIGINT', cleanup)