-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
172 lines (134 loc) · 4.64 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
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
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const crypto = require('crypto');
const routes = require('./routes');
const connection = require('./config/database');
const path = require('path');
const Chat = connection.Chat;
const Message = connection.Message;
const User = connection.User;
const {userJoin, getCurrentUser, userLeave, getRoomUsers} = require('./lib/usersUtils');
const moment = require('moment');
const MongoStore = require('connect-mongo');
require('dotenv').config();
var app = express();
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.json());
app.use(express.urlencoded({extended:true}));
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.set('view engine', 'ejs');
app.use(session({
secret:process.env.SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.DB_STRING,
collectionName: 'sessions'
}),
cookie:{
maxAge: 1000 * 60 * 60 * 24
}
}))
// PASSPORT AUTHENTICATION
require('./config/passport');
app.use(passport.initialize());
app.use(passport.session());
// ROUTES
// Imports all of the routes from ./routes/index.js
app.use(routes);
people = {};
io.on('connection',function(socket){
socket.on('joinRoom', (utilizador) => {
const user = userJoin(socket.id, utilizador.nome, utilizador.sala);
console.log(user);
socket.join(user.room)
// Broadcast when a user connects
let message = {id: user.username, msg: ' has joined the room'};
socket.broadcast.to(user.room).emit('update', message);
var showUsers = getRoomUsers(user.room)
// Send users and room info
io.to(user.room).emit('room users', showUsers)
});
socket.on('get messages', async (chatid)=>{
console.log("chatid is: " + chatid)
try{
var result = await Chat.findById(chatid,{_id:0, messages:1})
.populate({path: 'messages', model:'Message', populate:{path:'user', model:'User'}})
.exec()
console.log(result)
socket.emit('receive messages', result)
}catch(e){
console.log(e)
}
})
// Listen for chatMessage
socket.on('chat message', async msg =>{
const user = getCurrentUser(socket.id);
console.log('message: '+ JSON.stringify(msg));
try{
var mensagem = {content: msg.content, user: msg.username};
const message = new Message({
text: mensagem.content,
date: new Date().toLocaleString(),
user: msg.userid
})
//Awaits to insert message into db
var result = await message.save()
await Chat.findOneAndUpdate({"_id": msg.chatid},{$push : {messages: result._id}})
console.log(result)
const messg = {text: result.text, date: result.date, user: msg.username}
io.to(user.room).emit('message', messg);
}
catch (e) {
console.log(e)
}
});
//Find if user is admin of the current chat
socket.on('check admin', async data =>{
try{
var user = await User.findById(data.user, {admin:1})
console.log(user)
user.admin.forEach(elem=>{
if(elem == data.chatid){
console.log("user is admin of "+ data.chatid)
socket.emit('confirm admin', user)
}
else {
console.log("user is not admin")
}
})
}
catch(e){
return e
}
})
//Find all users except given current
socket.on('request users', async userid =>{
try{
var result = await User.find({'_id': { $ne: userid}}, {username:1}) //$ne -> not equal
console.log(result)
socket.emit('show users', result)
}
catch(err){
return err
}
})
// Runs when client disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if(user) {
let message = {id: user.username, msg: ' has left the room'};
io.to(user.room).emit('update', message );
// Send users and room info
let showUsers = getRoomUsers(user.room);
io.to(user.room).emit('room users', showUsers);
}
});
});
// SERVER
http.listen(3000, () =>{
console.log('Server running in port 3000');
});