start work on federationinfo

This commit is contained in:
Michael Jerger 2024-01-12 14:33:52 +01:00
parent 12558d62c8
commit 8610d94af8
4 changed files with 58 additions and 1 deletions

View file

@ -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
}

View file

@ -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))
}

View file

@ -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
}

View file

@ -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 {