Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
Somnath-Chattaraj committed Oct 23, 2024
2 parents 13f02c5 + add1f48 commit d98b9ec
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 104 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build and Deploy to Docker Hub
on:
push:
branches:
- master
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v2
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker image
uses: docker/build-push-action@v2
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: somnath910/statuscode1:latest
- name: Verify Pushed Image
run: docker pull somnath910/statuscode1:latest
9 changes: 2 additions & 7 deletions backend/src/controllers/chatControllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { PrismaClient } from '@prisma/client';
import { getCachedData, setCachedData } from '../lib/redis';


const prisma = new PrismaClient();

Expand All @@ -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: {
Expand All @@ -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) {
Expand Down
194 changes: 97 additions & 97 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,108 +73,108 @@ wss.on('connection', (ws) => {
break;

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,
sender: { connect: { user_id: data.userId } },
chatRoom: { connect: { id: data.roomId } },
}
});

// Broadcast the message to all clients in the room
wss.clients.forEach(client => {
const rooms = clientRoomMap.get(client);
if (client.readyState === WebSocket.OPEN && rooms?.has(data.roomId)) {
client.send(JSON.stringify({ type: 'newMessage', data: { roomId: data.roomId, message: newMessage } }));
}
});

chatRoom.users.forEach(user => {
const isUserConnected = [...wss.clients].some(
client => clientRoomMap.get(client)?.has(data.roomId) && client.readyState === WebSocket.OPEN && user.user_id === data.userId
);

console.log('User connected:', isUserConnected);
if (!isUserConnected && user.user_id !== data.userId) {
const htmlContent = `
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: #ffffff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
h1 {
color: #333;
}
p {
font-size: 16px;
line-height: 1.5;
color: #555;
}
a {
color: #007BFF;
text-decoration: none;
font-weight: bold;
}
.footer {
margin-top: 20px;
font-size: 12px;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h1>New Message in Chat Room ${data.roomId}</h1>
<p>You've received a new message in chat room <strong>${data.roomId}</strong>.</p>
<p>Click the link below and enter the room id to view the message:</p>
<p><a href="https://www.campusify.site/room/joinroom">Join Room</a></p>
<p class="footer">Thank you for using our service!</p>
</div>
</body>
</html>
`;

console.log('Sending email to', user.email);
sendMail(htmlContent, user.email, "Message notification");
}
});
} catch (error) {
ws.send(JSON.stringify({ type: 'error', data: { message: 'Failed to send message' } }));
}
break;

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,
sender: { connect: { user_id: data.userId } },
chatRoom: { connect: { id: data.roomId } },
}
});

// Broadcast the message to all clients in the room
wss.clients.forEach(client => {
const rooms = clientRoomMap.get(client);
if (client.readyState === WebSocket.OPEN && rooms?.has(data.roomId)) {
client.send(JSON.stringify({ type: 'newMessage', data: { roomId: data.roomId, message: newMessage } }));
}
});

// Send email to disconnected users
chatRoom.users.forEach(user => {
const isUserConnected = [...wss.clients].some(
client => clientRoomMap.get(client)?.has(data.roomId) && client.readyState === WebSocket.OPEN && user.user_id === data.userId
);

// Send email only to disconnected users and exclude the sender
if (!isUserConnected && user.user_id !== data.userId) {
const htmlContent = `
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: #ffffff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
h1 {
color: #333;
}
p {
font-size: 16px;
line-height: 1.5;
color: #555;
}
a {
color: #007BFF;
text-decoration: none;
font-weight: bold;
}
.footer {
margin-top: 20px;
font-size: 12px;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h1>New Message in Chat Room ${data.roomId}</h1>
<p>You've received a new message in chat room <strong>${data.roomId}</strong>.</p>
<p>Click the link below and enter the room id to view the message:</p>
<p><a href="https://www.campusify.site/room/joinroom">Join Room</a></p>
<p class="footer">Thank you for using our service!</p>
</div>
</body>
</html>
`;

console.log('Sending email to', user.email);
sendMail(htmlContent, user.email, "Message notification");
}
});
} catch (error) {
ws.send(JSON.stringify({ type: 'error', data: { message: 'Failed to send message' } }));
}
break;
default:
ws.send(JSON.stringify({ type: 'error', data: { message: 'Invalid message type' } }));
break;
}

});

ws.on('close', () => {
Expand Down

0 comments on commit d98b9ec

Please sign in to comment.