implement internal events

This commit is contained in:
srgooglo 2022-10-28 22:06:33 +00:00
parent 92879b0312
commit df17cd73af
3 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import user_connected from "./user_connected"
import user_disconnected from "./user_disconnected"
export default {
"user.connected": user_connected,
"user.disconnected": user_disconnected,
}

View File

@ -0,0 +1,19 @@
import { UserFollow } from "../models"
export default async (user_id) => {
// get followers of the user
const followers = await UserFollow.find({
to: user_id,
})
// send event to ws clients (if are connected)
followers.forEach((follow) => {
const connectedClient = global.wsInterface.clients.find((client) => {
return client.user_id === follow.user_id
})
if (connectedClient) {
connectedClient.socket.emit("friend.connected", user_id)
}
})
}

View File

@ -0,0 +1,19 @@
import { UserFollow } from "../models"
export default async (user_id) => {
// get followers of the user
const followers = await UserFollow.find({
to: user_id,
})
// send event to ws clients (if are connected)
followers.forEach((follow) => {
const connectedClient = global.wsInterface.clients.find((client) => {
return client.user_id === follow.user_id
})
if (connectedClient) {
connectedClient.socket.emit("friend.disconnected", user_id)
}
})
}