-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
176 lines (150 loc) · 5.93 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
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const { connection1, connection2 } = require('./public/chat/js/db.js');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const fileUpload = require('express-fileupload');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const port = 3000;
const secretKey = crypto.randomBytes(64).toString('hex');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileUpload());
app.use(session({
secret: secretKey,
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.post('/messages1', (req, res) => {
const { user, message, privateChatWith } = req.body;
const email = req.session.user.email;
const query = 'INSERT INTO messages (user, id_user, message, privateChatWith, is_read) VALUES (?, ?, ?, ?, FALSE)';
connection1.query(query, [user, email, message, privateChatWith], (err, results) => {
if (err) {
console.error('Erro ao inserir mensagem no banco de dados:', err);
res.status(500).send('Erro ao inserir mensagem');
return;
}
const mensagemInserida = { user, message, privateChatWith };
io.emit('chatMessage', mensagemInserida);
res.status(201).send('Mensagem inserida com sucesso');
});
});
app.get('/messages1', (req, res) => {
const privateChatWith = req.query.privateChatWith;
const emailUsuarioAtual = req.session.user.email;
let query;
let queryParams;
if (privateChatWith) {
query = `SELECT messages.*, users.foto_user AS foto_usuario, users.user_nome AS nome_usuario
FROM messages
LEFT JOIN users ON messages.id_user = users.email_user
WHERE (id_user = ? AND privateChatWith = ?)
OR (id_user = ? AND privateChatWith = ?)
ORDER BY timestamp DESC`;
queryParams = [emailUsuarioAtual, privateChatWith, privateChatWith, emailUsuarioAtual];
} else {
query = `SELECT messages.*, users.foto_user AS foto_usuario, users.user_nome AS nome_usuario
FROM messages
LEFT JOIN users ON messages.id_user = users.email_user
WHERE privateChatWith IS NULL
ORDER BY timestamp DESC`;
queryParams = [];
}
connection1.query(query, queryParams, (err, results) => {
if (err) {
console.error('Erro ao buscar mensagens do banco de dados:', err);
res.status(500).send('Erro ao buscar mensagens');
return;
}
res.json(results);
});
});
app.post('/messages/read', (req, res) => {
const { messageId } = req.body;
const emailUsuarioAtual = req.session.user.email;
const query = 'UPDATE messages SET is_read = TRUE WHERE id = ? AND privateChatWith = ?';
connection1.query(query, [messageId, emailUsuarioAtual], (err) => {
if (err) {
console.error('Erro ao atualizar status de leitura da mensagem:', err);
res.status(500).send('Erro ao atualizar status de leitura');
return;
}
res.status(200).send('Status de leitura atualizado com sucesso');
});
});
app.get('/users', (req, res) => {
const currentUserEmail = req.session.user.email;
const query = 'SELECT * FROM users WHERE email_user != ?';
connection1.query(query, [currentUserEmail], (err, results) => {
if (err) {
console.error('Erro ao buscar usuários:', err);
res.status(500).json({ error: 'Erro ao buscar usuários' });
return;
}
res.json(results);
});
});
app.get('/ultimaMensagem', (req, res) => {
const privateChatWith = req.query.privateChatWith;
const currentUserEmail = req.session.user.email;
let query;
let queryParams;
if (privateChatWith) {
query = `SELECT *
FROM messages
WHERE (id_user = ? AND privateChatWith = ?) OR (id_user = ? AND privateChatWith = ?)
ORDER BY timestamp DESC
LIMIT 1
`;
queryParams = [currentUserEmail, privateChatWith, privateChatWith, currentUserEmail];
} else {
query = `SELECT messages.*, users.user_nome AS nome_usuario
FROM messages
LEFT JOIN users ON messages.id_user = users.email_user
WHERE privateChatWith IS NULL
ORDER BY timestamp DESC
LIMIT 1
`;
queryParams = [];
}
connection1.query(query, queryParams, (err, results) => {
if (err) {
console.error('Erro ao buscar última mensagem do banco de dados:', err);
res.status(500).send('Erro ao buscar última mensagem');
return;
}
res.json(results);
});
});
const uploadsDir = path.join(__dirname, 'public/assets/uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
fs.readdirSync(path.join(__dirname, 'routes')).forEach((file) => {
const route = require(`./routes/${file}`);
app.use('/', route);
});
app.use((req, res) => {
res.status(404).redirect('/not-found/front-end/html/notfound.html');
});
server.listen(port, () => {
console.log(`Servidor Node.js escutando na porta ${port}`);
});
io.on('connection', (socket) => {
console.log('Novo cliente conectado');
socket.on('disconnect', () => {
console.log('Cliente desconectado');
});
});