Skip to content

Commit

Permalink
Merge pull request #106 from raj-mistry-01/main
Browse files Browse the repository at this point in the history
ChatApp
  • Loading branch information
ianshulx authored Oct 17, 2024
2 parents ccb89fa + 5b8cfa0 commit 9749f92
Show file tree
Hide file tree
Showing 70 changed files with 4,510 additions and 0 deletions.
23 changes: 23 additions & 0 deletions chatgram/.gitignore
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 added chatgram/backend/.env
Empty file.
13 changes: 13 additions & 0 deletions chatgram/backend/database.js
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;
20 changes: 20 additions & 0 deletions chatgram/backend/exp.js
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);
87 changes: 87 additions & 0 deletions chatgram/backend/index.js
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"))
18 changes: 18 additions & 0 deletions chatgram/backend/middleware/fetchuser.js
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
23 changes: 23 additions & 0 deletions chatgram/backend/models/ChatMessages.js
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;
18 changes: 18 additions & 0 deletions chatgram/backend/models/Friends.js
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
20 changes: 20 additions & 0 deletions chatgram/backend/models/Notifications.js
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
18 changes: 18 additions & 0 deletions chatgram/backend/models/Profilephoto.js
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
20 changes: 20 additions & 0 deletions chatgram/backend/models/Typing.js
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
28 changes: 28 additions & 0 deletions chatgram/backend/models/User.js
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
Loading

0 comments on commit 9749f92

Please sign in to comment.