-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
472 lines (399 loc) · 17.2 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
const path = require('path');
const fs = require('fs');
const express = require('express');
const WebSocket = require('ws');
const Database = require('./Database.js');
const SessionManager = require('./SessionManager.js');
const sessionManager = new SessionManager();
const crypto = require('crypto');
const {generateChatResponse, transcribeAudio} = require('./OpenAI-API-Calls.js');
const fileUpload = require('express-fileupload');
const mongoUrl = process.env.MongoDB_Driver_URL;
const dbName = 'easytext-messenger';
const db = new Database(mongoUrl, dbName);
db.connected.then(() => {
console.log(`[MongoClient] Connected to ${mongoUrl}/${dbName}`);
}).catch(err => {
console.error('[MongoClient] Connection error:', err);
});
function logRequest(req, res, next){
console.log(`${new Date()} ${req.ip} : ${req.method} ${req.path}`);
next();
}
const host = 'localhost';
const port = 3000;
const clientApp = path.join(__dirname, 'client');
const distPath = path.join(clientApp, 'dist');
const modelsPath = path.join(__dirname, 'models');
const faceapiPath = path.join(__dirname, 'face-api.min.js');
const broker = new WebSocket.Server({ port: 8000 });
const messageBlockSize = 10;
// express app
const app = express();
// to upload audio files
app.use(fileUpload({
createParentPath: true
}));
app.use(express.json()) // to parse application/json
app.use(express.urlencoded({ extended: true })) // to parse application/x-www-form-urlencoded
app.use(logRequest); // logging for debugdebug
app.use('/face-api.min.js', express.static(faceapiPath));
app.use('/models', express.static(modelsPath));
// serve static files from 'dist' directory under 'clientApp'
app.use('/dist', express.static(distPath));
let messages = {};
function sanitizeString(str) {
return str.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/&(?!(amp;|lt;|gt;|quot;|apos;|#039;))/g, '&');
}
function saveConversationIfNecessary(roomId, ws = null) {
if (messages[roomId] && messages[roomId].length > 0) {
if (messages[roomId].length >= messageBlockSize || ws == null) {
const conversation = {
room_id: roomId,
timestamp: Date.now(),
messages: messages[roomId].slice()
};
db.addConversation(conversation)
.then(() => {
console.log('Conversation saved for room:', roomId);
messages[roomId] = [];
})
.catch(err => {
console.error('Error saving conversation for room:', roomId, err);
});
} else if (ws) {
// timeout to save messages if block size is not reached within 10 seconds
clearTimeout(ws.saveConversationTimeout);
ws.saveConversationTimeout = setTimeout(() => {
saveConversationIfNecessary(roomId);
}, 10000);
}
}
}
db.getRooms()
.then(rooms => {
rooms.forEach(room => {
messages[room._id] = [];
});
})
.catch(err => {
console.error('Error initializing messages:', err);
});
broker.on('connection', function connection(ws, req) {
console.log('Client connected');
const cookieHeader = req.headers.cookie;
if (!cookieHeader) {
console.log('No cookie header, closing WebSocket connection.');
ws.close();
return;
}
const cookies = cookieHeader.split(';').reduce((acc, cookie) => {
const [name, value] = cookie.split('=').map(c => c.trim());
acc[name] = value;
return acc;
}, {});
const sessionToken = cookies['session'];
if (!sessionManager.isValidSession(sessionToken)) {
console.log('Invalid session, closing WebSocket connection.');
ws.close();
return;
}
ws.username = sessionManager.getUsername(sessionToken);
ws.on('message', function incoming(messageBuffer) {
console.log('Received message:', messageBuffer);
try {
let parsedMessage = JSON.parse(messageBuffer.toString());
let roomId = parsedMessage.roomId;
parsedMessage.text = sanitizeString(parsedMessage.text);
parsedMessage.username = ws.username;
let username = parsedMessage.username;
let text = parsedMessage.text;
const serializedMessage = JSON.stringify(parsedMessage);
broker.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
console.log('Broadcasting message to client');
client.send(serializedMessage);
}
});
if (!messages[roomId]) {
messages[roomId] = [];
}
messages[roomId].push({ username, text });
saveConversationIfNecessary(roomId, ws);
} catch (error) {
console.error('Error parsing incoming message:', error);
}
});
ws.on('close', function () {
saveConversationIfNecessary(ws.roomId);
console.log('Client disconnected');
});
});
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const user = await db.getUser(username);
if (!user) {
// res.redirect('/login');
res.status(401).send("Invalid username or password.")
} else {
if (isCorrectPassword(password, user.password)) {
sessionManager.createSession(res, username);
res.redirect('/');
} else {
// res.redirect('/login');
res.status(401).send("Invalid username or password.")
}
}
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
// endpoint to add a user in the database
app.post('/register', async (req, res) => {
console.log("Registering new user !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
const { newUsername, newPassword } = req.body;
try {
const userExists = await db.getUser(newUsername);
if (userExists) {
res.status(400).send('User already exists');
return;
}
const saltedHash = generateEncryptedPassword(newPassword); // consider switching to bcrypt for password hashing
const user = { username: newUsername, password: saltedHash };
await db.addUser(user);
sessionManager.createSession(res, newUsername);
res.redirect('/');
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.use('/login', express.static(clientApp + '/login.html', {extensions: ['html', 'css']}));
app.use('/register', express.static(clientApp + '/login.html', {extensions: ['html', 'css']}));
app.use('/', sessionManager.middleware, express.static(clientApp, {extensions: ['html', 'css']}));
app.get('/chat/:room_id/messages', sessionManager.middleware, (req, res) => {
const room_id = req.params.room_id;
const before = req.query.before ? parseInt(req.query.before) : Date.now();
db.getLastConversation(room_id, before)
.then(conversation => {
if (conversation) {
console.log('Server is sending the last conversation!');
res.json(conversation);
} else {
res.status(404).json({ error: `No conversation found for room ${room_id}` });
}
})
.catch(err => {
console.error('Error getting last conversation:', err);
res.status(500).json({ error: 'Internal Server Error' });
});
});
app.get('/chat/:room_id', sessionManager.middleware, async (req, res) => {
const room_id = req.params.room_id;
console.log("Room ID:", room_id);
let room = await db.getRoom(room_id);
if (room) {
res.json(room);
} else {
res.status(404).json({ error: `Room ${room_id} was not found` });
}
});
app.post('/chat', sessionManager.middleware, (req, res) => {
console.log("Received POST request to /chat with data:", req.body);
const roomData = req.body;
if (!roomData || !roomData.name) {
console.error("Name field is required");
res.status(400).json({ error: 'Name field is required' });
return;
}
roomData.name = sanitizeString(roomData.name);
db.getRooms().then(existingRooms => {
const newRoomId = `room-${existingRooms.length + 1}`;
const newRoom = {
_id: newRoomId,
name: roomData.name,
image: roomData.image || 'assets/default-room-icon.png'
};
return db.addRoom(newRoom);
})
.then(addedRoom => {
messages[addedRoom._id] = [];
console.log("New room added successfully");
res.status(200).json(addedRoom);
})
.catch(err => {
console.error("Error adding new room:", err);
res.status(500).json({ error: 'Internal Server Error' });
});
});
app.get('/chat', sessionManager.middleware, async (req, res) => {
try {
const rooms = await db.getRooms();
const chatData = rooms.map(room => {
return {
id: room._id,
name: room.name,
image: room.image,
messages: messages[room._id] || []
};
});
res.json(chatData);
} catch (error) {
console.error("Error handling GET request for /chat:", error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// endpoint to get all users
app.get('/users', sessionManager.middleware, async (req, res) => {
try {
const users = await db.getUsers();
res.json(users.map(user => ({ username: user.username })));
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// endpoint to get generated response
app.get('/chat/:room_id/generatedresponse', sessionManager.middleware, (req, res) => {
const room_id = req.params.room_id;
const limit = parseInt(req.query.limit) || 10;
const username = req.query.username;
const user = req.query.user;
console.log("Trying to get generated response");
db.getLatestMessages(room_id, limit)
.then(async (messages) => {
console.log("Latest messages: ", messages);
// do ChatGPT stuff
const formattedMessages = [
{ role: "system", content: `You are going to come up with a helpful response based on the context of the conversation below while pretending to be ${user}.` }
];
/* need to format messages for ChatGPT. For example
* messages: [{ role: "system", content: "You are going to come up with a helpful response." },
{ role: "user", content: "alice:I want to get better at coding"},
{ role: "user", content: "bob:I can help you"},,
{ role: "user", content: "alice:sure, what do you recommend?"},,
{ role: "user", content: "Come up with a response to alice"},] // the selected user was alice
*/
messages.forEach(msg => {
formattedMessages.push({ role: "user", content: `${msg.username}:${msg.text}` });
});
formattedMessages.push({ role: "user", content: `Come up with a response to ${username} as if you were writing a text message (no need to write "user: ")` });
console.log("Formatted messages for ChatGPT: ", formattedMessages);
try {
const response = await generateChatResponse(formattedMessages);
res.json({ response });
} catch (error) {
console.error('Error generating chat response:', error);
res.status(500).json({ error: 'Error generating response' });
}
})
.catch(err => {
console.error('Error getting latest messages:', err);
res.status(500).json({ error: 'Internal Server Error' });
});
});
// endpoint to get emotional response
app.get('/chat/:room_id/emotionalresponse', sessionManager.middleware, (req, res) => {
const room_id = req.params.room_id;
const emotion = req.query.emotion;
const limit = 10;
const username = req.query.username;
console.log(`Trying to get emotional response: ${emotion}`);
db.getLatestMessages(room_id, limit)
.then(async (messages) => {
// do ChatGPT stuff
const formattedMessages = [
{ role: "system", content: "You are an emotional chameleon that can pretend to be someone else." },
];
/* need to format messages for ChatGPT. For example
* messages: [{ role: "system", content: "You are going to come up with a helpful response." },
{ role: "user", content: "alice:I want to get better at coding"},
{ role: "user", content: "bob:I can help you"},,
{ role: "user", content: "alice:sure, what do you recommend?"},,
{ role: "user", content: "Come up with a response to alice"},] // the selected user was alice
*/
messages.forEach(msg => {
formattedMessages.push({ role: "user", content: `${msg.username}:${msg.text}` });
});
formattedMessages.push({ role: "system", content: `${username} is feeling ${emotion}.` });
formattedMessages.push({ role: "user", content: `Pretend you are ${username} and come up with an appropriate response that matches the emotion.` });
console.log("Formatted messages for ChatGPT: ", formattedMessages);
try {
const response = await generateChatResponse(formattedMessages);
res.json({ response });
} catch (error) {
console.error('Error generating chat response:', error);
res.status(500).json({ error: 'Error generating response' });
}
})
.catch(err => {
console.error('Error getting latest messages:', err);
res.status(500).json({ error: 'Internal Server Error' });
});
});
// endpoint to post audio file, make API call to Whisper model, and get translated text
app.post('/voice-to-text', async (req, res) => {
console.log(req.files);
if (!req.files || !req.files.audio) {
return res.status(400).send('No audio file uploaded.');
}
const audioFile = req.files.audio;
const savePath = path.join(__dirname, 'uploads', audioFile.name);
try {
await audioFile.mv(savePath);
const text = await transcribeAudio(savePath);
res.json({ text });
} catch (error) {
console.error('Failed to transcribe audio:', error);
res.status(500).send('Error processing audio');
}
finally {
fs.unlink(savePath, err => {
if (err) console.error('Failed to delete audio file:', err);
});
}
});
'/app.js', sessionManager.middleware, express.static(path.join(clientApp, 'app.js'));
'/index.html', sessionManager.middleware, express.static(path.join(clientApp, 'index.html'));
'/index', sessionManager.middleware, express.static(path.join(clientApp, 'index'));
'/', sessionManager.middleware, express.static(path.join(clientApp));
app.use((err, req, res, next) => {
if (err instanceof SessionManager.Error) {
if (req.headers.accept == 'application/json') {
res.status(401).send(err);
} else {
res.redirect('/login');
}
} else {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.get('/profile', sessionManager.middleware, (req, res) => {
res.json({ username: req.username });
});
app.get('/logout', (req, res) => {
sessionManager.deleteSession(req);
res.redirect('/login');
});
function isCorrectPassword(password, saltedHash) {
const salt = saltedHash.substring(0, 20);
const originalHash = saltedHash.substring(20);
const hash = crypto.createHash('sha256').update(password + salt).digest('base64');
return hash === originalHash;
}
function generateEncryptedPassword(plaintextPassword) {
const salt = crypto.randomBytes(20).toString('hex').slice(0, 20);
const hash = crypto.createHash('sha256').update(plaintextPassword + salt).digest('base64');
const saltedHash = salt + hash;
return saltedHash;
}
app.listen(port, '0.0.0.0', () => {
console.log(`${new Date()} App Started. Listening on ${host}:${port}, serving ${clientApp}`);
});