-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
437 lines (379 loc) · 15.3 KB
/
server.js
File metadata and controls
437 lines (379 loc) · 15.3 KB
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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const multer = require('multer');
const fs = require('fs');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Create uploads directory if it doesn't exist
const uploadsDir = path.join(__dirname, 'public', 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadsDir);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: 10 * 1024 * 1024 // 10MB limit
},
fileFilter: (req, file, cb) => {
// Allow images, documents, and some common file types
const allowedTypes = /jpeg|jpg|png|gif|pdf|doc|docx|txt|zip|mp4|mp3/;
const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = allowedTypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb(new Error('Invalid file type'));
}
}
});
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'public', 'uploads')));
app.use(express.json());
const users = new Map();
const messageHistory = [];
const rooms = new Map();
const activeUsernames = new Set();
const roomPasswords = new Map();
const userProfiles = new Map(); // Store user profiles
// Initialize general room
rooms.set('general', {
name: 'general',
owner: 'system',
hasPassword: false,
users: new Set()
});
// File upload endpoint
app.post('/upload', upload.single('file'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const fileInfo = {
filename: req.file.filename,
originalname: req.file.originalname,
mimetype: req.file.mimetype,
size: req.file.size,
url: `/uploads/${req.file.filename}`
};
res.json({ success: true, file: fileInfo });
} catch (error) {
res.status(500).json({ error: 'Upload failed: ' + error.message });
}
});
// User profile endpoints
app.post('/profile/:username', (req, res) => {
try {
const { username } = req.params;
const { status, bio, avatar } = req.body;
if (!activeUsernames.has(username.toLowerCase())) {
return res.status(404).json({ error: 'User not found' });
}
userProfiles.set(username.toLowerCase(), {
username,
status: status || 'online',
bio: bio || '',
avatar: avatar || '',
lastSeen: new Date(),
joinedAt: userProfiles.get(username.toLowerCase())?.joinedAt || new Date()
});
// Find the user's current room and broadcast to that room
const userSocket = Array.from(users.entries()).find(([id, user]) => user.username === username);
if (userSocket) {
const userRoom = userSocket[1].room;
// Broadcast updated user list to the room
io.to(userRoom).emit('userList', getUsersInRoom(userRoom));
// Also emit profile update event
io.to(userRoom).emit('profileUpdated', {
username,
profile: userProfiles.get(username.toLowerCase())
});
}
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Profile update failed: ' + error.message });
}
});
app.get('/profile/:username', (req, res) => {
try {
const { username } = req.params;
const profile = userProfiles.get(username.toLowerCase());
if (!profile) {
return res.status(404).json({ error: 'Profile not found' });
}
res.json(profile);
} catch (error) {
res.status(500).json({ error: 'Failed to get profile: ' + error.message });
}
});
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('checkUsername', (data) => {
if (data && data.username) {
const username = data.username.toString().trim().substring(0, 20);
const available = !activeUsernames.has(username.toLowerCase());
socket.emit('usernameCheck', { username, available });
}
});
socket.on('join', (data) => {
if (data && data.username) {
const username = data.username.toString().trim().substring(0, 20);
const room = (data.room || 'general').toString().trim().substring(0, 20);
if (username && room && !activeUsernames.has(username.toLowerCase())) {
activeUsernames.add(username.toLowerCase());
users.set(socket.id, { username, room });
socket.join(room);
// Create user profile if it doesn't exist
if (!userProfiles.has(username.toLowerCase())) {
userProfiles.set(username.toLowerCase(), {
username,
status: 'online',
bio: '',
avatar: '',
lastSeen: new Date(),
joinedAt: new Date()
});
} else {
// Update status to online
const profile = userProfiles.get(username.toLowerCase());
profile.status = 'online';
profile.lastSeen = new Date();
}
// Add user to room
if (rooms.has(room)) {
rooms.get(room).users.add(username);
}
socket.to(room).emit('userJoined', username);
io.to(room).emit('userList', getUsersInRoom(room));
socket.emit('roomList', getRoomList());
// Send user their profile
socket.emit('profileData', userProfiles.get(username.toLowerCase()));
}
}
});
socket.on('message', (data) => {
const user = users.get(socket.id);
if (user && data && (data.message || data.file)) {
const cleanMessage = data.message ? data.message.toString().trim().substring(0, 500) : '';
const messageData = {
username: user.username,
message: cleanMessage,
timestamp: new Date().toLocaleTimeString(),
room: user.room,
id: Date.now() + Math.random(),
replyTo: data.replyTo || null,
reactions: {}
};
// Include file data if present
if (data.file) {
messageData.file = {
url: data.file.url,
name: data.file.name,
type: data.file.type,
size: data.file.size
};
}
messageHistory.push(messageData);
if (messageHistory.length > 1000) messageHistory.shift();
io.to(user.room).emit('message', messageData);
}
});
socket.on('privateMessage', (data) => {
const sender = users.get(socket.id);
const recipient = Array.from(users.entries()).find(([id, user]) => user.username === data.to);
if (sender && recipient && data && data.message) {
const cleanMessage = data.message.toString().trim().substring(0, 500);
if (cleanMessage) {
const messageData = {
username: sender.username,
message: cleanMessage,
timestamp: new Date().toLocaleTimeString(),
private: true,
to: data.to,
replyTo: data.replyTo || null
};
socket.emit('privateMessage', messageData);
io.to(recipient[0]).emit('privateMessage', messageData);
}
}
});
socket.on('typing', (data) => {
const user = users.get(socket.id);
if (user) {
socket.to(user.room).emit('typing', { username: user.username, isTyping: data.isTyping });
}
});
socket.on('switchRoom', (data) => {
const user = users.get(socket.id);
if (user && data && data.room) {
const newRoom = data.room.toString().trim();
const password = data.password;
// Check if room exists
if (!rooms.has(newRoom)) {
socket.emit('roomError', { message: 'Room does not exist', type: 'not_found' });
return;
}
const roomData = rooms.get(newRoom);
// Check password for protected rooms
if (roomData.hasPassword && newRoom !== 'general') {
const correctPassword = roomPasswords.get(newRoom);
if (!password || password !== correctPassword) {
socket.emit('roomError', {
message: 'Wrong password',
type: 'wrong_password',
room: newRoom
});
return;
}
}
// Leave current room
socket.leave(user.room);
if (rooms.has(user.room)) {
rooms.get(user.room).users.delete(user.username);
}
socket.to(user.room).emit('userLeft', user.username);
io.to(user.room).emit('userList', getUsersInRoom(user.room));
// Join new room
user.room = newRoom;
socket.join(newRoom);
roomData.users.add(user.username);
// Clean chat when switching rooms - no history
socket.emit('roomSwitched', { success: true, room: newRoom });
socket.to(newRoom).emit('userJoined', user.username);
io.to(newRoom).emit('userList', getUsersInRoom(newRoom));
}
});
socket.on('createRoom', (data) => {
const user = users.get(socket.id);
if (user && data && data.room) {
const roomName = data.room.toString().trim().substring(0, 20);
const password = data.password;
const owner = data.owner || user.username;
if (roomName && !rooms.has(roomName) && roomName !== 'general') {
// Create new room
rooms.set(roomName, {
name: roomName,
owner: owner,
hasPassword: !!password,
users: new Set()
});
if (password) {
roomPasswords.set(roomName, password);
}
io.emit('roomCreated', {
room: roomName,
owner: owner,
hasPassword: !!password
});
io.emit('roomList', getRoomList());
} else {
socket.emit('roomError', { message: 'Room already exists or invalid name' });
}
}
});
socket.on('disconnect', () => {
const user = users.get(socket.id);
if (user) {
activeUsernames.delete(user.username.toLowerCase());
users.delete(socket.id);
// Update user profile status
if (userProfiles.has(user.username.toLowerCase())) {
const profile = userProfiles.get(user.username.toLowerCase());
profile.status = 'offline';
profile.lastSeen = new Date();
}
// Remove from room
if (rooms.has(user.room)) {
rooms.get(user.room).users.delete(user.username);
}
socket.to(user.room).emit('userLeft', user.username);
io.to(user.room).emit('userList', getUsersInRoom(user.room));
}
console.log('User disconnected:', socket.id);
});
socket.on('editMessage', (data) => {
const user = users.get(socket.id);
if (user && data && data.messageId && data.newMessage) {
// Find the message in history
const message = messageHistory.find(msg =>
msg.id == data.messageId &&
msg.username === user.username &&
msg.room === user.room
);
if (message) {
const cleanMessage = data.newMessage.toString().trim().substring(0, 500);
message.message = cleanMessage;
message.edited = true;
// Broadcast edit to room
io.to(user.room).emit('messageEdited', {
messageId: data.messageId,
newMessage: cleanMessage
});
}
}
});
socket.on('reaction', (data) => {
const user = users.get(socket.id);
if (user && data && data.messageId && data.emoji) {
// Find the message in history
const message = messageHistory.find(msg => msg.id == data.messageId && msg.room === user.room);
if (message) {
if (!message.reactions) message.reactions = {};
if (!message.reactions[data.emoji]) message.reactions[data.emoji] = [];
const userIndex = message.reactions[data.emoji].indexOf(user.username);
if (userIndex > -1) {
// Remove reaction
message.reactions[data.emoji].splice(userIndex, 1);
if (message.reactions[data.emoji].length === 0) {
delete message.reactions[data.emoji];
}
} else {
// Add reaction
message.reactions[data.emoji].push(user.username);
}
// Broadcast updated reactions to room
io.to(user.room).emit('reaction', {
messageId: data.messageId,
reactions: message.reactions
});
}
}
});
});
function getUsersInRoom(room) {
return Array.from(users.values())
.filter(user => user.room === room)
.map(user => {
const profile = userProfiles.get(user.username.toLowerCase()) || {};
return {
username: user.username,
status: profile.status || 'online',
bio: profile.bio || '',
avatar: profile.avatar || ''
};
});
}
function getRoomList() {
return Array.from(rooms.values()).map(room => ({
name: room.name,
owner: room.owner,
hasPassword: room.hasPassword,
userCount: room.users.size
}));
}
const PORT = process.env.PORT || 3002;
server.listen(PORT, () => {
console.log(`Chat server running on http://localhost:${PORT}`);
});