user sessions
parent
f252c76c46
commit
704ae06edd
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/websocket/v2"
|
"github.com/gofiber/websocket/v2"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -87,4 +88,12 @@ type InitUserSocketConnection struct {
|
||||||
type UserData struct {
|
type UserData struct {
|
||||||
Username string
|
Username string
|
||||||
Email string
|
Email string
|
||||||
|
Sessions []UserSessionSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSessionSocket struct {
|
||||||
|
UserAgent string
|
||||||
|
ConnectionStatus uint8
|
||||||
|
LastUsed time.Time
|
||||||
|
ExpiresAt time.Time
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ type UserSession struct {
|
||||||
Id string
|
Id string
|
||||||
UserId string
|
UserId string
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
LastUsed time.Time
|
||||||
ExpiresAt time.Time
|
ExpiresAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ const (
|
||||||
SentCmdUpdateGroupTask = 6
|
SentCmdUpdateGroupTask = 6
|
||||||
SentCmdReloadingGroupTasks = 7
|
SentCmdReloadingGroupTasks = 7
|
||||||
SendCmdGroupTasksReloaded = 8
|
SendCmdGroupTasksReloaded = 8
|
||||||
|
SendCmdUpdateUserSessions = 9
|
||||||
)
|
)
|
||||||
|
|
||||||
// commands received from web clients
|
// commands received from web clients
|
||||||
|
|
|
@ -2,6 +2,7 @@ package socketclients
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"janex/admin-dashboard-backend/modules/cache"
|
"janex/admin-dashboard-backend/modules/cache"
|
||||||
|
"janex/admin-dashboard-backend/modules/database"
|
||||||
"janex/admin-dashboard-backend/modules/structs"
|
"janex/admin-dashboard-backend/modules/structs"
|
||||||
"janex/admin-dashboard-backend/modules/utils"
|
"janex/admin-dashboard-backend/modules/utils"
|
||||||
)
|
)
|
||||||
|
@ -18,3 +19,51 @@ func UpdateConnectedUsers() {
|
||||||
Body: len(cache.GetSocketClients()),
|
Body: len(cache.GetSocketClients()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendMessageToUser(userId string, ignoreUserSessionId string, sendSocketMessage structs.SendSocketMessage) {
|
||||||
|
for _, client := range cache.GetSocketClients() {
|
||||||
|
if client.UserId == userId && client.SessionId != ignoreUserSessionId {
|
||||||
|
client.SendMessage(sendSocketMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserSessions(userId string) []structs.UserSessionSocket {
|
||||||
|
var userSessions []structs.UserSession
|
||||||
|
|
||||||
|
database.DB.Where("user_id = ?", userId).Find(&userSessions)
|
||||||
|
|
||||||
|
var userSessionsSocket []structs.UserSessionSocket
|
||||||
|
|
||||||
|
socketClients := cache.GetSocketClients()
|
||||||
|
|
||||||
|
for _, userSession := range userSessions {
|
||||||
|
userSessionsSocket = append(userSessionsSocket, structs.UserSessionSocket{
|
||||||
|
UserAgent: userSession.UserAgent,
|
||||||
|
ConnectionStatus: isUserSessionConnected(userSession.Id, socketClients),
|
||||||
|
LastUsed: userSession.LastUsed,
|
||||||
|
ExpiresAt: userSession.ExpiresAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return userSessionsSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateUserSessionsForUser(userId string, ignoreUserSessionId string) {
|
||||||
|
GetUserSessions(userId)
|
||||||
|
|
||||||
|
SendMessageToUser(userId, ignoreUserSessionId, structs.SendSocketMessage{
|
||||||
|
Cmd: utils.SendCmdUpdateUserSessions,
|
||||||
|
Body: GetUserSessions(userId),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isUserSessionConnected(userSessionId string, socketClients []*structs.SocketClient) uint8 {
|
||||||
|
for _, socketClient := range socketClients {
|
||||||
|
if socketClient.SessionId == userSessionId {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
|
@ -51,6 +51,7 @@ func RunHub() {
|
||||||
User: structs.UserData{
|
User: structs.UserData{
|
||||||
Username: user.Username,
|
Username: user.Username,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
|
Sessions: socketclients.GetUserSessions(userId),
|
||||||
},
|
},
|
||||||
CategoryGroups: cache.GetCategoryGroupsSorted(),
|
CategoryGroups: cache.GetCategoryGroupsSorted(),
|
||||||
GroupTasks: grouptasks.GetAllGroupTasks(),
|
GroupTasks: grouptasks.GetAllGroupTasks(),
|
||||||
|
@ -59,6 +60,7 @@ func RunHub() {
|
||||||
})
|
})
|
||||||
|
|
||||||
socketclients.UpdateConnectedUsers()
|
socketclients.UpdateConnectedUsers()
|
||||||
|
socketclients.UpdateUserSessionsForUser(userId, sessionId)
|
||||||
|
|
||||||
case data := <-broadcast:
|
case data := <-broadcast:
|
||||||
var receivedMessage structs.ReceivedMessage
|
var receivedMessage structs.ReceivedMessage
|
||||||
|
@ -77,20 +79,6 @@ func RunHub() {
|
||||||
category := receivedMessage.Body["category"].(string)
|
category := receivedMessage.Body["category"].(string)
|
||||||
groupId := receivedMessage.Body["id"].(string)
|
groupId := receivedMessage.Body["id"].(string)
|
||||||
|
|
||||||
/*
|
|
||||||
var globalInputsJsonString string
|
|
||||||
|
|
||||||
if globalInputs != nil {
|
|
||||||
jsonString, err := json.Marshal(globalInputs)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Msgf("Failed to marshal global inputs %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
globalInputsJsonString = string(jsonString)
|
|
||||||
} */
|
|
||||||
|
|
||||||
globalInputsJsonString := utils.MarshalJson(receivedMessage.Body["globalInputs"])
|
globalInputsJsonString := utils.MarshalJson(receivedMessage.Body["globalInputs"])
|
||||||
|
|
||||||
groupTaskId := uuid.New().String()
|
groupTaskId := uuid.New().String()
|
||||||
|
@ -136,19 +124,6 @@ func RunHub() {
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
case utils.ReceivedCmdTaskContinueTaskStep:
|
case utils.ReceivedCmdTaskContinueTaskStep:
|
||||||
/*
|
|
||||||
|
|
||||||
if taskInputs != nil {
|
|
||||||
jsonString, err := json.Marshal(taskInputs)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Msgf("Failed to marshal task inputs %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
taskInputsJsonString = string(jsonString)
|
|
||||||
} */
|
|
||||||
|
|
||||||
go grouptasks.RunGroupTask(grouptasks.RunGroupTaskArgs{
|
go grouptasks.RunGroupTask(grouptasks.RunGroupTaskArgs{
|
||||||
StartType: grouptasks.RunGroupTaskStartTypeTryAgain,
|
StartType: grouptasks.RunGroupTaskStartTypeTryAgain,
|
||||||
GroupTaskId: receivedMessage.Body["groupTaskId"].(string),
|
GroupTaskId: receivedMessage.Body["groupTaskId"].(string),
|
||||||
|
@ -180,6 +155,8 @@ func RunHub() {
|
||||||
case connection := <-unregister:
|
case connection := <-unregister:
|
||||||
cache.DeleteClientByConn(connection)
|
cache.DeleteClientByConn(connection)
|
||||||
|
|
||||||
|
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
|
||||||
|
|
||||||
socketclients.UpdateConnectedUsers()
|
socketclients.UpdateConnectedUsers()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue