-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
497 lines (461 loc) · 11.4 KB
/
main.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
var express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
flash = require('connect-flash'),
socketio = require('socket.io'),
compilex = require('compilex'),
passport = require('passport'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose'),
User = require('./models/User'),
Post = require('./models/Post'),
Notif = require('./models/Notif'),
socketio = require('socket.io');
var students = [];
var app = express();
var rooms = [];
var expressServer = app.listen(3000, () => {
console.log('Server is running');
});
var io = socketio(expressServer);
// mongoose.connect("mongodb://localhost/whiteboard",{ useNewUrlParser: true });
mongoose.connect('mongodb://localhost/whiteboard', {
useUnifiedTopology: true,
useNewUrlParser: true
});
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(flash());
app.use(
require('express-session')({
secret: 'hackathon is on!!',
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.error1 = req.flash('error1');
res.locals.error2 = req.flash('error2');
res.locals.error3 = req.flash('error3');
res.locals.out = req.flash('out');
next();
});
// Landing page
app.get('/', (req, res) => {
res.render('landing');
});
//To put middleware
app.get('/dashboard', isLoggedIn, (req, res) => {
res.render('home');
});
//Routes Credentials
// or login in users
app.get('/login', (req, res) => {
res.render('login');
});
app.get('/room/student', isLoggedIn, (req, res) => {
res.render('student-room', {
user: req.user
});
});
app.get('/room/:roomId', isLoggedIn, function(req, res) {
// console.log(req.params.roomId);
res.render('student-room', {
user: req.user,
roomId: req.params.roomId
});
});
app.post(
'/login',
passport.authenticate('local', {
successRedirect: '/dashboard',
failureRedirect: '/login'
}),
function(req, res) {}
);
// For registering new user
app.post('/register', (req, res) => {
if (req.body.designation) {
if (req.body.password === req.body.confirmpassword) {
var newUser = new User({
username: req.body.username,
email: req.body.email,
designation: req.body.designation
});
User.register(newUser, req.body.password, async function(err, user) {
if (err) {
console.log(err);
req.flash('error1', 'Username already exists');
res.redirect('/login');
} else {
await User.authenticate('local')(req, res, function() {
res.redirect('/dashboard');
});
}
});
} else {
// Confirm password is wrong
req.flash('error2', 'Passwords do not match');
res.redirect('/login');
}
} else {
// Please select designation.
req.flash('error3', 'Select Designation');
res.redirect('/login');
}
});
// Save your code
app.post('/post/code', (req, res) => {
if (req.body.designation == 'Student') {
// console.log(req.body.email);
const post = new Post({
username: req.body.username,
email: req.body.email,
question: req.body.question,
code: req.body.code,
input: req.body.input,
output: req.body.output
});
post.save(function(err) {
if (!err) {
res.send({
status: true
});
} else {
res.send({
status: false
});
}
});
} else {
console.log('Teachers cant post');
res.send({
status: false
});
}
});
//Notification code
app.post('/notif', (req, res) => {
//if(req.body.designation == 'Student') {
// console.log(req.body.email);
const notif = new Notif({
tousername: req.body.tousername,
fromusername: req.body.fromusername,
code: req.body.code,
comment: req.body.comment
});
notif.save(function(err) {
if (!err) {
res.send({
status: true
});
} else {
res.send({
status: false
});
}
});
});
//notif get
app.get('/notif/:tousername', async (req, res) => {
try {
const notifs = await Notif.find({ tousername: req.params.tousername });
res.status(200).json({ notifs: notifs });
} catch (err) {
res.status(400).json({ error: err });
}
});
// Download code
app.post('/download', async (req, res) => {
try {
res.setHeader(`Content-disposition', 'attachment; filename=${req.body.name}.c`);
res.setHeader('Content-type', 'text/plain');
res.charset = 'UTF-8';
res.write(req.body.data);
res.end();
} catch (err) {
res.status(400).json({ error: err });
}
});
// Retrieve all posts
app.get('/post/all', async (req, res) => {
try {
const posts = await Post.find().sort({ date: -1 });
res.status(200).json({ posts: posts });
} catch (err) {
res.status(400).json({ error: err });
}
});
// Today's posts
app.get('/post/count/today', async (req, res) => {
try {
var start = new Date();
start.setHours(0, 0, 0, 0);
var end = new Date();
end.setHours(23, 59, 59, 999);
const todayPosts = await Post.find({ date: { $gte: start, $lt: end } }).count();
res.status(200).json({ today: todayPosts });
} catch (err) {
res.status(400).json({ error: err });
}
});
// User's posts
app.get('/post/:username', async (req, res) => {
try {
const userPosts = await Post.find({ username: req.params.username }).sort({ date: -1 });
res.status(200).json({ userPosts: userPosts });
} catch (err) {
res.status(400).json({ error: err });
}
});
app.get('/post/count/:username', async (req, res) => {
try {
const userPosts = await Post.find({ username: req.params.username }).count();
res.status(200).json({ userPosts: userPosts });
} catch (err) {
res.status(400).json({ error: err });
}
});
// Get email from username
app.get('/email/:username', async (req, res) => {
try {
const user = await User.find({ username: req.params.username });
res.status(200).json({ user: user });
} catch (err) {
res.status(400).json({ error: err });
}
});
// LOGOUT
app.get('/logout', (req, res) => {
req.logout();
req.flash('out', 'Successfully logged you out');
res.redirect('/login');
});
//Checking for Authenication
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
//req.flash("error", "Please Login First!");
return res.redirect('/login');
}
}
// Route for ROOMS
app.get('/room', isLoggedIn, (req, res) => {
res.render('room', {
user: req.user
});
});
// SETTING UP SOCKETIO
// io.on('connect',(socket) => {
// socket.on('create-room',function(msg){
// console.log(msg)
// socket.join(msg.roomId);
// // socket.to(msg.roomId).emit('send-room-data',{'username':msg.})
// var room = {
// "roomName" : msg.roomName,
// "teacher" : msg.teacher,
// "roomId" : msg.roomId,
// };
// var firstRoomSection = {
// "roomId" : msg.roomId,
// "students" : []
// }
// rooms.push(room);
// socket.emit('success-creating-room',{
// "result" : true,
// "roomId" : msg.roomId
// });
// // socket.broadcast.emit('room-created', {
// // 'username':msg.username,
// // 'roomId':msg.roomId,
// // 'roomName':msg.roomName
// // });
// });
// // Send the room lIst to data
// setInterval(function(){
// socket.emit('get-rooms',{
// "rooms" : rooms
// });
// },2000);
var adminID = '';
var teacherArray = [];
var userData = [];
var user = '';
var activeuserSocketId = '';
io.on('connect', function(socket) {
// console.log("Welcome new user");
socket.on('ok', function() {
//
});
socket.on('connect-to-room', function(data) {
socket.join(data.roomId);
var count = 0;
if (data.designation === 'Teacher') {
// adminID = socket.id;
adminID = socket.id;
teacherArray.forEach(function(item) {
if (item.username == data.username) {
// item.code = data.code;
} else {
count + -1;
}
});
if (count == teacherArray.length) {
var temp = {
id: socket.id,
username: data.username
};
teacherArray.push(temp);
}
}
});
socket.on('get-students', function(msg) {
// console.log(msg);
// console.log(students);
// console.log(rooms);
studentInRoom = [];
students.forEach(function(item) {
// console.log(item)
if (item.roomId == msg['roomId']) {
studentInRoom = item.students;
}
});
socket.emit('send-student', {
studentsList: studentInRoom
});
// console.log(studentInRoom);
});
socket.on('transfer-teacher-data', function(data) {
// if (user == data.username) {
// console.log(activeuserSocketId);
// console.log(data.code);
socket.broadcast.to(activeuserSocketId).emit('code-on-user', data.code);
// }
});
socket.on('get-user-data', function(data) {
console.log(data);
user = data.username;
// activeuserSocketId = data.socketId;
// console.log(user,"Active user is here bro ",activeuserSocketId);
userData.forEach(function(item) {
if (data.username == item.username) {
socket.emit('sending-code-data', item.code);
}
});
});
socket.on('userdata', function(data) {
// console.log(user,data.username);
if (user == data.username) {
activeuserSocketId = socket.id;
console.log('Here is data of socket which is sending ', socket.id);
console.log(adminID);
// activeuserSocketId = socket.id;
socket.broadcast.to(adminID).emit('code', data.code);
}
var count = 0;
userData.forEach(function(item) {
if (item.username == data.username) {
item.code = data.code;
} else {
count + -1;
}
});
if (count == userData.length) {
var temp = {
code: data.code,
username: data.username
};
userData.push(temp);
}
// console.log("Dais here boiss");
// console.log(userData);
});
});
// });
app.post('/create-room', function(req, res) {
var room = {
roomName: req.body.roomName,
teacher: req.body.teacher,
roomId: req.body.roomId
};
rooms.push(room);
var firstRoomSection = {
roomId: req.body.roomId,
students: []
};
students.push(firstRoomSection);
res.send(true);
});
app.get('/get-room', function(req, res) {
res.send({
rooms: rooms
});
});
app.post('/enter-room', function(req, res) {
// console.log(req.body);
students.forEach(function(student) {
if (student.roomId == req.body.roomId) {
student.students.push(req.body.student);
}
});
// console.log(students);
res.send(true);
});
// app.get('/newpage:id',function(req, res){
// res.send('Teacher has enter room page ',id)
// });
// Compilex
var compiler = require('compilex');
var option = { stats: true };
compiler.init(option);
app.post('/compile/c', function(req, res) {
// console.log("entered here");
var code = req.body.code;
var input = req.body.input;
// console.log(code);
// console.log(input);
compiler.flush(function() {
console.log('All previous temporary files flushed !');
});
if (input === '') {
var envData = { OS: 'linux', cmd: 'gcc', options: { timeout: 1000 } };
compiler.compileCPP(envData, code, function(data) {
if (data.error) {
res.send({
output: data.error,
status: false
});
} else {
console.log(data.output);
res.send({
output: data.output,
status: true
});
}
});
} else {
var envData = { OS: 'linux', cmd: 'gcc', options: { timeout: 1000 } };
compiler.compileCPPWithInput(envData, code, input, function(data) {
if (data.error) {
res.send({
output: data.error,
status: true
});
} else {
console.log(data.output);
res.send({
output: data.output,
status: true
});
}
});
}
});