fix: nullable server id in activity log and fix preset logging

This commit is contained in:
2026-03-26 22:48:59 +01:00
parent f23f276ee5
commit f2580b6de0
6 changed files with 31 additions and 14 deletions

View File

@@ -117,7 +117,13 @@ func (c *ConfigPresetController) CreatePreset(ctx *fiber.Ctx) error {
return c.errorHandler.HandleParsingError(ctx, err) 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 { if err != nil {
return c.errorHandler.HandleServiceError(ctx, err) return c.errorHandler.HandleServiceError(ctx, err)
} }

View File

@@ -21,7 +21,7 @@ const (
type ActivityLog struct { type ActivityLog struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;"` 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"` Server *Server `json:"server,omitempty" gorm:"foreignKey:ServerID"`
UserID string `json:"userId" gorm:"not null"` UserID string `json:"userId" gorm:"not null"`
Username string `json:"username" gorm:"not null"` Username string `json:"username" gorm:"not null"`

View File

@@ -160,7 +160,7 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati
if details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData); details != "" { if details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData); details != "" {
go func(details string) { go func(details string) {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: serverUUID, ServerID: &serverUUID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionConfigUpdate, Action: model.ActionConfigUpdate,
@@ -285,7 +285,7 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri
if details := BuildConfigDetails(configFile, oldDataUTF8, newData); details != "" { if details := BuildConfigDetails(configFile, oldDataUTF8, newData); details != "" {
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: serverUUID, ServerID: &serverUUID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionConfigUpdate, Action: model.ActionConfigUpdate,

View File

@@ -12,9 +12,9 @@ import (
) )
const ( const (
presetMaxNameLen = 100 presetMaxNameLen = 100
presetMaxDescLen = 500 presetMaxDescLen = 500
presetMaxSectionBytes = 64 * 1024 // 64 KB per config section presetMaxSectionBytes = 64 * 1024 // 64 KB per config section
) )
type ConfigPresetService struct { type ConfigPresetService struct {
@@ -46,7 +46,7 @@ func (s *ConfigPresetService) GetPreset(ctx context.Context, id uuid.UUID) (*mod
return preset, nil 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 == "" { if req.Name == "" {
return nil, fmt.Errorf("preset name is required") 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 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 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)) details := fmt.Sprintf(`{"preset_id":%q,"preset_name":%q,"sections_applied":%d}`, presetID, preset.Name, len(results))
go func() { go func() {
if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: serverUUID, ServerID: &serverUUID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionPresetApply, Action: model.ActionPresetApply,

View File

@@ -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) details := fmt.Sprintf(`{"driver_count":%d,"race_count":%d}`, driverCount, raceCount)
go func() { go func() {
if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: serverID, ServerID: &serverID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionLeaderboardUpdate, Action: model.ActionLeaderboardUpdate,

View File

@@ -110,7 +110,7 @@ func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (stri
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: server.ID, ServerID: &server.ID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionServerStart, Action: model.ActionServerStart,
@@ -147,7 +147,7 @@ func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (strin
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: server.ID, ServerID: &server.ID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionServerStop, Action: model.ActionServerStop,
@@ -184,7 +184,7 @@ func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (st
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
ServerID: server.ID, ServerID: &server.ID,
UserID: actorID, UserID: actorID,
Username: actorUsername, Username: actorUsername,
Action: model.ActionServerRestart, Action: model.ActionServerRestart,