diff --git a/backend/src/controllers/chatControllers.ts b/backend/src/controllers/chatControllers.ts index a69b3cd..2df40f3 100644 --- a/backend/src/controllers/chatControllers.ts +++ b/backend/src/controllers/chatControllers.ts @@ -1,6 +1,7 @@ import { Request, Response } from 'express'; import { PrismaClient } from '@prisma/client'; + const prisma = new PrismaClient(); @@ -8,6 +9,7 @@ export const getChatHistory = async (req: Request, res: Response) => { const { roomId } = req.params; try { + const messages = await prisma.message.findMany({ where: { chatRoomId: roomId }, include: { @@ -22,8 +24,6 @@ export const getChatHistory = async (req: Request, res: Response) => { message: message.content, at: message.timestamp, })); - - // Send the formatted messages as a JSON response res.json(messageFormat); } catch (error) { diff --git a/backend/src/server.ts b/backend/src/server.ts index cc294a3..81511ff 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,23 +1,25 @@ import WebSocket, { Server } from 'ws'; import { PrismaClient } from '@prisma/client'; +import sendMail from './mail/sendMail'; const prisma = new PrismaClient(); const wss = new Server({ port: 8080 }); let clientCount = 0; -// Map to track which rooms each client is in const clientRoomMap = new Map>(); // WebSocket -> Set of room IDs wss.on('connection', (ws) => { const clientId = ++clientCount; // Increment and get the client ID console.log(`Client${clientId} connected`); + const user = []; - // Initialize client-room mapping clientRoomMap.set(ws, new Set()); ws.on('message', async (message) => { const { type, data } = JSON.parse(message.toString()); // data will contain roomId, userId, targetUserId, and message // type will contain 'createRoom', 'joinRoom', 'sendMessage' + const roomId = data.roomId; + switch (type) { case 'createRoom': @@ -72,6 +74,20 @@ wss.on('connection', (ws) => { case 'sendMessage': try { + const chatRoom = await prisma.chatRoom.findUnique({ + where: { id: roomId }, + include: { + users: { + select: { user_id: true, username: true, email: true } // Ensure to fetch email + } + } + }); + + if (!chatRoom) { + ws.send(JSON.stringify({ type: 'error', data: { message: 'Chat room not found' } })); + return; + } + const newMessage = await prisma.message.create({ data: { content: data.message, @@ -87,6 +103,65 @@ wss.on('connection', (ws) => { if (rooms?.has(data.roomId)) { client.send(JSON.stringify({ type: 'newMessage', data: { roomId: data.roomId, message: newMessage } })); } + } else { + + chatRoom.users.forEach(user => { + + const isUserConnected = [...wss.clients].some(c => clientRoomMap.get(c)?.has(data.roomId) && c.readyState === WebSocket.OPEN); + + if (!isUserConnected && user.user_id !== data.userId) { + const htmlContent = ` + + + + + +
+

New Message in Chat Room ${data.roomId}

+

You've received a new message in chat room ${data.roomId}.

+

Click the link below and enter the room id to view the message:

+

Join Room

+ +
+ + + `; + sendMail(htmlContent, user.email, "Message notification"); + } + }); } }); } catch (error) {