-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
131 lines (107 loc) · 3.28 KB
/
app.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
//Importing npm packages
const express = require("express");
const http = require("http");
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
const bodyParser = require("body-parser");
const methodOverride = require('method-override');
const passport = require("passport");
const keys = require("./config/keys");
const socketio = require("socket.io");
const formatMessage = require('./utils/messages');
const moment = require("moment");
//Importing MongoDB models
require("./db/mongoose");
const User = require("./models/User");
const webScraper = require("./db/webScraper");
//Importing Routes
const user = require("./routes/user");
const auth = require("./routes/auth");
const friend = require("./routes/friend");
//Variables
const port = process.env.PORT||3000;
const app = express();
const server=http.createServer(app);
const io = socketio(server);
app.use(express.json());
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use(express.static("./assets"));
app.use(
cookieSession({
name: "session",
keys: [keys.sessionSecret]
})
);
app.use(cookieParser());
//Passport Middleware
require("./middleware/PassportMiddleware");
app.use(passport.initialize());
app.use(passport.session());
app.use("/user",user);
app.use("/auth",auth);
app.use("/friend",friend);
app.get("/",(req,res)=>{
webScraper();
// console.log(req.user);
if(req.session.token == null){
res.render("home",{
currentUser:req.user
});
}else{
User.findOne({email:req.user.email},(err,user)=>{
if(err) Error(err);
if(user){
res.redirect("/user/dashboard");
}
});
}
});
io.on('connection',(socket)=>{
console.log("New Web Socket Connection");
// Runs when user joins room
socket.on('joinRoom',({user_id})=>{
socket.join(user_id);
console.log("Room Joined");
});
// Listen for chat message
socket.on('chatMessage',({msg,friend_id,user_id})=>{
User.findOne({_id:user_id},async(err,user)=>{
User.findOne({_id:friend_id},async (err,friend)=>{
user.friends.forEach(async (x,i)=>{
if(x.friend_id==friend_id){
user.friends[i].chats.push({
//sender
user_id,
//receiver
friend_id,
msg,
time:moment().format('h:mm a')
});
await user.save();
}
});
friend.friends.forEach(async(y,i)=>{
if(y.friend_id==user_id){
friend.friends[i].chats.push({
user_id,
friend_id,
msg,
time:moment().format('h:mm a')
});
await friend.save();
}
});
io.to(user_id).emit('message',formatMessage(user_id,msg,friend_id));
io.to(friend_id).emit('message',formatMessage(user_id,msg,user_id));
});
});
});
// socket.on('disconnect',()=>{
// io.emit('message',formatMessage('Chat BOT',"User Disconnected"))
// });
})
server.listen(port,()=>{
console.log("Server started on "+ port + "!");
});