-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
465 lines (413 loc) · 14.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
/**
* Code for Barbara VM Server
* @author Kit Zellerbach
*/
const express = require('express');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const path = require('path');
const compression = require('compression');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const responseTime = require('response-time');
const helmet = require('helmet');
const dotenv = require('dotenv');
const { TwitterApi } = require('twitter-api-v2');
dotenv.config();
const client = new TwitterApi({
appKey: process.env.CONSUMER_KEY,
appSecret: process.env.CONSUMER_SECRET,
accessToken: process.env.ACCESS_KEY,
accessSecret: process.env.ACCESS_SECRET
});
// Enable read and write permissions
const rwClient = client.readWrite;
// Mongo
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);
// CHANGE from 1.x: need to pass in mongoose instance
var deepPopulate = require('mongoose-deep-populate')(mongoose);
var User = require('./models/user');
var Session = require('./models/session');
const app = express();
var port = process.env.PORT || 3000;
var http = require('http').Server(app);
http.listen(port, function () {
console.log('listening on *:' + port);
});
// Middleware
app.use(express.static('public', {}));
app.use(favicon(path.join(__dirname, 'public', 'icon.png')));
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb'
}));
app.use(bodyParser.json());
// app.use(bodyParser({limit: '50mb'}));
app.use(cookieParser());
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
app.use(responseTime());
app.use(helmet());
app.use(compression());
// Initialize Passport and restore authentication state, if any, from the session.
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'html');
/**
* Login
*/
passport.use('login', new LocalStrategy(
function (username, password, done) {
// check in mongo if a user with username exists or not
User.findOne({ 'username': username },
function (err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user) {
console.log('User Not Found with username ' + username);
return done(null, false, { message: 'No user found' });
}
user.comparePassword(password, (error, isMatch) => {
if (!isMatch) {
console.log("Wrong password")
return done(null, false, { message: 'Invalid username & password.' });
}
return done(null, user);
});
}
);
}
));
/**
* Signup
*/
passport.use('signup', new LocalStrategy(
{
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function (req, username, password, done) {
findOrCreateUser = function () {
// find a user in Mongo with provided username
User.findOne({ 'username': username }, function (err, user) {
// In case of any error, return using the done method
if (err) {
console.log('Error in SignUp: ' + err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists with username: ' + username);
return done(null, false, { message: 'user exists.' });
} else {
// if there is no user with that email create the user
var newUser = new User();
// set the user's local credentials
newUser.username = username;
newUser.password = password;
newUser.email = req.param('email');
newUser.firstName = req.param('firstName');
newUser.lastName = req.param('lastName');
// save the user
newUser.save(function (err) {
if (err) {
console.log('Error in Saving user: ' + err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
// Delay the execution of findOrCreateUser and execute the method in the next tick of the event loop
process.nextTick(findOrCreateUser);
}
));
// only the user ID is serialized and added to the session
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// for every request, the id is used to find the user, which will be restored
// to req.user.
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
/**
* Routes
*/
app.get('/',
function (req, res) {
console.log("/ called");
res.sendFile(path.join(__dirname + '/public/intro.html'));
});
app.get('/about',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/about.html'));
});
app.get('/playground',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/editor.html'));
});
app.get('/editor',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/editor.html'));
});
app.get('/target_lang',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/target_lang.html'));
});
app.get('/tutorial',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/tutorial.html'));
});
app.get('/quilting',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/quilting.html'));
});
app.get('/variants',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/variants.html'));
});
/**
* Login & Sign-up
*/
app.get('/login',
function (req, res) {
res.sendFile(path.join(__dirname + '/public/intro.html'));
});
app.post('/login',
passport.authenticate('login', { failureRedirect: '/' }),
function (req, res) {
res.redirect('back');
});
app.post('/signup',
passport.authenticate('signup', { failureRedirect: '/' }),
function (req, res) {
res.status(200).json({ status: "ok" });
});
app.get('/logout',
function (req, res) {
req.logout(function (err) {
if (err) { return next(err); }
res.redirect('back');
});
});
/**
* Twitter
*/
app.post('/tweet', async function (req, res) {
try {
// Split the base64 image string from the 'img' field in the request body
const base64Data = req.body.img.split(",")[1]; // Get the data part after the comma
// Upload the media to Twitter
const mediaId = await rwClient.v1.uploadMedia(Buffer.from(base64Data, 'base64'), { type: 'png' });
// If media upload is successful, post a tweet with the media
const tweetText = "A new pattern by " + req.body.name + " at https://www.barbara.graphics/playground?id=" + req.body.id;
const tweet = await rwClient.v2.tweet(tweetText, { media: { media_ids: [mediaId] } });
console.log("Successfully tweeted an image!");
// Return tweet id to give user tweet url
res.status(200).json({ id: tweet.data.id });
} catch (error) {
console.error("Failed to post tweet: ", error);
}
});
/**
* Save Session
* TOOD: Refactor this mess
*/
app.post('/savesession',
function (req, res) {
var session = new Session(req.body);
console.log(req.body);
if (req.body.parent) {
Session.findOne({ _id: req.body.parent }, function (err, parentSession) {
session.parent = parentSession;
parentSession.children.push(session);
parentSession.save().then(() => {
session.save().then(() => {
console.log("saved session w/ parent & children");
if (req.body.user) {
User.findOne({ _id: req.body.user }, function (err, user) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
// Save to user
console.log(user);
user.sessions.push(session._id);
user.save(function (err) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
// Seems like sending the session back exceeds the stack size
res.status(200).json(session._id);
}
)
});
} else {
res.status(200).json(session._id);
}
}).catch(error => console.log(error));
}).catch(error => console.log(error));
});
} else {
session.save(function (err) {
if (err) {
res.status(400);
throw err;
}
console.log("saved session w/out parent & children");
if (req.body.user) {
User.findOne({ _id: req.body.user }, function (err, user) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
// Save to user
console.log(user);
user.sessions.push(session._id);
user.save(function (err) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
res.status(200).json(session._id);
}
)
});
} else {
res.status(200).json(session._id);
}
});
}
});
/**
* Delete
*/
app.delete('/session/:id', function (req, res) {
console.log("DELETE");
console.log(req.params.id);
Session.findOneAndDelete({ "_id": req.params.id }, function (err, session) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
// TODO: Delete from children/parent?
// Delete from user
User.findOne({ _id: session.user }, function (err, user) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
user.sessions.pull(session._id);
user.save(function (err) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
res.status(200).json({ status: "ok" });
}
)
});
});
});
/**
* Get Sessions
*/
app.get('/sessions',
function (req, res) {
Session.find({}, { _id: 1, svg: 1, name: 1 }, function (err, data) {
data.map(function (item) {
item.children = undefined;
return item;
});
res.send(data);
// 15 most recent
// TODO: Add pagination (mongoose-paginate?)
}).sort({ _id: -1 }).limit(15);
});
// Session by session id
app.get('/sessions/:id',
function (req, res) {
Session.findOne({ _id: req.params.id }, function (err, data) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
if (!data) {
res.status(200).json({ "status": "no" });
} else {
res.send(data);
}
});
});
// Session by user id
app.get('/sessions/user/:id',
function (req, res) {
User.findOne({ _id: req.params.id })
.populate('sessions') // <==
.exec(function (err, sessions) {
if (err) {
console.log('Error: ' + err);
res.status(400);
throw err;
}
res.status(200).json(sessions);
});
});
// Session children
app.get('/sessions/children/:id',
function (req, res) {
Session.findOne({ _id: req.params.id }, function (err, data) {
res.send(data);
});
});
// TODO: Sessions by parent id
// app.get('/sessions/parents/:id',
// function (req, res) {
// Session.findOne({_id: req.params.id}, function (err, data) {
// // res.send(data);
// data.deepPopulate('parent', function (err, _data) {
// // console.log(_data);
// res.send(_data)
// });
// });
// });
app.get('/loggeduser', function (req, res) {
if (req.user === undefined) {
// The user is not logged in
res.json({});
} else {
res.json({
username: req.user
});
}
});
/**
* Custom middleware
* @param req
* @param res
* @param next
*/
function loggedIn(req, res, next) {
console.log(req.user);
if (req.user) {
next();
} else {
res.redirect('/login');
}
}