Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature chat app #345

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions .github/workflows/auto-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ jobs:
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
<<<<<<< HEAD
const issue = context.payload.issue;
const issueBody = issue.body ? issue.body.toLowerCase() : '';
const issueTitle = issue.title.toLowerCase();

// Add gssoc label to all issues
=======
const isIssue = context.payload.issue !== undefined;
const item = isIssue ? context.payload.issue : context.payload.pull_request;
const itemBody = item.body ? item.body.toLowerCase() : '';
const itemTitle = item.title.toLowerCase();

// Add labels to both issues and pull requests
>>>>>>> acb0156e716581bdeabbb4c9a21a5ba8b16b29ee
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
4 changes: 4 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ SMTP_EMAIL=
SMTP_PASSWORD=
#Response mail where you want to get the feedback submitted of user
RESPONSE_MAIL=
# Your socket port number, recommended 8800
SOCKET_PORT=8800
# Your front end URL
FRONT_END_URL=
#huggingface api key for chatbot
HUGGINGFACE_API_KEY=

Expand Down
43 changes: 43 additions & 0 deletions backend/Controllers/ChatController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import ChatModel from "../models/chatModel.js";

export const createChat = async (req, res) => {
const newChat = new ChatModel({
members: [req.body.senderId, req.body.receiverId],
});
try {
const result = await newChat.save();
res.status(200).json(result);
} catch (error) {
res.status(500).json(error);
}
};

export const userChats = async (req, res) => {
try {
const chat = await ChatModel.find({
members: { $in: [req.params.userId] },
});
res.status(200).json(chat);
} catch (error) {
res.status(500).json(error);
}
};

export const findChat = async (req, res) => {
try {
const chat = await ChatModel.findOne({
members: { $all: [req.params.firstId, req.params.secondId] },
});
res.status(200).json(chat)
} catch (error) {
res.status(500).json(error)
}
};

const ChatController = {
findChat,
userChats,
createChat,
}

export default ChatController;
33 changes: 33 additions & 0 deletions backend/Controllers/MessageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import MessageModel from "../models/messageModel.js";

export const addMessage = async (req, res) => {
const { chatId, senderId, text } = req.body;
const message = new MessageModel({
chatId,
senderId,
text,
});
try {
const result = await message.save();
res.status(200).json(result);
} catch (error) {
res.status(500).json(error);
}
};

export const getMessages = async (req, res) => {
const { chatId } = req.params;
try {
const result = await MessageModel.find({ chatId });
res.status(200).json(result);
} catch (error) {
res.status(500).json(error);
}
};

const MessageController={
getMessages,
addMessage
}

export default MessageController;
20 changes: 20 additions & 0 deletions backend/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ const getAllUserName = async (req, res) => {
}
};

/**
* @route {GET} /api/user/:id
* @description Get details of a user by ID
* @access public
*/
const getUserById = async (req, res) => {
try {
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json({ success: false, message: "User not found" });
}
res.status(200).json({ success: true, user });
} catch (error) {
console.log(error);
res.status(500).json({ success: false, message: "Internal Server Error" });
}
};

/**
* @route {POST} /api/usernames
* @description Authenticates an User
Expand Down Expand Up @@ -261,7 +279,9 @@ const UserController = {
Signup,
Login,
getAllUserName,
getAllUsers, // Added new endpoint
verifyUserByToken,
getUserById,
forgotPassword,
resetPassword,
UpdateImage,
Expand Down
50 changes: 22 additions & 28 deletions backend/middleware/rateLimit.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
const rateLimit = (limit, windowMs) => {
const requestCounts = {};
const rateLimit = (limit, windowMs, endpoint = null) => {
const requestCounts = {};

return (req, res, next) => {
const ip = req.ip; // Get client IP
const currentTime = Date.now();
return (req, res, next) => {
const ip = req.ip;
const currentTime = Date.now();
const key = endpoint ? `${ip}-${endpoint}` : ip; // Track by endpoint

// If this IP doesn't exist, initialize it
if (!requestCounts[ip]) {
requestCounts[ip] = { count: 1, startTime: currentTime };
} else {
const { count, startTime } = requestCounts[ip];

if (currentTime - startTime < windowMs) {
// If the limit is exceeded, block the request
if (count >= limit) {
return res.status(429).json({
message: "Too many requests, please slow down!",
});
}
// Otherwise, increment the request count
requestCounts[ip].count++;
} else {
// Reset the count and time if the time window has passed
requestCounts[ip] = { count: 1, startTime: currentTime };
}
if (!requestCounts[key]) {
requestCounts[key] = { count: 1, startTime: currentTime };
} else {
const { count, startTime } = requestCounts[key];
if (currentTime - startTime < windowMs) {
if (count >= limit) {
return res.status(429).json({
message: "Too many requests, please slow down!",
});
}

next();
};
requestCounts[key].count++;
} else {
requestCounts[key] = { count: 1, startTime: currentTime };
}
}
next();
};
};

export { rateLimit };
15 changes: 15 additions & 0 deletions backend/models/chatModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mongoose from "mongoose";

const ChatSchema = new mongoose.Schema(
{
members: {
type: Array,
},
},
{
timestamps: true,
}
);

const ChatModel = mongoose.model("Chat", ChatSchema);
export default ChatModel;
21 changes: 21 additions & 0 deletions backend/models/messageModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mongoose from "mongoose";

const MessageSchema = new mongoose.Schema(
{
chatId: {
type: String,
},
senderId: {
type: String,
},
text: {
type: String,
},
},
{
timestamps: true,
}
);

const MessageModel = mongoose.model("Message", MessageSchema);
export default MessageModel
Loading