Passport/pkg/internal/services/connections.go

38 lines
800 B
Go
Raw Normal View History

package services
import (
"sync"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/gofiber/contrib/websocket"
)
var (
wsMutex sync.Mutex
wsConn = make(map[uint]map[*websocket.Conn]bool)
)
2024-03-31 08:03:59 +00:00
func ClientRegister(user models.Account, conn *websocket.Conn) {
wsMutex.Lock()
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[*websocket.Conn]bool)
}
wsConn[user.ID][conn] = true
wsMutex.Unlock()
}
func ClientUnregister(user models.Account, conn *websocket.Conn) {
wsMutex.Lock()
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[*websocket.Conn]bool)
}
delete(wsConn[user.ID], conn)
wsMutex.Unlock()
2024-06-26 12:28:12 +00:00
if status, err := GetStatus(user.ID); err != nil || !status.IsInvisible {
if len(wsConn[user.ID]) == 0 {
_ = SetAccountLastSeen(user.ID)
}
}
}