-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketServer.js
116 lines (91 loc) · 3.48 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
let users = []
const EditData = (data, id, call) => {
const newData = data.map(item =>
item.id === id ? {...item, call} : item
)
return newData;
}
const SocketServer = (socket) => {
// Connect - Disconnect
socket.on('joinUser', user => {
users.push({id: user._id, socketId: socket.id, followers: user.followers})
})
socket.on('disconnect', () => {
const data = users.find(user => user.socketId === socket.id)
if(data){
const clients = users.filter(user =>
data.followers.find(item => item._id === user.id)
)
if(clients.length > 0){
clients.forEach(client => {
socket.to(`${client.socketId}`).emit('CheckUserOffline', data.id)
})
}
if(data.call){
const callUser = users.find(user => user.id === data.call)
if(callUser){
users = EditData(users, callUser.id, null)
socket.to(`${callUser.socketId}`).emit('callerDisconnect')
}
}
}
users = users.filter(user => user.socketId !== socket.id)
})
// Likes
socket.on('likePost', newPost => {
const 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 => {
const 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)
})
}
})
// Comments
socket.on('createComment', newPost => {
const 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 => {
const 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)
})
}
})
// 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)
})
// Notification
socket.on('createNotify', msg => {
const client = users.find(user => msg.recipients.includes(user.id))
client && socket.to(`${client.socketId}`).emit('createNotifyToClient', msg)
})
socket.on('removeNotify', msg => {
const client = users.find(user => msg.recipients.includes(user.id))
client && socket.to(`${client.socketId}`).emit('removeNotifyToClient', msg)
})
}
module.exports = SocketServer