user avatar
parent
f8022c6046
commit
6e8faf9c5a
|
@ -66,7 +66,7 @@ func LoadGroups(category string) {
|
||||||
|
|
||||||
if category != "" {
|
if category != "" {
|
||||||
socketclients.BroadcastMessage(structs.SendSocketMessage{
|
socketclients.BroadcastMessage(structs.SendSocketMessage{
|
||||||
Cmd: utils.SendCmdGroupTasksReloaded,
|
Cmd: utils.SentCmdGroupTasksReloaded,
|
||||||
Body: struct {
|
Body: struct {
|
||||||
Category string
|
Category string
|
||||||
UpdatedGroups []structs.Group
|
UpdatedGroups []structs.Group
|
||||||
|
|
|
@ -88,9 +88,18 @@ type InitUserSocketConnection struct {
|
||||||
CategoryGroups []CategoryGroup // config specific group tasks
|
CategoryGroups []CategoryGroup // config specific group tasks
|
||||||
GroupTasks []GroupTasks // in database saved group tasks
|
GroupTasks []GroupTasks // in database saved group tasks
|
||||||
GroupTasksSteps []GroupTaskSteps
|
GroupTasksSteps []GroupTaskSteps
|
||||||
|
AllUsers []AllUsers
|
||||||
|
}
|
||||||
|
|
||||||
|
type AllUsers struct {
|
||||||
|
Id string
|
||||||
|
Avatar string
|
||||||
|
Username string
|
||||||
|
ConnectionStatus uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserData struct {
|
type UserData struct {
|
||||||
|
Id string
|
||||||
Username string
|
Username string
|
||||||
Email string
|
Email string
|
||||||
Sessions []UserSessionSocket
|
Sessions []UserSessionSocket
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id string
|
Id string
|
||||||
|
Avatar string
|
||||||
Username string
|
Username string
|
||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
|
|
|
@ -13,6 +13,8 @@ const (
|
||||||
LenUserId = 36
|
LenUserId = 36
|
||||||
|
|
||||||
HeaderXAuthorization = "X-Authorization"
|
HeaderXAuthorization = "X-Authorization"
|
||||||
|
|
||||||
|
MaxAvatarSize = 5 * 1024 * 1024 // 5 MB
|
||||||
)
|
)
|
||||||
|
|
||||||
// commands sent to web clients
|
// commands sent to web clients
|
||||||
|
@ -24,8 +26,9 @@ const (
|
||||||
SentCmdUpdateGroupTaskStep = 5
|
SentCmdUpdateGroupTaskStep = 5
|
||||||
SentCmdUpdateGroupTask = 6
|
SentCmdUpdateGroupTask = 6
|
||||||
SentCmdReloadingGroupTasks = 7
|
SentCmdReloadingGroupTasks = 7
|
||||||
SendCmdGroupTasksReloaded = 8
|
SentCmdGroupTasksReloaded = 8
|
||||||
SendCmdUpdateUserSessions = 9
|
SentCmdUpdateUserSessions = 9
|
||||||
|
SentCmdUpdateAllUsersUserAvatar = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
// commands received from web clients
|
// commands received from web clients
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"janex/admin-dashboard-backend/modules/database"
|
||||||
|
"janex/admin-dashboard-backend/modules/structs"
|
||||||
|
"janex/admin-dashboard-backend/modules/utils"
|
||||||
|
"janex/admin-dashboard-backend/socketclients"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/savsgio/gotils/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UpdateAvatar(c *fiber.Ctx) error {
|
||||||
|
log.Info().Msg("update avatar")
|
||||||
|
|
||||||
|
fileHeader, err := c.FormFile("file")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Msgf("User Avatar file error: %v", err)
|
||||||
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fileHeader.Size > utils.MaxAvatarSize {
|
||||||
|
log.Error().Msg("User Avatar file size limit exceeded")
|
||||||
|
return c.SendStatus(fiber.StatusRequestEntityTooLarge)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msgf("userid", c.Locals("userId"))
|
||||||
|
|
||||||
|
user := structs.User{Id: c.Locals("userId").(string)}
|
||||||
|
|
||||||
|
database.DB.First(&user)
|
||||||
|
|
||||||
|
log.Debug().Msgf("Found user %s", user.Avatar)
|
||||||
|
|
||||||
|
if user.Avatar != "" {
|
||||||
|
os.Remove("./public/avatars/" + user.Avatar)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName := uuid.V4() + "." + strings.Split(fileHeader.Header["Content-Type"][0], "/")[1]
|
||||||
|
|
||||||
|
database.DB.Model(&structs.User{}).Where("id = ?", user.Id).Updates(&structs.User{Avatar: fileName})
|
||||||
|
|
||||||
|
socketclients.BroadcastMessage(structs.SendSocketMessage{
|
||||||
|
Cmd: utils.SentCmdUpdateAllUsersUserAvatar,
|
||||||
|
Body: struct {
|
||||||
|
UserId string
|
||||||
|
Avatar string
|
||||||
|
}{
|
||||||
|
UserId: user.Id,
|
||||||
|
Avatar: fileName,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return c.SaveFile(fileHeader, fmt.Sprintf("./public/avatars/%s", fileName))
|
||||||
|
}
|
|
@ -16,6 +16,9 @@ func SetupRoutes(app *fiber.App) {
|
||||||
u.Post("/auth/login", user.UserLogin)
|
u.Post("/auth/login", user.UserLogin)
|
||||||
u.Delete("/auth/logout", user.UserLogout)
|
u.Delete("/auth/logout", user.UserLogout)
|
||||||
u.Delete("/session/:idForDeletion", userSessionValidation, user.SignOutSession)
|
u.Delete("/session/:idForDeletion", userSessionValidation, user.SignOutSession)
|
||||||
|
u.Post("/avatar", userSessionValidation, user.UpdateAvatar)
|
||||||
|
|
||||||
|
app.Static("/", "./public")
|
||||||
}
|
}
|
||||||
|
|
||||||
func userSessionValidation(c *fiber.Ctx) error {
|
func userSessionValidation(c *fiber.Ctx) error {
|
||||||
|
@ -33,5 +36,7 @@ func userSessionValidation(c *fiber.Ctx) error {
|
||||||
return fiber.ErrUnauthorized
|
return fiber.ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Locals("userId", userSession.UserId)
|
||||||
|
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,18 @@ func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateConnectedUsers() {
|
func UpdateConnectedUsers(userId string) {
|
||||||
BroadcastMessage(structs.SendSocketMessage{
|
BroadcastMessage(structs.SendSocketMessage{
|
||||||
Cmd: utils.SentCmdUpdateConnectedUsers,
|
Cmd: utils.SentCmdUpdateConnectedUsers,
|
||||||
Body: len(cache.GetSocketClients()),
|
Body: struct {
|
||||||
|
WebSocketUsersCount int
|
||||||
|
UserId string
|
||||||
|
ConnectionStatus uint8
|
||||||
|
}{
|
||||||
|
WebSocketUsersCount: len(cache.GetSocketClients()),
|
||||||
|
UserId: userId,
|
||||||
|
ConnectionStatus: isUserGenerallyConnected(userId),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +72,7 @@ func UpdateUserSessionsForUser(userId string, ignoreUserSessionId string) {
|
||||||
GetUserSessions(userId)
|
GetUserSessions(userId)
|
||||||
|
|
||||||
SendMessageToUser(userId, ignoreUserSessionId, structs.SendSocketMessage{
|
SendMessageToUser(userId, ignoreUserSessionId, structs.SendSocketMessage{
|
||||||
Cmd: utils.SendCmdUpdateUserSessions,
|
Cmd: utils.SentCmdUpdateUserSessions,
|
||||||
Body: GetUserSessions(userId),
|
Body: GetUserSessions(userId),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -78,3 +86,34 @@ func isUserSessionConnected(userSessionId string, socketClients []*structs.Socke
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used to determine if a user is connected regardless of the session used
|
||||||
|
func isUserGenerallyConnected(userId string) uint8 {
|
||||||
|
for _, socketClient := range cache.GetSocketClients() {
|
||||||
|
if socketClient.UserId == userId {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all users from database.
|
||||||
|
// This is used in the UI to display all users.
|
||||||
|
func GetAllUsers() []structs.AllUsers {
|
||||||
|
var users []structs.User
|
||||||
|
var allUsers []structs.AllUsers
|
||||||
|
|
||||||
|
database.DB.Find(&users)
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
allUsers = append(allUsers, structs.AllUsers{
|
||||||
|
Id: user.Id,
|
||||||
|
Avatar: user.Avatar,
|
||||||
|
Username: user.Username,
|
||||||
|
ConnectionStatus: isUserGenerallyConnected(user.Id),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return allUsers
|
||||||
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@ func RunHub() {
|
||||||
Cmd: utils.SentCmdInitUserSocketConnection,
|
Cmd: utils.SentCmdInitUserSocketConnection,
|
||||||
Body: structs.InitUserSocketConnection{
|
Body: structs.InitUserSocketConnection{
|
||||||
User: structs.UserData{
|
User: structs.UserData{
|
||||||
|
Id: user.Id,
|
||||||
Username: user.Username,
|
Username: user.Username,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Sessions: socketclients.GetUserSessions(userId),
|
Sessions: socketclients.GetUserSessions(userId),
|
||||||
|
@ -56,10 +57,11 @@ func RunHub() {
|
||||||
CategoryGroups: cache.GetCategoryGroupsSorted(),
|
CategoryGroups: cache.GetCategoryGroupsSorted(),
|
||||||
GroupTasks: grouptasks.GetAllGroupTasks(),
|
GroupTasks: grouptasks.GetAllGroupTasks(),
|
||||||
GroupTasksSteps: grouptasks.GetAllGroupTasksSteps(),
|
GroupTasksSteps: grouptasks.GetAllGroupTasksSteps(),
|
||||||
|
AllUsers: socketclients.GetAllUsers(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
socketclients.UpdateConnectedUsers()
|
socketclients.UpdateConnectedUsers(userId)
|
||||||
socketclients.UpdateUserSessionsForUser(userId, sessionId)
|
socketclients.UpdateUserSessionsForUser(userId, sessionId)
|
||||||
|
|
||||||
case data := <-broadcast:
|
case data := <-broadcast:
|
||||||
|
@ -157,7 +159,7 @@ func RunHub() {
|
||||||
|
|
||||||
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
|
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
|
||||||
|
|
||||||
socketclients.UpdateConnectedUsers()
|
socketclients.UpdateConnectedUsers(connection.Locals("userId").(string))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue