Skip to content

Commit

Permalink
Added feature to view all messages recieved
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan-git committed Dec 27, 2024
1 parent e0128c2 commit cbb72fb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
22 changes: 20 additions & 2 deletions backend/controllers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Conversation } from "../models/communication.model.js";
export const sendMsg = async (req, res) => {
try {
const { message } = req.body;
const senderId = req.params.id;
const recieverId = req.user._id;
const recieverId = req.params.id;
const senderId = req.user._id;
const msg = new Message({
senderId,
recieverId,
Expand Down Expand Up @@ -43,3 +43,21 @@ export const sendMsg = async (req, res) => {
res.status(500).json({ error: "error while sending message" });
}
};

export const getMsg = async (req, res) => {
let mess = [];
const otherId = req.params.id;
const thisId = req.user._id;
const conv = await Conversation.findOne({
participants: { $all: [otherId, thisId] },
}).populate("messages");
if (!conv) {
res.send("No messages found");
} else {
res.send(
conv.messages.map((data) => {
return data.message;
})
);
}
};
14 changes: 14 additions & 0 deletions backend/controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { User } from "../models/user.auth.js";

export const getUsers = async (req, res) => {
try {
const thisId = req.user._id;
const allUsers = await User.find({ _id: { $ne: thisId } }).select(
"-password"
);
res.send(allUsers);
} catch (error) {
console.log("error in obtaining users for sidebar", error.message);
res.status(500).json({ error: "Internal server error" });
}
};
6 changes: 4 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cookieParser from "cookie-parser";

import authrouter from "./routes/user.auth.js";
import msgRouter from "./routes/message.route.js";
import usersRouter from "./routes/users.js";
import { authConn } from "./db/user.auth.js";

dotenv.config();

const app = express();
const PORT = process.env.PORT || 5000;

Expand All @@ -15,9 +17,9 @@ app.use(cookieParser());

app.use("/api/auth", authrouter);
app.use("/api/message", msgRouter);
app.use("/api/users", usersRouter);

app.listen(PORT, () => {
authConn();
console.log(`Listening to ${PORT}`);
app.use("/api/auth", authrouter);
});
4 changes: 3 additions & 1 deletion backend/routes/message.route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import express from "express";
import { sendMsg } from "../controllers/message.js";
import { sendMsg, getMsg } from "../controllers/message.js";
import protectRoute from "../middleware/protectRoute.js";

const router = express.Router();

router.post("/send/:id", protectRoute, sendMsg);

router.get("/:id", protectRoute, getMsg);

export default router;
9 changes: 9 additions & 0 deletions backend/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from "express";
import { getUsers } from "../controllers/users.js";
import protectRoute from "../middleware/protectRoute.js";

const router = express.Router();

router.get("/", protectRoute, getUsers);

export default router;

0 comments on commit cbb72fb

Please sign in to comment.