From b9977f63330339587a97818f2ac86ec696411343 Mon Sep 17 00:00:00 2001 From: Somnath Chattaraj Date: Wed, 23 Oct 2024 21:07:37 +0530 Subject: [PATCH] Fix message --- backend/src/controllers/chatControllers.ts | 9 ++------- backend/src/server.ts | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/backend/src/controllers/chatControllers.ts b/backend/src/controllers/chatControllers.ts index 35046e8..2df40f3 100644 --- a/backend/src/controllers/chatControllers.ts +++ b/backend/src/controllers/chatControllers.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; import { PrismaClient } from '@prisma/client'; -import { getCachedData, setCachedData } from '../lib/redis'; + const prisma = new PrismaClient(); @@ -9,11 +9,7 @@ export const getChatHistory = async (req: Request, res: Response) => { const { roomId } = req.params; try { - const cacheKey = `search:${roomId}`; - const cachedResults = await getCachedData(cacheKey); - if (cachedResults) { - return res.status(200).json(JSON.parse(cachedResults)); - } + const messages = await prisma.message.findMany({ where: { chatRoomId: roomId }, include: { @@ -28,7 +24,6 @@ export const getChatHistory = async (req: Request, res: Response) => { message: message.content, at: message.timestamp, })); - await setCachedData(cacheKey, JSON.stringify(messageFormat), 3600); res.json(messageFormat); } catch (error) { diff --git a/backend/src/server.ts b/backend/src/server.ts index 19adcb6..81511ff 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -104,9 +104,9 @@ wss.on('connection', (ws) => { client.send(JSON.stringify({ type: 'newMessage', data: { roomId: data.roomId, message: newMessage } })); } } else { - // Handle sending email to disconnected users + chatRoom.users.forEach(user => { - // Check if user is disconnected + const isUserConnected = [...wss.clients].some(c => clientRoomMap.get(c)?.has(data.roomId) && c.readyState === WebSocket.OPEN); if (!isUserConnected && user.user_id !== data.userId) {