-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from raj-mistry-01/main
ChatApp
- Loading branch information
Showing
70 changed files
with
4,510 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
Empty file.
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,13 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const connectToMongo = async () => { | ||
try { | ||
await mongoose.connect("mongoStr"); | ||
console.log("Connected to MongoDB"); | ||
} catch (error) { | ||
console.error("Error connecting to MongoDB:", error); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
module.exports = connectToMongo; |
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,20 @@ | ||
function convertUTCToIST(utcDateString) { | ||
const utcDate = new Date(utcDateString); | ||
const utcTime = utcDate.getTime(); | ||
const istOffset = 5.5 * 60 * 60 * 1000; | ||
const istTime = utcTime + istOffset; | ||
const istDate = new Date(istTime); | ||
let hours = istDate.getUTCHours(); | ||
const minutes = istDate.getUTCMinutes(); | ||
const ampm = hours >= 12 ? 'PM' : 'AM'; | ||
hours = hours % 12; | ||
hours = hours ? hours : 12; | ||
const strHours = hours.toString().padStart(2, '0'); | ||
const strMinutes = minutes.toString().padStart(2, '0'); | ||
return `${strHours}:${strMinutes} ${ampm}`; | ||
} | ||
|
||
// Example usage: | ||
const utcDateString = '2024-07-12T16:22:40.333+00:00'; | ||
const istTimeString = convertUTCToIST(utcDateString); | ||
console.log(istTimeString); |
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,87 @@ | ||
//mongodb://localhost:27017/chatgram?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false | ||
const express = require("express"); | ||
const cors = require("cors"); | ||
const socketIo = require("socket.io"); | ||
const ChatMessage = require("./models/ChatMessages.js") | ||
const Typing = require("./models/Typing.js") | ||
const connectToMongo = require("./database.js") | ||
connectToMongo(); | ||
const app = express(); | ||
app.use(cors()); | ||
app.use(express.json()); | ||
const port = 5000 | ||
const server = app.listen(port, () => { | ||
console.log(`App listening at http://localhost:${port}`); | ||
}); | ||
|
||
const io = socketIo(server, { | ||
cors: { | ||
origin: "*", | ||
methods: ["GET", "POST"] | ||
} | ||
}); | ||
|
||
const onlineUsersMap = {} | ||
|
||
const emitOnlineUsers = () => { | ||
console.log("backend"); | ||
io.emit("OnlineIds",Object.keys(onlineUsersMap)) | ||
} | ||
|
||
io.on('connection', (socket) => { | ||
socket.on("newuserConnected",(userId)=>{ | ||
console.log("Signed Up or Loggged In"); | ||
onlineUsersMap[userId] = socket.id | ||
console.log(onlineUsersMap); | ||
emitOnlineUsers() | ||
}) | ||
socket.on('disconnect', () => { | ||
console.log('Client disconnected'); | ||
const userId = Object.keys(onlineUsersMap).find(key => onlineUsersMap[key] === socket.id) | ||
delete onlineUsersMap[userId]; | ||
console.log(onlineUsersMap); | ||
emitOnlineUsers() | ||
}); | ||
socket.on("logoutuser",(socketId)=>{ | ||
console.log("User Logged Out"); | ||
const userId = Object.keys(onlineUsersMap).find(key => onlineUsersMap[key] === socketId) | ||
delete onlineUsersMap[userId]; | ||
console.log(onlineUsersMap); | ||
emitOnlineUsers() | ||
}) | ||
}); | ||
|
||
|
||
|
||
// ==> all collection real time | ||
|
||
const setupChangeStream = (collectionName, eventName) => { | ||
const changeStream = collectionName.watch(); | ||
|
||
changeStream.on('change', (change) => { | ||
io.emit(eventName, change.fullDocument); | ||
}); | ||
}; | ||
|
||
setupChangeStream(Typing, 'typingdet'); | ||
setupChangeStream(ChatMessage, 'newMessage'); | ||
|
||
|
||
|
||
process.on('unhandledRejection', (error) => { | ||
console.error('Unhandled Rejection:', error); | ||
server.close(() => { | ||
process.exit(1); | ||
}); | ||
}); | ||
|
||
|
||
// routes of backend | ||
app.use("/api/authentication", require("./routes/authentication")); | ||
app.use("/api/fetchdatabase", require("./routes/FetchAllUser.js")); | ||
app.use("/api/notifications", require("./routes/Notification.js")); | ||
app.use("/api/friends", require("./routes/Friend.js")); | ||
app.use("/api/messages", require("./routes/ChatMessage.js")); | ||
app.use("/api/typing", require("./routes/Typingdet.js")); | ||
app.use("/api/genai",require("./routes/genai.js")) | ||
app.use("/api/photo",require("./routes/fetchthephoto.js")) |
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,18 @@ | ||
const jwt = require("jsonwebtoken") | ||
const JWT_SECRET = "escapethematrix" | ||
const fetchuser = (req,res,next) => { | ||
const authtoken = req.header('authToken') | ||
if (!authtoken) { | ||
return res.staus(400).send("Please bring up the token") | ||
} | ||
try { | ||
const data = jwt.verify(authtoken,JWT_SECRET) | ||
req.user = data.user | ||
next() | ||
} | ||
catch { | ||
return res.status(401).send("hehehe") | ||
} | ||
} | ||
|
||
module.exports = fetchuser |
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,23 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const chatMessageSchema = new mongoose.Schema({ | ||
sender: { | ||
type: String, | ||
required: true | ||
}, | ||
receiver: { | ||
type: String, | ||
required: true | ||
}, | ||
message: { | ||
type: String, | ||
required: true | ||
}, | ||
timestamp: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
const ChatMessage = mongoose.model('chatMessages', chatMessageSchema); | ||
module.exports = ChatMessage; |
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,18 @@ | ||
const mongoose = require("mongoose") | ||
const {Schema} = mongoose; | ||
const FriendsSchema = new Schema ({ | ||
KiskaFriend : { | ||
type : "String", | ||
required : true | ||
}, | ||
WhoisTheFriend : { | ||
type : "String", | ||
required : true | ||
}, | ||
date : { | ||
type : Date, | ||
default : Date.now | ||
} | ||
}) | ||
const Friends = mongoose.model("friends",FriendsSchema) | ||
module.exports = Friends |
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,20 @@ | ||
const mongoose = require("mongoose") | ||
const {Schema} = mongoose; | ||
const NotificationSchema = Schema({ | ||
fromEmail : { | ||
type : String, | ||
required : true, | ||
unique : false, | ||
}, | ||
toEmail : { | ||
type : String, | ||
required : true, | ||
unique : false | ||
}, | ||
date : { | ||
type : Date, | ||
default : Date.now | ||
}, | ||
}) | ||
const Notificaion = mongoose.model("notification",NotificationSchema) | ||
module.exports = Notificaion |
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,18 @@ | ||
const mongoose = require("mongoose") | ||
const {Schema} = mongoose; | ||
const photo = Schema({ | ||
WhosPhoto : { | ||
type : String, | ||
required : true, | ||
unique : false, | ||
}, | ||
Image : { | ||
type : String, | ||
}, | ||
date : { | ||
type : Date, | ||
default : Date.now | ||
}, | ||
}) | ||
const Profilephoto = mongoose.model("profilephotos",NotificationSchema) | ||
module.exports = Profilephoto |
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,20 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const typing = new mongoose.Schema({ | ||
WhoisTheTyping : { | ||
type: String, | ||
required: true, | ||
}, | ||
KismeTyping : { | ||
type : String, | ||
required : true | ||
}, | ||
timestamp: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}) | ||
|
||
const Typing = mongoose.model('typingdetection', typing); | ||
|
||
module.exports = Typing |
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,28 @@ | ||
const mongoose = require("mongoose") | ||
const {Schema} = mongoose; | ||
const UserSchema = new Schema({ | ||
name : { | ||
type : String, | ||
required : true | ||
}, | ||
email : { | ||
type : String, | ||
required : true, | ||
unique : true | ||
}, | ||
password : { | ||
type : String, | ||
required : true | ||
}, | ||
profilephoto :{ | ||
type : String, | ||
required : true | ||
}, | ||
date : { | ||
type : Date, | ||
default : Date.now | ||
}, | ||
|
||
}) | ||
const User = mongoose.model("user",UserSchema) | ||
module.exports = User |
Oops, something went wrong.