-
Notifications
You must be signed in to change notification settings - Fork 83
/
socketServer.js
124 lines (101 loc) · 3.6 KB
/
socketServer.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
let users = [];
let admins = [];
const SocketServer = (socket) => {
//#region //!Connection
socket.on("joinUser", (id) => {
users.push({ id, socketId: socket.id });
});
socket.on("joinAdmin", (id) => {
admins.push({ id, socketId: socket.id });
const admin = admins.find((admin) => admin.id === id);
let totalActiveUsers = users.length;
socket.to(`${admin.socketId}`).emit("activeUsers", totalActiveUsers);
});
socket.on("disconnect", () => {
users = users.filter((user) => user.socketId !== socket.id);
admins = admins.filter((user) => user.socketId !== socket.id);
});
//#endregion
//#region //!Like
socket.on("likePost", (newPost) => {
let ids = [...newPost.user.followers, newPost.user._id];
const clients = users.filter((user) => ids.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("likeToClient", newPost);
});
}
});
socket.on("unLikePost", (newPost) => {
let ids = [...newPost.user.followers, newPost.user._id];
const clients = users.filter((user) => ids.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("unLikeToClient", newPost);
});
}
});
//#endregion
//#region //!comment
socket.on("createComment", (newPost) => {
let ids = [...newPost.user.followers, newPost.user._id];
const clients = users.filter((user) => ids.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("createCommentToClient", newPost);
});
}
});
socket.on("deleteComment", (newPost) => {
let ids = [...newPost.user.followers, newPost.user._id];
const clients = users.filter((user) => ids.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("deleteCommentToClient", newPost);
});
}
});
//#endregion
//#region //!follow
socket.on("follow", (newUser) => {
const user = users.find((user) => user.id === newUser._id);
user && socket.to(`${user.socketId}`).emit("followToClient", newUser);
});
socket.on("unFollow", (newUser) => {
const user = users.find((user) => user.id === newUser._id);
user && socket.to(`${user.socketId}`).emit("unFollowToClient", newUser);
});
//#endregion
//#region //!Notifications
socket.on("createNotify", (msg) => {
const clients = users.filter((user) => msg.recipients.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("createNotifyToClient", msg);
});
}
});
socket.on("removeNotify", (msg) => {
const clients = users.filter((user) => msg.recipients.includes(user.id));
if (clients.length > 0) {
clients.forEach((client) => {
socket.to(`${client.socketId}`).emit("removeNotifyToClient", msg);
});
}
});
//#endregion
socket.on("getActiveUsers", (id) => {
const admin = admins.find((user) => user.id === id);
const totalActiveUsers = users.length;
socket
.to(`${admin.socketId}`)
.emit("getActiveUsersToClient", totalActiveUsers);
});
//#region //!Messages
socket.on("addMessage", (msg) => {
const user = users.find(user => user.id === msg.recipient);
user && socket.to(`${user.socketId}`).emit("addMessageToClient", msg);
});
//#endregion
}
module.exports = SocketServer;