-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
167 lines (149 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require("dotenv").config();
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const morgan = require("morgan");
const PORT = process.env.PORT || 3002;
const DATABASE = process.env.DATABASE;
const expressError = require("./utils/expressError");
const Routes = require("./routes");
const helmet = require("helmet");
const sanitize = require("express-mongo-sanitize");
const http = require("http");
const server = http.createServer(app);
const socket = require("socket.io");
const Message = require("./models/message");
const cors = require("cors");
app.use(cors({ origin: "https://practical-carson-947785.netlify.app" }));
app.use(helmet());
app.use(sanitize());
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const io = socket(server, {
cors: { origin: "https://practical-carson-947785.netlify.app" },
});
let users = {};
io.on("connection", (socket) => {
socket.on("user connected", (userId) => {
if (users[userId]) {
socket.emit(
"error",
"UralShop Chat probably opened in another tab. You are disconnected now."
);
socket.disconnect();
delete users[userId];
}
users[userId] = { roomId: null, socketId: socket.id };
io.emit("user data", users);
socket.on("join room", (roomId) => {
users[userId] = { roomId };
socket.join(roomId);
io.emit("user data", users);
io.to(roomId).emit("get chat messages");
socket.on("message seen", () => {
Message.updateMany(
{
$and: [{ chatroom: roomId }, { receiver: userId }],
},
{ seen: true }
).then(() => {
io.to(roomId).emit("get chat messages");
});
});
socket.on("start typing", () => {
socket.broadcast.to(roomId).emit("typing", true);
});
socket.on("stop typing", () => {
socket.broadcast.to(roomId).emit("typing", false);
});
socket.on("send message", async (message) => {
const { sender, receiver, chatroom, body, isPhoto } = message;
let countRoomMember = 0;
for (const [key, value] of Object.entries(users)) {
const { roomId } = value;
if (roomId === chatroom) {
countRoomMember++;
}
}
if (!isPhoto) {
const messageObject = new Message({
sender,
receiver,
chatroom,
body,
seen: countRoomMember > 1 ? true : false,
});
messageObject.save((err, msg) => {
if (err) {
socket
.to(roomId)
.emit("error", "An error occured while sending the message.");
} else {
io.to(roomId).emit("message sent", msg);
if (countRoomMember === 1) {
socket.broadcast
.to(users[receiver])
.emit("message", "for your eyes only");
}
}
});
} else {
//const getPhotoMessage = await Message.findById(message._id)
const updatedMessage = await Message.findByIdAndUpdate(
message._id,
{
seen: countRoomMember > 1 ? true : false,
},
{ new: true }
);
io.to(roomId).emit("message sent", updatedMessage);
}
});
});
socket.on("left room", (roomId) => {
users[userId] = { roomId: null };
socket.leave(roomId);
delete users[userId];
socket.broadcast.to(roomId).emit("typing", false);
socket.disconnect();
io.emit("user data", users);
});
socket.on("disconnect", () => {
delete users[userId];
io.emit("user data", users);
});
});
});
mongoose
.connect(DATABASE, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: true,
})
.then(() => console.log("Connected To Database."))
.catch((err) => console.log("Could Not Connect To Database." + err));
app.use("/api/user", Routes.UserRoutes);
app.use("/api/shop", Routes.ShopRoutes);
app.use("/api/cart", Routes.CartRoutes);
app.use("/api/product", Routes.ProductRoutes);
app.use("/api/review", Routes.ReviewRoutes);
app.use("/api/wishlist", Routes.WishlistRoutes);
app.use("/api/chat", Routes.ChatRoutes);
app.use("/api/address", Routes.AddressRoutes);
app.use("/api/stripe", Routes.StripeRoutes);
app.use("/api/order", Routes.OrderRoutes);
app.use("/api/seller_rating", Routes.SellerRatingRoutes);
app.get("/", (req, res) => {
res.send("Güney Ural");
});
app.all("*", (req, res, next) => {
next(new expressError("Could Not Find Route.", 404));
});
app.use((err, req, res, next) => {
const { statusCode = 500 } = err;
console.log(err);
if (!err.message) err.message = "Something went wrong";
res.status(statusCode).json({ statusCode, errorMessage: err.message });
});
server.listen(PORT, () => console.log(`Server on port ${PORT}`));