Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Welcome to [**Tezasia '23 Hackathon**](https://unstop.com/competitions/tezasia-h
Please find attached the rules and steps to submit your project for the hackathon :

## Step - 1: Fork the repository
Fork the given repository to your GitHub profile.
Fork the given repository to your GitHub profile.

![Screenshot from 2023-07-25 22-28-46](https://github.com/TauqeerAhmad5201/TezAsia-2k23/assets/68806440/ab08cb62-2521-4968-8933-14356844026b)

Expand Down
53 changes: 53 additions & 0 deletions olive/BACKEND/controllers/messagescontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const messagemodel = require("../model/mesasgemodel")


module.exports.addmessage=async (req,res,next)=>{
try{
const {from,to,message}=req.body
const data=await messagemodel.create({
message:{text:message},
users:[from,to],
sender:from,
})
if(data ) return res.json({msg:"messae added"})
return res.json({ msg:"failed to add msg"})
}catch(ex){
next(ex)
}

}



module.exports.getallmessage=async (req,res,next)=>{
try{
const{from,to}=req.body
const messages=await messagemodel
.find({
users:{
$all:[from,to]
}
})
.sort({updatedAt:1})
const projectedmessages=messages.map((msg)=>{
return{
fromself:msg.sender.toString()===from,
message:msg.message.text,
msgtime:msg.createdAt.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' }),
}
})
res.json(projectedmessages)
}catch(ex){
next(ex)
}
}

module.exports.deletemessage=async (req,next)=>{
try{
const receiver = req.params.currentchat;
console.log(receiver)
await messagemodel.deleteMany({users: receiver});
}catch(ex){
next(ex)
}
}
25 changes: 25 additions & 0 deletions olive/BACKEND/controllers/reportcontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const reportmodel = require("../model/reportmodel")



module.exports.report=async (req,res,next)=>{
try{
const {reportsenderusername,
reportrecieverusername,
reportsenderid,
reportreceiverid,
reportreason,}=req.body
const data=await reportmodel.create({
reportsenderusername,
reportrecieverusername,
reportsenderid,
reportreceiverid,
reportreason,
})
if(data ) return res.json({msg:"messae added"})
return res.json({ msg:"failed to add msg"})
}
catch(ex){
next(ex)
}
}
109 changes: 109 additions & 0 deletions olive/BACKEND/controllers/usercontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const User=require("../model/usermodel")
const bcrypt=require("bcrypt")
const mongoose = require('mongoose');

let db;

mongoose.connection.on('connected', () => {
db = mongoose.connection.db;
});



module.exports.signup=async (req,res,next)=>{
try{
const {username,email,password,photo}=req.body
const usernamecheck=await User.findOne({username})
if(usernamecheck)
return res.json({msg:"username already used",status:false})
const emailcheck=await User.findOne({email})
if(emailcheck)
return res.json({msg:"Entered Email is already registered",status:false})
const hashedpassword=await bcrypt.hash(password,10)
const user=await User.create({
email,
username,
password:hashedpassword,
photo,
})
delete user.password
return res.json({status:true,user})
}
catch(ex){
next(ex)
}
}

module.exports.login=async (req,res,next)=>{
try{
const {username,email,password}=req.body

const user=await User.findOne({$or: [{ email }, { username }]})
if(!user)
return res.json({msg:"This username is not registered! note: username and email are case sensitive",status:false})

const mail=await User.findOne({email})
if(!mail)
return res.json({msg:"this email is not registered note: username and email are case sensitive",status:false})

const ispassswordvalid=await bcrypt.compare(password,user.password)
const isPasswordValidWithEmail = await bcrypt.compare(password, user.password);
const isPasswordValidWithUsername = await bcrypt.compare(password, mail.password);

if(!isPasswordValidWithEmail && isPasswordValidWithUsername){
return res.json({msg:"Username is not registered with this email",status:false})
}
if(isPasswordValidWithEmail && !isPasswordValidWithUsername)
return res.json({msg:"Email is not registered with this username",status:false})
if(!isPasswordValidWithEmail && !isPasswordValidWithUsername)
return res.json({msg:"Wrong Password",status:false})
delete user.password

return res.json({status:true,user})
}
catch(ex){
next(ex)
}
}


module.exports.getallusers=async (req,res,next)=>{
try{
const users=await User.find({_id:{$ne:req.params.id}}).select([
"email",
"username",
"id",
"photo",
"createdAt"
])
return res.json(users)
}catch(ex){
next(ex)
}
}

module.exports.deleteuser=async (req,res,next)=>{
try{
const users=await User.deleteOne({ username: req.params.username })
return res.json(users)
}catch(ex){
next(ex)
}
}


module.exports.liveconnections=async (req,res,next)=>{
try{
if (!db) {
return res.status(500).json({ error: 'Database connection not ready' });
}

db.command({ serverStatus: 1 }, (err, result) => {
if (err) throw err;
res.json({ connectionCount: result.connections.current });
});
}catch(ex){
next(ex)
}
}

78 changes: 78 additions & 0 deletions olive/BACKEND/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@


const express=require("express")
const cors=require("cors")
const mongoose=require("mongoose")
const bodyParser = require('body-parser');
const Userroutes=require("./routes/userroutes")
const socket=require("socket.io")
const messageroute=require("./routes/messagesroute")

const app=express()
require("dotenv").config()

app.use(bodyParser.json({ limit: "200mb" }));
app.use(bodyParser.urlencoded({ limit: "200mb", extended: true, parameterLimit: 1000000 }));
app.use(cors())
app.use(express.json());


app.use("/api/auth",Userroutes)
app.use("/api/messages",messageroute)





const PORT=process.env.PORT || 5000

/*const dburl='mongodb://0.0.0.0:27017/chat'*/
/*const dburl='mongodb+srv://PRADEEP123:'+encodeURIComponent('Knight@9026')+'@cluster0.x5jbf4w.mongodb.net/chat?retryWrites=true&w=majority'*/
const dburl=process.env.db_url



mongoose.connect(dburl, {
useNewUrlParser: true,
useUnifiedTopology: true
});

mongoose.connection.on('connected', () => {
console.log('Successfully connected to MongoDB');


});








const server=app.listen(process.env.PORT,()=>{
console.log(`server started at port ${PORT}`)
})

const io =socket(server,{
cors:{
origin:"http://localhost:3000",
Credentials:true,
}
})


global.onlineUsers=new Map()

io.on("connection",(socket)=>{
global.chatSocket=socket
socket.on("add-user",(userId)=>{
onlineUsers.set(userId,socket.id)
})
socket.on("send-msg",(data)=>{
const sendUserSocket=onlineUsers.get(data.to)
if(sendUserSocket){
socket.to(sendUserSocket).emit("msg-recieve",data.message)
}
})
})
22 changes: 22 additions & 0 deletions olive/BACKEND/model/mesasgemodel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose=require("mongoose")

const messageschema=new mongoose.Schema({
message:{
text:{
type:String,
required:true,
},
},
users:Array,
sender:{
type:mongoose.Schema.Types.ObjectId,
ref:"user",
required:true,
}
},
{
timestamps:true
}
)

module.exports=mongoose.model("messages",messageschema)
30 changes: 30 additions & 0 deletions olive/BACKEND/model/reportmodel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const mongoose = require('mongoose');

const reportschema = new mongoose.Schema({
reportsenderusername: {
type: String,
required: true
},
reportrecieverusername: {
type: String,
required: true
},
reportsenderid: {
type: String,
required: true
},
reportreceiverid: {
type: String,
required: true
},
reportreason: {
type: String,
required: true
}
},
{
timestamps:true
}
);

module.exports = mongoose.model('Reports', reportschema);
35 changes: 35 additions & 0 deletions olive/BACKEND/model/usermodel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const mongoose=require("mongoose")

const userschema=new mongoose.Schema({
username:{
type:String,
required:true,
min:5,
max:20,
unique:true
},
email:{
type:String,
required:true,
unique:true,
max:34,
},password:{
type:String,
required:true,
unique:true,
max:20,
},photo:{
type:String,
},
blocked: {
type: Boolean,
default: false
}
}
,
{
timestamps:true
}
)

module.exports=mongoose.model("users",userschema)
Loading