From f2580b6de0532c720a5ff693194ca292324fe295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Thu, 26 Mar 2026 22:48:59 +0100 Subject: [PATCH] fix: nullable server id in activity log and fix preset logging --- local/controller/config_preset.go | 8 +++++++- local/model/activity_log.go | 2 +- local/service/config.go | 4 ++-- local/service/config_preset.go | 23 +++++++++++++++++------ local/service/leaderboard.go | 2 +- local/service/service_control.go | 6 +++--- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/local/controller/config_preset.go b/local/controller/config_preset.go index 7fb12e5..fb792b1 100644 --- a/local/controller/config_preset.go +++ b/local/controller/config_preset.go @@ -117,7 +117,13 @@ func (c *ConfigPresetController) CreatePreset(ctx *fiber.Ctx) error { return c.errorHandler.HandleParsingError(ctx, err) } - preset, err := c.service.CreatePreset(ctx.UserContext(), &req) + actorID, _ := ctx.Locals("userID").(string) + actorUsername, _ := ctx.Locals("actorUsername").(string) + if actorUsername == "" { + actorUsername = "system" + } + + preset, err := c.service.CreatePreset(ctx.UserContext(), &req, actorID, actorUsername) if err != nil { return c.errorHandler.HandleServiceError(ctx, err) } diff --git a/local/model/activity_log.go b/local/model/activity_log.go index e23523b..66c79e8 100644 --- a/local/model/activity_log.go +++ b/local/model/activity_log.go @@ -21,7 +21,7 @@ const ( type ActivityLog struct { ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;"` - ServerID uuid.UUID `json:"serverId" gorm:"not null;type:uuid;index"` + ServerID *uuid.UUID `json:"serverId" gorm:"type:uuid;index"` Server *Server `json:"server,omitempty" gorm:"foreignKey:ServerID"` UserID string `json:"userId" gorm:"not null"` Username string `json:"username" gorm:"not null"` diff --git a/local/service/config.go b/local/service/config.go index 95601b4..84ddeaa 100755 --- a/local/service/config.go +++ b/local/service/config.go @@ -160,7 +160,7 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati if details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData); details != "" { go func(details string) { _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverUUID, + ServerID: &serverUUID, UserID: actorID, Username: actorUsername, Action: model.ActionConfigUpdate, @@ -285,7 +285,7 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri if details := BuildConfigDetails(configFile, oldDataUTF8, newData); details != "" { go func() { _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverUUID, + ServerID: &serverUUID, UserID: actorID, Username: actorUsername, Action: model.ActionConfigUpdate, diff --git a/local/service/config_preset.go b/local/service/config_preset.go index be0f4c4..caada5d 100644 --- a/local/service/config_preset.go +++ b/local/service/config_preset.go @@ -12,9 +12,9 @@ import ( ) const ( - presetMaxNameLen = 100 - presetMaxDescLen = 500 - presetMaxSectionBytes = 64 * 1024 // 64 KB per config section + presetMaxNameLen = 100 + presetMaxDescLen = 500 + presetMaxSectionBytes = 64 * 1024 // 64 KB per config section ) type ConfigPresetService struct { @@ -46,7 +46,7 @@ func (s *ConfigPresetService) GetPreset(ctx context.Context, id uuid.UUID) (*mod return preset, nil } -func (s *ConfigPresetService) CreatePreset(ctx context.Context, req *model.ConfigPresetCreateRequest) (*model.ConfigPreset, error) { +func (s *ConfigPresetService) CreatePreset(ctx context.Context, req *model.ConfigPresetCreateRequest, actorID, actorUsername string) (*model.ConfigPreset, error) { if req.Name == "" { return nil, fmt.Errorf("preset name is required") } @@ -70,7 +70,18 @@ func (s *ConfigPresetService) CreatePreset(ctx context.Context, req *model.Confi return nil, err } - logging.Info("Preset created: id=%s name=%q", preset.ID, preset.Name) + details := fmt.Sprintf(`{"preset_id":%q,"preset_name":%q}`, preset.ID, preset.Name) + go func() { + if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ + UserID: actorID, + Username: actorUsername, + Action: model.ActionPresetCreate, + Details: details, + }); logErr != nil { + logging.Error("Failed to log preset create: %v", logErr) + } + }() + return preset, nil } @@ -154,7 +165,7 @@ func (s *ConfigPresetService) ApplyPreset(ctx context.Context, serverID string, details := fmt.Sprintf(`{"preset_id":%q,"preset_name":%q,"sections_applied":%d}`, presetID, preset.Name, len(results)) go func() { if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverUUID, + ServerID: &serverUUID, UserID: actorID, Username: actorUsername, Action: model.ActionPresetApply, diff --git a/local/service/leaderboard.go b/local/service/leaderboard.go index f43437d..c1407e0 100755 --- a/local/service/leaderboard.go +++ b/local/service/leaderboard.go @@ -45,7 +45,7 @@ func (s *LeaderboardService) Update(ctx context.Context, serverID uuid.UUID, lb details := fmt.Sprintf(`{"driver_count":%d,"race_count":%d}`, driverCount, raceCount) go func() { if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverID, + ServerID: &serverID, UserID: actorID, Username: actorUsername, Action: model.ActionLeaderboardUpdate, diff --git a/local/service/service_control.go b/local/service/service_control.go index 89ad683..06dc391 100755 --- a/local/service/service_control.go +++ b/local/service/service_control.go @@ -110,7 +110,7 @@ func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (stri actorID, actorUsername := extractActor(ctx) go func() { _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, + ServerID: &server.ID, UserID: actorID, Username: actorUsername, Action: model.ActionServerStart, @@ -147,7 +147,7 @@ func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (strin actorID, actorUsername := extractActor(ctx) go func() { _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, + ServerID: &server.ID, UserID: actorID, Username: actorUsername, Action: model.ActionServerStop, @@ -184,7 +184,7 @@ func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (st actorID, actorUsername := extractActor(ctx) go func() { _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, + ServerID: &server.ID, UserID: actorID, Username: actorUsername, Action: model.ActionServerRestart,