-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
564 lines (469 loc) · 19.8 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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
require('dotenv').config();
const express = require('express');
const mysql = require('mysql2');
const cors = require('cors');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const http = require('http');
const { Server } = require('socket.io');
const crypto = require('crypto');
// Init Express
const app = express();
const PORT = 5000;
const SECRET_KEY = process.env.SECRET_KEY;
const SERVER_DB_PW = process.env.SERVER_DB_PW;
app.use(cors());
app.use(bodyParser.json());
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: SERVER_DB_PW,
database: 'momotalk_v2',
});
db.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL');
});
// Socket.io Instance
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
},
});
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('join_room', (userId) => {
if (userId) {
socket.join(userId.toString());
console.log(`User ${userId} joined room`);
}
});
socket.on('leave_room', (userId) => {
if (userId) {
socket.leave(userId.toString());
console.log(`User ${userId} left room`);
}
});
socket.on('send_message', (data) => {
const { senderId, receiverId, text } = data;
if (!senderId || !receiverId || !text) {
console.error('Invalid message payload:', data);
return;
}
const query = 'INSERT INTO dms (sender_id, receiver_id, text, timestamp) VALUES (?, ?, ?, NOW())';
db.query(query, [senderId, receiverId, text], (err) => {
if (!err) {
io.to(receiverId.toString()).emit('receive_message', {
senderId,
receiverId,
text,
timestamp: new Date(),
});
} else {
console.error('Error saving message:', err.message);
}
});
});
// Listen to Friend Request
socket.on('send_friend_request', ({ senderId, receiverId, senderUsername }) => {
console.log(`Friend request sent from ${senderId} to ${receiverId}`);
io.to(receiverId.toString()).emit('receive_friend_request', {
senderId,
senderUsername,
});
});
// Listen for response of the friend requests
socket.on('respond_friend_request', ({ senderId, receiverId, action }) => {
console.log(`Friend request ${action} by ${receiverId}`);
io.to(senderId.toString()).emit('friend_request_responded', { receiverId, action });
if (action === 'accept') {
io.to(senderId.toString()).emit('update_friend_list');
}
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
// ROUTERS
app.post('/register', async (req, res) => {
const { username, email, password, nickname } = req.body;
if (!username || !email || !password || !nickname) {
return res.status(400).json({ error: 'Username, email, password, and nickname are required.' });
}
try {
const hashedPassword = await bcrypt.hash(password, 10);
const query = 'INSERT INTO users (username, email, password, nickname) VALUES (?, ?, ?, ?)';
db.query(query, [username, email, hashedPassword, nickname], (err) => {
if (err) {
if (err.code === 'ER_DUP_ENTRY') {
return res.status(400).json({ error: 'Username or email already exists.' });
}
return res.status(500).json({ error: 'Database error.' });
}
res.json({ message: 'User registered successfully.' });
});
} catch (error) {
res.status(500).json({ error: 'Internal server error.' });
}
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = 'SELECT * FROM users WHERE username = ?';
db.query(query, [username], async (err, results) => {
if (err) return res.status(500).json({ error: err.message });
if (results.length === 0) return res.status(401).json({ error: 'Invalid credentials' });
const user = results[0];
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
return res.status(401).json({ error: 'Invalid credentials' });
}
// Generate new session_token
const sessionToken = crypto.randomBytes(32).toString('hex');
const updateQuery = 'UPDATE users SET session_token = ? WHERE id = ?';
db.query(updateQuery, [sessionToken, user.id], (err) => {
if (err) return res.status(500).json({ error: 'Failed to update session token' });
const token = jwt.sign({ userId: user.id, username: user.username, sessionToken }, SECRET_KEY, { expiresIn: '3h' });
res.json({
token,
userId: user.id,
username: user.username,
nickname: user.nickname,
avatar: user.avatar || null,
email: user.email,
});
});
});
});
function authenticateToken(req, res, next) {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token provided' });
jwt.verify(token, SECRET_KEY, (err, decoded) => {
if (err) {
console.error('JWT Verification Error:', err.message);
return res.status(401).json({ error: 'Invalid or expired token' });
}
const { userId, sessionToken } = decoded;
// Auth if session_token matches to prevent same user login multiple times
const query = 'SELECT session_token FROM users WHERE id = ?';
db.query(query, [userId], (err, results) => {
if (err) return res.status(500).json({ error: 'Database error' });
if (results.length === 0 || results[0].session_token !== sessionToken) {
return res.status(401).json({ error: 'Invalid session token' });
}
req.user = { userId };
next();
});
});
}
// POST
app.post('/messages', authenticateToken, (req, res) => {
const { text } = req.body;
const query = 'INSERT INTO messages (user_id, text, timestamp) VALUES (?, ?, NOW())';
db.query(query, [req.user.userId, text], (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'Message sent' });
});
});
app.get('/messages', authenticateToken, (req, res) => {
const { page = 1, limit = 10 } = req.query;
const offset = (page - 1) * limit;
const query = `
SELECT messages.id, messages.text, messages.timestamp, users.nickname AS username
FROM messages
JOIN users ON messages.user_id = users.id
ORDER BY messages.timestamp ASC
LIMIT ? OFFSET ?
`;
db.query(query, [parseInt(limit), parseInt(offset)], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json(results);
});
});
app.get('/friends', authenticateToken, (req, res) => {
const userId = req.user.userId;
const friendsQuery = `
SELECT u.id, u.username, u.nickname, u.avatar,
(SELECT d.text
FROM dms d
WHERE (d.sender_id = u.id AND d.receiver_id = ?)
OR (d.sender_id = ? AND d.receiver_id = u.id)
ORDER BY d.timestamp DESC LIMIT 1) AS lastMessage,
(SELECT d.timestamp
FROM dms d
WHERE (d.sender_id = u.id AND d.receiver_id = ?)
OR (d.sender_id = ? AND d.receiver_id = u.id)
ORDER BY d.timestamp DESC LIMIT 1) AS lastMessageTime
FROM users u
JOIN friends f ON
((f.user_id = ? AND f.friend_id = u.id) OR (f.friend_id = ? AND f.user_id = u.id))
AND f.status = 'accepted';
`;
db.query(friendsQuery, [userId, userId, userId, userId, userId, userId], (err, results) => {
if (err) {
// console.error('Error executing friends query:', err.message); // For DEBUG
return res.status(500).json({ error: 'Failed to fetch friends' });
}
res.json(results);
});
});
app.post('/friend/add', authenticateToken, (req, res) => {
const { friendUsername } = req.body;
const userId = req.user.userId;
if (!friendUsername) {
return res.status(400).json({ error: 'Friend username is required.' });
}
// No add myself
const getUserQuery = 'SELECT id FROM users WHERE username = ?';
db.query(getUserQuery, [friendUsername], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
if (results.length === 0) return res.status(404).json({ error: 'User not found.' });
const friendId = results[0].id;
if (friendId === userId) {
return res.status(400).json({ error: 'You cannot add yourself as a friend.' });
}
// See if already friend
const checkFriendshipQuery = `
SELECT * FROM friends
WHERE (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)
`;
db.query(checkFriendshipQuery, [userId, friendId, friendId, userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
if (results.length > 0) {
return res.status(400).json({ error: 'Friendship already exists or pending.' });
}
// Friend request
const addFriendQuery = 'INSERT INTO friends (user_id, friend_id, status) VALUES (?, ?, "pending")';
db.query(addFriendQuery, [userId, friendId], (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'Friend request sent', receiverId: friendId });
});
});
});
});
app.post('/friend/accept', authenticateToken, (req, res) => {
const { friendId } = req.body;
const userId = req.user.userId;
const checkRequestQuery = 'SELECT * FROM friends WHERE user_id = ? AND friend_id = ? AND status = "pending"';
db.query(checkRequestQuery, [friendId, userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
if (results.length === 0) return res.status(404).json({ error: 'No pending friend request found' });
const acceptFriendQuery = 'UPDATE friends SET status = "accepted" WHERE user_id = ? AND friend_id = ?';
db.query(acceptFriendQuery, [friendId, userId], (err) => {
if (err) return res.status(500).json({ error: err.message });
const reverseFriendQuery = `
INSERT INTO friends (user_id, friend_id, status)
VALUES (?, ?, "accepted")
ON DUPLICATE KEY UPDATE status = "accepted"
`;
db.query(reverseFriendQuery, [userId, friendId], (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'Friend request accepted and relationship updated' });
});
});
});
});
app.get('/friend/requests', authenticateToken, (req, res) => {
const userId = req.user.userId;
const query = `
SELECT f.id, u.username, u.nickname
FROM friends f
JOIN users u ON f.user_id = u.id
WHERE f.friend_id = ? AND f.status = 'pending'
`;
db.query(query, [userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json(results);
});
});
app.post('/user/update', authenticateToken, async (req, res) => {
const { nickname, avatar, oldPassword, newPassword } = req.body;
const userId = req.user.userId;
// Validate request
if (!nickname && !avatar && (!oldPassword || !newPassword)) {
return res.status(400).json({ error: 'No updates provided.' });
}
try {
// Handle password update
if (oldPassword && newPassword) {
const query = "SELECT password FROM users WHERE id = ?";
const [rows] = await db.promise().query(query, [userId]);
if (rows.length === 0) {
return res.status(404).json({ error: 'User not found.' });
}
const user = rows[0];
const isMatch = await bcrypt.compare(oldPassword, user.password);
if (!isMatch) {
return res.status(400).json({ error: 'Current password is incorrect.' });
}
const hashedPassword = await bcrypt.hash(newPassword, 10);
const updatePasswordQuery = "UPDATE users SET password = ? WHERE id = ?";
await db.promise().query(updatePasswordQuery, [hashedPassword, userId]);
}
// Handle nickname and avatar updates
const updates = [];
const params = [];
if (nickname) {
updates.push('nickname = ?');
params.push(nickname);
}
if (avatar) {
updates.push('avatar = ?');
params.push(avatar);
}
if (updates.length > 0) {
params.push(userId);
const updateQuery = `UPDATE users SET ${updates.join(', ')} WHERE id = ?`;
await db.promise().query(updateQuery, params);
}
res.json({ message: 'Profile updated successfully.' });
} catch (error) {
console.error('Error updating user profile:', error.message);
res.status(500).json({ error: 'Failed to update profile.' });
}
});
app.post('/friend/respond', authenticateToken, (req, res) => {
const { requestId, action } = req.body; // action: "accept" or "reject"
const userId = req.user.userId;
if (action === 'accept') {
const acceptQuery = `
UPDATE friends
SET status = 'accepted'
WHERE id = ? AND friend_id = ?
`;
db.query(acceptQuery, [requestId, userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'Friend request accepted', senderId: requestId });
});
} else if (action === 'reject') {
const rejectQuery = `
DELETE FROM friends
WHERE id = ? AND friend_id = ?
`;
db.query(rejectQuery, [requestId, userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'Friend request rejected', senderId: requestId });
});
}
});
app.post('/friend/remove', authenticateToken, (req, res) => {
const { friendId } = req.body;
const userId = req.user.userId;
const deleteFriendQuery = `
DELETE FROM friends
WHERE (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)
`;
const deleteDMQuery = `
DELETE FROM dms
WHERE (sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)
`;
// Friend Deletion Sequence
db.beginTransaction((transactionErr) => {
if (transactionErr) {
console.error('Transaction start error:', transactionErr.message);
return res.status(500).json({ error: 'Internal server error.' });
}
//Delete friend
db.query(deleteFriendQuery, [userId, friendId, friendId, userId], (friendErr) => {
if (friendErr) {
return db.rollback(() => {
console.error('Error deleting friend:', friendErr.message);
return res.status(500).json({ error: 'Failed to remove friend.' });
});
}
//Delete Chat
db.query(deleteDMQuery, [userId, friendId, friendId, userId], (dmErr) => {
if (dmErr) {
return db.rollback(() => {
console.error('Error deleting DMs:', dmErr.message);
return res.status(500).json({ error: 'Failed to remove DMs.' });
});
}
db.commit((commitErr) => {
if (commitErr) {
return db.rollback(() => {
console.error('Transaction commit error:', commitErr.message);
return res.status(500).json({ error: 'Failed to complete transaction.' });
});
}
res.json({ message: 'Friend and DMs removed successfully.' });
});
});
});
});
});
app.post('/dm/send', authenticateToken, (req, res) => {
const { receiverId, text } = req.body;
const senderId = req.user.userId;
const sendDMQuery = 'INSERT INTO dms (sender_id, receiver_id, text, timestamp) VALUES (?, ?, ?, NOW())';
db.query(sendDMQuery, [senderId, receiverId, text], (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'DM sent' });
});
});
app.get('/dm/:friendId', authenticateToken, (req, res) => {
const friendId = req.params.friendId;
const userId = req.user.userId;
const getDMQuery = `
SELECT dms.id, dms.text, dms.timestamp,
dms.sender_id AS senderId, dms.receiver_id AS receiverId,
(dms.sender_id = ?) AS self -- Calculate for self directly so front-end don't have to do it
FROM dms
WHERE (dms.sender_id = ? AND dms.receiver_id = ?)
OR (dms.sender_id = ? AND dms.receiver_id = ?)
ORDER BY dms.timestamp ASC
`;
db.query(getDMQuery, [userId, userId, friendId, friendId, userId], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json(results);
});
});
app.get('/protected-resource', authenticateToken, (req, res) => {
if (!req.user || req.user.role !== 'admin') { //This role for future use! Currently, no role for users yet!
return res.status(403).json({ error: 'Access forbidden: insufficient permissions' });
}
res.json({ message: 'Welcome, admin!' });
});
app.get('/users/search', authenticateToken, (req, res) => {
const { query } = req.query;
if (!query) return res.status(400).json({ error: 'Query parameter is required.' });
const searchQuery = 'SELECT id, username, nickname FROM users WHERE username LIKE ?';
db.query(searchQuery, [`%${query}%`], (err, results) => {
if (err) return res.status(500).json({ error: err.message });
res.json(results);
});
});
app.post('/dm/delete', authenticateToken, (req, res) => {
const { friendId } = req.body;
const userId = req.user.userId;
const deleteDMQuery = 'DELETE FROM dms WHERE (sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)';
db.query(deleteDMQuery, [userId, friendId, friendId, userId], (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ message: 'DM history deleted' });
});
});
//Clean Friend Request!
const cleanExpiredRequests = () => {
const deleteQuery = `
DELETE FROM friends
WHERE status = 'pending' AND TIMESTAMPDIFF(DAY, created_at, NOW()) > 30
`;
db.query(deleteQuery, (err, results) => {
if (err) {
console.error('Error cleaning expired friend requests:', err.message);
} else {
console.log(`Cleaned ${results.affectedRows} expired friend requests`);
}
});
};
setInterval(cleanExpiredRequests, 24 * 60 * 60 * 1000);
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log('Initializing cleanup for expired friend requests...');
cleanExpiredRequests();
});
setInterval(cleanExpiredRequests, 24 * 60 * 60 * 1000);