-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c28ea6
commit 73a0d86
Showing
9 changed files
with
483 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
MongoDB_URL = "mongodb+srv://chatapp:4qXzEAQv4Q7i17HO@cluster0.0uitnr9.mongodb.net/?retryWrites=true&w=majority" | ||
MongoDB_URL = "mongodb+srv://usk3210:Z4yljANhOWHwbl6q@cluster0.mmcwbvx.mongodb.net/?retryWrites=true&w=majority" | ||
Port = 8080 | ||
secret = "c#$%&HKKM4573$%*jk" | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const Router = require("express").Router() | ||
const { accessChat, createGroupChat, fetchChats, renameGroup, addToGroup, removeFromGroup} = require("../controllers/chatController") | ||
const { protectRoute } = require("../middlewares/authmiddleware") | ||
|
||
|
||
Router.post("/",protectRoute, accessChat) | ||
Router.get("/",protectRoute, fetchChats) | ||
Router.post("/group",protectRoute, createGroupChat) | ||
Router.put("/rename",protectRoute, renameGroup) | ||
Router.put("/groupadd",protectRoute, addToGroup) | ||
Router.put("/groupremove",protectRoute, removeFromGroup) | ||
|
||
|
||
module.exports = Router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
const express = require("express") | ||
const router = express.Router() | ||
const { registerUser,loginUser,getAllusers } = require("../controllers/usercontroller") | ||
|
||
const { body, validationResult } = require('express-validator'); | ||
const { protectRoute } = require("../middlewares/authmiddleware"); | ||
|
||
|
||
|
||
|
||
router.post("/register", | ||
//email must be email | ||
body("email").isEmail(), | ||
// is name is alhabetical | ||
body("name").isAlphanumeric(), | ||
//password length is minimum 5 | ||
body("password").isLength({min:5}),(req,res,next)=>{ | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
next() | ||
} | ||
,registerUser) | ||
|
||
router.post("/login",body("email").isEmail(), | ||
(req,res,next)=>{ | ||
const errors = validationResult(req); | ||
if(!errors.isEmpty()){ | ||
return res.status(400).json({errors:errors.array()}) | ||
} | ||
next() | ||
} ,loginUser) | ||
|
||
//get all users | ||
|
||
router.get("/",protectRoute, getAllusers) | ||
|
||
|
||
|
||
router.post("/login") | ||
router.post("/register") | ||
|
||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
|
||
const Chat = require("../models/chatmodel") | ||
|
||
const accessChat = async(req,res)=>{ | ||
const {userId} = req.body | ||
try{ | ||
if(!userId){ | ||
return res.status(400).json({ | ||
status:"Failed", | ||
error:"userId param not sent with requst" | ||
}) | ||
} | ||
//check chat exists with these userId | ||
let isChat = await Chat.find({ | ||
isgroupchat:false, | ||
users:{$all:[req.user._id,userId]} | ||
|
||
|
||
}).populate("users", "-password").populate("latestMessage") | ||
let chatdata; | ||
console.log(isChat) | ||
if(isChat.length>0){ | ||
return res.status(200).json({ | ||
isChat | ||
}) | ||
} | ||
else{ | ||
chatdata = { | ||
chatName :"sender", | ||
isgroupchat:false, | ||
users:[req.user._id,userId] | ||
} | ||
} | ||
console.log(chatdata) | ||
const createdChat = await Chat.create(chatdata) | ||
const Fullchat = await Chat.find({_id:createdChat._id}).populate("users", "-password") | ||
|
||
return res.status(200).json({ | ||
Fullchat | ||
}) | ||
|
||
|
||
|
||
|
||
} | ||
catch(err){ | ||
return res.status(500).json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
|
||
|
||
|
||
} | ||
|
||
} | ||
|
||
const fetchChats = async(req,res)=>{ | ||
|
||
try{ | ||
|
||
const chats = await Chat.find({users:{$all:[req.user._id]}}) | ||
.populate("users", "-password") | ||
.populate("isgroupAdmin", "-password") | ||
.populate("latestMessage") | ||
.sort({updatedAt : -1}) | ||
return res.json({ | ||
status:"success", | ||
chats:chats | ||
}) | ||
|
||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
} | ||
|
||
|
||
} | ||
|
||
const createGroupChat = async(req,res) =>{ | ||
const {users, name} = req.body | ||
|
||
|
||
//here we take bunch of users and name of groupchat from body | ||
try{ | ||
if(!req.body.users || !req.body.name){ | ||
return res.status(400).json({ | ||
status:"Failed", | ||
error:"Please fill all the fields" | ||
}) | ||
} | ||
//users coming through request is in json format so we parse it for actual array | ||
let users = JSON.parse(req.body.users) | ||
if(users.length>2){ | ||
return res.json({ | ||
status:"Failed", | ||
error:"more than 2users are required to form a group" | ||
}) | ||
} | ||
// we push current loggedin user in to users | ||
users.push(req.user) | ||
|
||
// create groupchat | ||
const groupChat = await Chat.create({ | ||
chatName: req.body.name, | ||
users:users, | ||
isgroupchat:true, | ||
isgroupAdmin: req.user // logged in user is the admin of group | ||
}) | ||
|
||
const fullGroupChat = await Chat.findOne({_id:groupChat.id}) | ||
.populate("users", "-password") | ||
.populate("isgroupAdmin", "-password") | ||
|
||
res.status(200).json({ | ||
status:"success", | ||
fullGroupChat:fullGroupChat | ||
}) | ||
|
||
} | ||
|
||
catch(err){ | ||
return res.status(500).json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
|
||
} | ||
|
||
} | ||
|
||
const renameGroup = async(req,res)=>{ | ||
try{ | ||
const {chatId,chatname} = req.body | ||
|
||
if(!chatId || !chatname){ | ||
return res.status(400).json({error:"all fields are mandatory"}) | ||
} | ||
|
||
const updatedChat = await Chat.findByIdAndUpdate(chatId, | ||
{chatName:chatname },{new:true}) | ||
.populate("users", "-password") | ||
.populate('isgroupAdmin', "-password") | ||
if(!updatedChat){ | ||
return res.status(404).json({ | ||
status:"Failed", | ||
error:"Chat Not Found" | ||
}) | ||
} | ||
else{ | ||
return res.status(200).json({ | ||
status:"success", | ||
updatedChat:updatedChat | ||
}) | ||
} | ||
|
||
} | ||
catch(err){ | ||
return res.status(500).json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
} | ||
} | ||
|
||
const addToGroup = async(req,res)=>{ | ||
try{ | ||
const {chatId, userId} = req.body | ||
const added = await Chat.findByIdAndUpdate(chatId, | ||
{ $push:{users:userId}}, | ||
{new:true}).populate("users", "-password") | ||
.populate('isgroupAdmin', "-password") | ||
|
||
if(!added){ | ||
return res.status(400).json({error:"chat was not found"}) | ||
} | ||
return res.json({ | ||
status:"success", | ||
added:added | ||
}) | ||
} | ||
catch(err){ | ||
res.status(500).json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
} | ||
} | ||
|
||
|
||
const removeFromGroup = async(req,res)=>{ | ||
const {chatId, userId} = req.body | ||
|
||
try{ | ||
const removed = await Chat.findByIdAndUpdate(chatId, | ||
{$pull:{users:userId}},{ | ||
new:true | ||
}).populate("users", "-password") | ||
.populate("isgroupAdmin", "-password") | ||
|
||
if(!removed){ | ||
return res.status(400).json({error:"chat was not found with that id"}) | ||
} | ||
return res.json({ | ||
status:"success", | ||
removed:removed | ||
}) | ||
|
||
|
||
} | ||
catch(err){ | ||
res.status(500).json({ | ||
status:"Failed", | ||
error:err.message | ||
}) | ||
} | ||
} | ||
|
||
|
||
module.exports = {accessChat, createGroupChat,fetchChats, renameGroup, addToGroup, removeFromGroup} |
Oops, something went wrong.