From 8610d94af8cb9015c5b93d382dd1afcaf201574f Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 12 Jan 2024 14:33:52 +0100 Subject: [PATCH] start work on federationinfo --- models/forgefed/federationinfo.go | 35 ++++++++++++++++++++ models/forgefed/federationinfo_repository.go | 12 +++++++ models/forgefed/nodeinfo.go | 2 ++ modules/validation/validateable.go | 10 +++++- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 models/forgefed/federationinfo.go create mode 100644 models/forgefed/federationinfo_repository.go diff --git a/models/forgefed/federationinfo.go b/models/forgefed/federationinfo.go new file mode 100644 index 0000000000..205eb161ea --- /dev/null +++ b/models/forgefed/federationinfo.go @@ -0,0 +1,35 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/validation" +) + +// FederationInfo data type +// swagger:model +type FederationInfo struct { + ID int64 `xorm:"pk autoincr"` + HostFqdn string `xorm:"INDEX VARCHAR(255) NOT NULL"` + NodeInfo NodeInfo `xorm:"NOT NULL"` + LatestActivity timeutil.TimeStamp `xorm:"NOT NULL"` + Create timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +// Validate collects error strings in a slice and returns this +func (info FederationInfo) Validate() []string { + var result []string + result = append(result, validation.ValidateNotEmpty(string(info.HostFqdn), "HostFqdn")...) + result = append(result, validation.ValidateMaxLen(string(info.HostFqdn), 255, "HostFqdn")...) + if (info.NodeInfo == NodeInfo{}) { + result = append(result, "Field NodeInfo may not be empty") + } else { + result = append(result, info.NodeInfo.Validate()...) + } + result = append(result, validation.ValidateNotEmpty(info.LatestActivity, "LatestActivity")...) + + return result +} diff --git a/models/forgefed/federationinfo_repository.go b/models/forgefed/federationinfo_repository.go new file mode 100644 index 0000000000..cf1e27fa9e --- /dev/null +++ b/models/forgefed/federationinfo_repository.go @@ -0,0 +1,12 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "code.gitea.io/gitea/models/db" +) + +func init() { + db.RegisterModel(new(FederationInfo)) +} diff --git a/models/forgefed/nodeinfo.go b/models/forgefed/nodeinfo.go index 0d721a9a24..cdf7a51f99 100644 --- a/models/forgefed/nodeinfo.go +++ b/models/forgefed/nodeinfo.go @@ -8,6 +8,7 @@ import ( "net/url" "code.gitea.io/gitea/modules/validation" + "github.com/valyala/fastjson" ) @@ -96,6 +97,7 @@ func (id ActorID) AsWellKnownNodeInfoUri() string { // NodeInfo data type // swagger:model type NodeInfo struct { + ID int64 `xorm:"pk autoincr"` Source SourceType } diff --git a/modules/validation/validateable.go b/modules/validation/validateable.go index d1537a7a2c..212e79352c 100644 --- a/modules/validation/validateable.go +++ b/modules/validation/validateable.go @@ -6,6 +6,7 @@ package validation import ( "fmt" "strings" + "unicode/utf8" ) type Validateable interface { @@ -21,13 +22,20 @@ func IsValid(v Validateable) (bool, error) { return true, nil } -func ValidateNotEmpty(value, fieldName string) []string { +func ValidateNotEmpty(value string, fieldName string) []string { if value == "" { return []string{fmt.Sprintf("Field %v may not be empty", fieldName)} } return []string{} } +func ValidateMaxLen(value string, maxLen int, fieldName string) []string { + if utf8.RuneCountInString(value) > maxLen { + return []string{fmt.Sprintf("Value in field %v was longer than %v", fieldName, maxLen)} + } + return []string{} +} + func ValidateOneOf(value any, allowed []any) []string { for _, allowedElem := range allowed { if value == allowedElem {