From 7a7e158ec3bf747a31c5d1cb485cd66a0056d496 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 17 Dec 2024 18:22:53 -0800 Subject: [PATCH] submissions: legendary code duplication --- pkg/service_internal/script_policy.go | 62 +++++++++++++++++++++++++++ pkg/service_internal/scripts.go | 50 +++++++++++++++++++++ pkg/service_internal/submissions.go | 14 ++++++ 3 files changed, 126 insertions(+) create mode 100644 pkg/service_internal/script_policy.go create mode 100644 pkg/service_internal/scripts.go diff --git a/pkg/service_internal/script_policy.go b/pkg/service_internal/script_policy.go new file mode 100644 index 0000000..925026d --- /dev/null +++ b/pkg/service_internal/script_policy.go @@ -0,0 +1,62 @@ +package service_internal + +import ( + "context" + "strconv" + + "git.itzana.me/strafesnet/maps-service/pkg/internal" + "git.itzana.me/strafesnet/maps-service/pkg/model" +) + +// CreateScriptPolicy implements createScriptPolicy operation. +// +// Create a new script policy. +// +// POST /script-policy +func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolicyCreate) (*api.ID, error) { + from_script, err := svc.DB.Scripts().Get(ctx, req.FromScriptID) + if err != nil { + return nil, err + } + + // the existence of ToScriptID does not need to be validated because it's checked by a foreign key constraint. + + script, err := svc.DB.ScriptPolicy().Create(ctx, model.ScriptPolicy{ + ID: 0, + FromScriptHash: from_script.Hash, + ToScriptID: req.ToScriptID, + Policy: model.Policy(req.Policy), + }) + if err != nil { + return nil, err + } + + return &api.ID{ + ID: script.ID, + }, nil +} + +// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation. +// +// Get the policy for the given hash of script source code. +// +// GET /script-policy/hash/{FromScriptHash} +func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetScriptPolicyFromHashParams) (*api.ScriptPolicy, error) { + // parse hash from hex + hash, err := strconv.ParseUint(params.FromScriptHash, 16, 64) + if err != nil { + return nil, err + } + + policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash) + if err != nil { + return nil, err + } + + return &api.ScriptPolicy{ + ID: policy.ID, + FromScriptHash: model.HashFormat(policy.FromScriptHash), + ToScriptID: policy.ToScriptID, + Policy: int32(policy.Policy), + }, nil +} diff --git a/pkg/service_internal/scripts.go b/pkg/service_internal/scripts.go new file mode 100644 index 0000000..497bf1c --- /dev/null +++ b/pkg/service_internal/scripts.go @@ -0,0 +1,50 @@ +package service_internal + +import ( + "context" + + "git.itzana.me/strafesnet/maps-service/pkg/internal" + "git.itzana.me/strafesnet/maps-service/pkg/model" +) + +// CreateScript implements createScript operation. +// +// Create a new script. +// +// POST /scripts +func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*api.ID, error) { + script, err := svc.DB.Scripts().Create(ctx, model.Script{ + ID: 0, + Name: req.Name, + Hash: model.HashSource(req.Source), + Source: req.Source, + SubmissionID: req.SubmissionID.Or(0), + }) + if err != nil { + return nil, err + } + + return &api.ID{ + ID: script.ID, + }, nil +} + +// GetScript implements getScript operation. +// +// Get the specified script by ID. +// +// GET /scripts/{ScriptID} +func (svc *Service) GetScript(ctx context.Context, params api.GetScriptParams) (*api.Script, error) { + script, err := svc.DB.Scripts().Get(ctx, params.ScriptID) + if err != nil { + return nil, err + } + + return &api.Script{ + ID: script.ID, + Name: script.Name, + Hash: model.HashFormat(script.Hash), + Source: script.Source, + SubmissionID: script.SubmissionID, + }, nil +} diff --git a/pkg/service_internal/submissions.go b/pkg/service_internal/submissions.go index 0dc12c8..491939d 100644 --- a/pkg/service_internal/submissions.go +++ b/pkg/service_internal/submissions.go @@ -22,6 +22,20 @@ func (svc *Service) ActionSubmissionValidated(ctx context.Context, params intern return svc.DB.Submissions().IfStatusThenUpdate(ctx, params.SubmissionID, []model.Status{model.StatusValidating}, smap) } +// ActionSubmissionAccepted implements actionSubmissionAccepted operation. +// +// (Internal endpoint) Role Validator changes status from Validating -> Accepted. +// +// POST /submissions/{SubmissionID}/status/validator-failed +func (svc *Service) ActionSubmissionAccepted(ctx context.Context, params internal.ActionSubmissionAcceptedParams) error { + println("[ActionSubmissionAccepted] Implicit Validator permission granted!") + + // transaction + smap := datastore.Optional() + smap.Add("status_id", model.StatusAccepted) + return svc.DB.Submissions().IfStatusThenUpdate(ctx, params.SubmissionID, []model.Status{model.StatusValidating}, smap) +} + // ActionSubmissionReleased implements actionSubmissionReleased operation. // // (Internal endpoint) Role Releaser changes status from Uploaded -> Released.