submissions: normalize get from hash as list requests

This commit is contained in:
Quaternions 2024-12-18 15:22:23 -08:00
parent 516bd7a439
commit 621edbdbe0
6 changed files with 144 additions and 53 deletions

View File

@ -33,6 +33,7 @@ type Scripts interface {
Create(ctx context.Context, smap model.Script) (model.Script, error)
Update(ctx context.Context, id int64, values OptionalMap) error
Delete(ctx context.Context, id int64) error
List(ctx context.Context, filters OptionalMap, page model.Page) ([]model.Script, error)
}
type ScriptPolicy interface {

View File

@ -2,19 +2,27 @@ package model
import (
"fmt"
"strconv"
"time"
"github.com/dchest/siphash"
)
// compute the hash of a source code string
func HashSource(source string) uint64{
return siphash.Hash(0, 0, []byte(source))
}
// format a hash value as a hexidecimal string
func HashFormat(hash uint64) string{
return fmt.Sprintf("%016x", hash)
}
// parse a hexidecimal hash string
func HashParse(hash string) (uint64, error){
return strconv.ParseUint(hash, 16, 64)
}
type Script struct {
ID int64 `gorm:"primaryKey"`
Name string

View File

@ -2,7 +2,6 @@ package service
import (
"context"
"strconv"
"git.itzana.me/strafesnet/maps-service/pkg/api"
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
@ -56,7 +55,11 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptP
filter := datastore.Optional()
if params.FromScriptHash.IsSet(){
filter.AddNotNil("from_script_hash", params.FromScriptHash.Value)
hash, err := model.HashParse(params.FromScriptHash.Value)
if err != nil {
return nil, err
}
filter.AddNotNil("from_script_hash", hash)
}
if params.ToScriptID.IsSet(){
filter.AddNotNil("to_script_id", params.ToScriptID.Value)
@ -90,7 +93,7 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptP
//
// Delete the specified script policy by ID.
//
// DELETE /script-policy/id/{ScriptPolicyID}
// DELETE /script-policy/{ScriptPolicyID}
func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScriptPolicyParams) error {
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
if !ok {
@ -108,7 +111,7 @@ func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScr
//
// Get the specified script policy by ID.
//
// GET /script-policy/id/{ScriptPolicyID}
// GET /script-policy/{ScriptPolicyID}
func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPolicyParams) (*api.ScriptPolicy, error) {
_, ok := ctx.Value("UserInfo").(UserInfo)
if !ok {
@ -130,43 +133,11 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol
}, 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) {
_, ok := ctx.Value("UserInfo").(UserInfo)
if !ok {
return nil, ErrUserInfo
}
// Read permission for script policy only requires you to be logged in
// 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
}
// UpdateScriptPolicy implements updateScriptPolicy operation.
//
// Update the specified script policy by ID.
//
// PATCH /script-policy/id/{ScriptPolicyID}
// POST /script-policy/{ScriptPolicyID}
func (svc *Service) UpdateScriptPolicy(ctx context.Context, req *api.ScriptPolicyUpdate, params api.UpdateScriptPolicyParams) error {
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
if !ok {

View File

@ -39,6 +39,52 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a
}, nil
}
// ListScripts implements listScripts operation.
//
// Get list of scripts.
//
// GET /scripts
func (svc *Service) ListScripts(ctx context.Context, params api.ListScriptsParams) ([]api.Script, error) {
filter := datastore.Optional()
if params.Hash.IsSet(){
hash, err := model.HashParse(params.Hash.Value)
if err != nil {
return nil, err
}
filter.AddNotNil("hash", hash)
}
if params.Name.IsSet(){
filter.AddNotNil("name", params.Name.Value)
}
if params.Source.IsSet(){
filter.AddNotNil("source", params.Source.Value)
}
if params.SubmissionID.IsSet(){
filter.AddNotNil("submission_id", params.SubmissionID.Value)
}
items, err := svc.DB.Scripts().List(ctx, filter, model.Page{
Number: params.Page,
Size: params.Limit,
})
if err != nil {
return nil, err
}
var resp []api.Script
for i := 0; i < len(items); i++ {
resp = append(resp, api.Script{
ID: items[i].ID,
Hash: model.HashFormat(items[i].Hash),
Source: items[i].Source,
SubmissionID: items[i].SubmissionID,
})
}
return resp, nil
}
// DeleteScript implements deleteScript operation.
//
// Delete the specified script by ID.

View File

@ -2,8 +2,8 @@ package service_internal
import (
"context"
"strconv"
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
"git.itzana.me/strafesnet/maps-service/pkg/internal"
"git.itzana.me/strafesnet/maps-service/pkg/model"
)
@ -36,27 +36,45 @@ func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolic
}, nil
}
// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation.
// ListScriptPolicy implements listScriptPolicy operation.
//
// Get the policy for the given hash of script source code.
// Get list of script policies.
//
// 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)
// GET /script-policy
func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptPolicyParams) ([]api.ScriptPolicy, error) {
filter := datastore.Optional()
if params.FromScriptHash.IsSet(){
hash, err := model.HashParse(params.FromScriptHash.Value)
if err != nil {
return nil, err
}
filter.AddNotNil("from_script_hash", hash)
}
if params.ToScriptID.IsSet(){
filter.AddNotNil("to_script_id", params.ToScriptID.Value)
}
if params.Policy.IsSet(){
filter.AddNotNil("policy", params.Policy.Value)
}
items, err := svc.DB.ScriptPolicy().List(ctx, filter, model.Page{
Number: params.Page,
Size: params.Limit,
})
if err != nil {
return nil, err
}
policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash)
if err != nil {
return nil, err
var resp []api.ScriptPolicy
for i := 0; i < len(items); i++ {
resp = append(resp, api.ScriptPolicy{
ID: items[i].ID,
FromScriptHash: model.HashFormat(items[i].FromScriptHash),
ToScriptID: items[i].ToScriptID,
Policy: int32(items[i].Policy),
})
}
return &api.ScriptPolicy{
ID: policy.ID,
FromScriptHash: model.HashFormat(policy.FromScriptHash),
ToScriptID: policy.ToScriptID,
Policy: int32(policy.Policy),
}, nil
return resp, nil
}

View File

@ -3,6 +3,7 @@ package service_internal
import (
"context"
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
"git.itzana.me/strafesnet/maps-service/pkg/internal"
"git.itzana.me/strafesnet/maps-service/pkg/model"
)
@ -29,6 +30,52 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a
}, nil
}
// ListScripts implements listScripts operation.
//
// Get list of scripts.
//
// GET /scripts
func (svc *Service) ListScripts(ctx context.Context, params api.ListScriptsParams) ([]api.Script, error) {
filter := datastore.Optional()
if params.Hash.IsSet(){
hash, err := model.HashParse(params.Hash.Value)
if err != nil {
return nil, err
}
filter.AddNotNil("hash", hash)
}
if params.Name.IsSet(){
filter.AddNotNil("name", params.Name.Value)
}
if params.Source.IsSet(){
filter.AddNotNil("source", params.Source.Value)
}
if params.SubmissionID.IsSet(){
filter.AddNotNil("submission_id", params.SubmissionID.Value)
}
items, err := svc.DB.Scripts().List(ctx, filter, model.Page{
Number: params.Page,
Size: params.Limit,
})
if err != nil {
return nil, err
}
var resp []api.Script
for i := 0; i < len(items); i++ {
resp = append(resp, api.Script{
ID: items[i].ID,
Hash: model.HashFormat(items[i].Hash),
Source: items[i].Source,
SubmissionID: items[i].SubmissionID,
})
}
return resp, nil
}
// GetScript implements getScript operation.
//
// Get the specified script by ID.