-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
175 lines (172 loc) · 6.12 KB
/
app.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
168
169
170
171
172
173
174
175
const express = require("express");
const cors = require("cors");
const bodyparser = require("body-parser");
require("dotenv").config();
const connectDB = require("./db/connect");
const authRoutes = require("./routes/authRoutes");
const chatRoutes = require("./routes/chatRoutes");
const messageRoutes = require("./routes/messageRoutes");
const userRoutes = require("./routes/userRoutes");
const { ROUTE_PATHS, SOCKET_EVENTS } = require("./utils/constants");
const { authenticateUser } = require("./controllers/utils");
const socketIO = require("socket.io");
const jwtDecode = require("jwt-decode");
const MessageStore = require("./models/messageStore");
const Chat = require("./models/chat");
const Message = require("./models/message");
const User = require("./models/user");
const app = express();
app.use(cors());
app.use(bodyparser.urlencoded({ extended: false }));
app.use(express.json());
// routes
app.use(ROUTE_PATHS.AUTH, authRoutes);
app.use(ROUTE_PATHS.CHAT, authenticateUser, chatRoutes);
app.use(ROUTE_PATHS.MESSAGE, authenticateUser, messageRoutes);
app.use(ROUTE_PATHS.USER, authenticateUser, userRoutes);
const port = process.env.PORT || 5000;
let server;
const start = async () => {
try {
await connectDB(process.env.MONGO_URI);
server = app.listen(port, () =>
console.log(`Server is listening on port ${port}...`)
);
const io = socketIO(server, {
cors: {
origin: "*",
},
});
io.on(SOCKET_EVENTS.CONNECTION, (socket) => {
console.log("wow we are connected");
socket.on(SOCKET_EVENTS.SEND_MESSAGE, async (arg) => {
const { chatID, text, authToken } = arg;
if (!chatID || !text || !authToken) return;
try {
const user = await jwtDecode(authToken);
const chat = await Chat.findOne({
_id: chatID,
});
const messageStore = await MessageStore.findOne({
_id: chat?.messageStoreID,
});
const recieverIDs = chat?.members.filter(
(member) => member !== user?.username
);
const message = new Message({
text,
senderID: user?._id,
recieverIDs,
chatID,
});
await message.save();
messageStore?.messages.push(message?._id);
await messageStore.save();
chat.latestMessage = message.text;
chat.lastUpdatedTime = Date.now();
await chat.save();
// Marking this message as unread for members of group
await (async () => {
console.log(chat.members);
for (let i = 0; i < chat.members.length; i++) {
const reciever = await User.findOne({
username: chat.members[i],
});
if (reciever._id.toString() !== user._id) {
console.log(reciever);
const chatListOfReciever = reciever.chatList;
for (let j = 0; j < chatListOfReciever.length; j++) {
if (chatListOfReciever[j].id.toString() === chatID) {
chatListOfReciever[j] = {
...chatListOfReciever[j],
unreadMessageCount:
chatListOfReciever[j].unreadMessageCount + 1,
};
}
await reciever.save();
}
}
}
})();
// for reciever
socket.broadcast.emit(SOCKET_EVENTS.RECIEVE_MESSAGE, {
message,
sender: {
name: user?.name,
username: user?.username,
},
});
// for sender
socket.emit(SOCKET_EVENTS.RECIEVE_MESSAGE, {
message,
sender: {
name: user?.name,
username: user?.username,
},
});
} catch (e) {
console.log("Something went wrong", e);
}
});
socket.on(SOCKET_EVENTS.MARK_AS_READ, async (arg) => {
const { chatID, authToken } = arg;
if (!chatID || !authToken) return;
try {
const userDecoded = await jwtDecode(authToken);
console.log(userDecoded);
const user = await User.findOne({
username: userDecoded.username,
});
const chatList = user.chatList;
for (let i = 0; i < chatList.length; i++) {
if (chatList[i].id.toString() === chatID) {
chatList[i] = { ...chatList[i], unreadMessageCount: 0 };
await user.save();
}
}
const response = {
personalChatMap: [],
chatList: [],
};
const chatListIDs = user.chatList.map((chat) => chat.id.toString());
response.chatList = await Chat.find({
_id: { $in: [...chatListIDs] },
});
for (let i = 0; i < response.chatList.length; i++) {
const { unreadMessageCount } = user.chatList.find(
(val) => val.id.toString() === response.chatList[i]._id.toString()
);
let imageUrl = response.chatList[i].group_profile_pic;
if (!response.chatList[i].isGroup) {
const members = response.chatList[i].members;
if (members[0] === user.username) {
const member = await User.findOne({ username: members[1] });
imageUrl = member.profile_img;
} else {
const member = await User.findOne({ username: members[0] });
imageUrl = member.profile_img;
}
}
response.chatList[i] = {
...response.chatList[i].toObject(),
group_profile_pic: imageUrl,
unreadMessageCount,
};
}
response.chatList.sort(
(a, b) => new Date(b.lastUpdatedTime) - new Date(a.lastUpdatedTime)
);
response.personalChatMap = [...user.personalChatMap];
socket.emit(SOCKET_EVENTS.GET_CHAT_LIST, {
...response,
});
} catch (e) {
console.log(e);
}
});
});
} catch (error) {
console.log(error);
}
};
start();