mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-29 04:36:18 +01:00
refactor FederationInfo -> FederationHost
This commit is contained in:
parent
e926ea16e2
commit
c58a995fb2
5 changed files with 88 additions and 88 deletions
|
@ -11,9 +11,9 @@ import (
|
|||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
// FederationInfo data type
|
||||
// FederationHost data type
|
||||
// swagger:model
|
||||
type FederationInfo struct {
|
||||
type FederationHost struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
// TODO: implement a toLower here & add a toLowerValidation
|
||||
HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"`
|
||||
|
@ -24,25 +24,25 @@ type FederationInfo struct {
|
|||
}
|
||||
|
||||
// Factory function for PersonID. Created struct is asserted to be valid
|
||||
func NewFederationInfo(nodeInfo NodeInfo, hostFqdn string) (FederationInfo, error) {
|
||||
result := FederationInfo{
|
||||
func NewFederationHost(nodeInfo NodeInfo, hostFqdn string) (FederationHost, error) {
|
||||
result := FederationHost{
|
||||
HostFqdn: hostFqdn,
|
||||
NodeInfo: nodeInfo,
|
||||
}
|
||||
if valid, err := validation.IsValid(result); !valid {
|
||||
return FederationInfo{}, err
|
||||
return FederationHost{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Validate collects error strings in a slice and returns this
|
||||
func (info FederationInfo) Validate() []string {
|
||||
func (host FederationHost) Validate() []string {
|
||||
var result []string
|
||||
result = append(result, validation.ValidateNotEmpty(info.HostFqdn, "HostFqdn")...)
|
||||
result = append(result, validation.ValidateMaxLen(info.HostFqdn, 255, "HostFqdn")...)
|
||||
result = append(result, info.NodeInfo.Validate()...)
|
||||
if !info.LatestActivity.IsZero() && info.LatestActivity.After(time.Now().Add(10*time.Minute)) {
|
||||
result = append(result, fmt.Sprintf("Latest Activity may not be far futurer: %v", info.LatestActivity))
|
||||
result = append(result, validation.ValidateNotEmpty(host.HostFqdn, "HostFqdn")...)
|
||||
result = append(result, validation.ValidateMaxLen(host.HostFqdn, 255, "HostFqdn")...)
|
||||
result = append(result, host.NodeInfo.Validate()...)
|
||||
if !host.LatestActivity.IsZero() && host.LatestActivity.After(time.Now().Add(10*time.Minute)) {
|
||||
result = append(result, fmt.Sprintf("Latest Activity may not be far futurer: %v", host.LatestActivity))
|
||||
}
|
||||
|
||||
return result
|
61
models/forgefed/federationhost_repository.go
Normal file
61
models/forgefed/federationhost_repository.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgefed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(FederationHost))
|
||||
}
|
||||
|
||||
func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
|
||||
host := new(FederationHost)
|
||||
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, fmt.Errorf("FederationInfo record %v does not exist", ID)
|
||||
}
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
|
||||
host := new(FederationHost)
|
||||
// TODO: use parameter with toLower
|
||||
has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, nil
|
||||
}
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func CreateFederationHost(ctx context.Context, host FederationHost) error {
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).Insert(host)
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateFederationHost(ctx context.Context, host FederationHost) error {
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).ID(host.ID).Update(host)
|
||||
return err
|
||||
}
|
|
@ -10,8 +10,8 @@ import (
|
|||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
func Test_FederationInfoValidation(t *testing.T) {
|
||||
sut := FederationInfo{
|
||||
func Test_FederationHostValidation(t *testing.T) {
|
||||
sut := FederationHost{
|
||||
HostFqdn: "host.do.main",
|
||||
NodeInfo: NodeInfo{
|
||||
Source: "forgejo",
|
||||
|
@ -22,7 +22,7 @@ func Test_FederationInfoValidation(t *testing.T) {
|
|||
t.Errorf("sut should be valid but was %q", err)
|
||||
}
|
||||
|
||||
sut = FederationInfo{
|
||||
sut = FederationHost{
|
||||
HostFqdn: "host.do.main",
|
||||
NodeInfo: NodeInfo{},
|
||||
LatestActivity: time.Now(),
|
||||
|
@ -31,7 +31,7 @@ func Test_FederationInfoValidation(t *testing.T) {
|
|||
t.Errorf("sut should be invalid")
|
||||
}
|
||||
|
||||
sut = FederationInfo{
|
||||
sut = FederationHost{
|
||||
HostFqdn: "host.do.main",
|
||||
NodeInfo: NodeInfo{
|
||||
Source: "forgejo",
|
|
@ -1,61 +0,0 @@
|
|||
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgefed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(FederationInfo))
|
||||
}
|
||||
|
||||
func GetFederationInfo(ctx context.Context, ID int64) (*FederationInfo, error) {
|
||||
info := new(FederationInfo)
|
||||
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, fmt.Errorf("FederationInfo record %v does not exist", ID)
|
||||
}
|
||||
if res, err := validation.IsValid(info); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func FindFederationInfoByHostFqdn(ctx context.Context, fqdn string) (*FederationInfo, error) {
|
||||
info := new(FederationInfo)
|
||||
// TODO: use parameter with toLower
|
||||
has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, nil
|
||||
}
|
||||
if res, err := validation.IsValid(info); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func CreateFederationInfo(ctx context.Context, info FederationInfo) error {
|
||||
if res, err := validation.IsValid(info); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).Insert(info)
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateFederationInfo(ctx context.Context, info FederationInfo) error {
|
||||
if res, err := validation.IsValid(info); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).ID(info.ID).Update(info)
|
||||
return err
|
||||
}
|
|
@ -96,7 +96,7 @@ func RepositoryInbox(ctx *context.APIContext) {
|
|||
"RepositoryInbox: Validating ActorID", err)
|
||||
return
|
||||
}
|
||||
federationInfo, err := forgefed.FindFederationInfoByHostFqdn(ctx, rawActorID.Host)
|
||||
federationInfo, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError,
|
||||
"RepositoryInbox: Error while loading FederationInfo", err)
|
||||
|
@ -180,7 +180,7 @@ func RepositoryInbox(ctx *context.APIContext) {
|
|||
}
|
||||
}
|
||||
federationInfo.LatestActivity = activity.StartTime
|
||||
err = forgefed.UpdateFederationInfo(ctx, *federationInfo)
|
||||
err = forgefed.UpdateFederationHost(ctx, *federationInfo)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo", err)
|
||||
return
|
||||
|
@ -211,35 +211,35 @@ func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
|
|||
return users, nil
|
||||
}
|
||||
|
||||
func createFederationInfo(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationInfo, error) {
|
||||
func createFederationInfo(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
|
||||
actionsUser := user_model.NewActionsUser()
|
||||
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
body, err := client.GetBody(actorID.AsWellKnownNodeInfoURI())
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
nodeInfoWellKnown, err := forgefed.NewNodeInfoWellKnown(body)
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
body, err = client.GetBody(nodeInfoWellKnown.Href)
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
nodeInfo, err := forgefed.NewNodeInfo(body)
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
result, err := forgefed.NewFederationInfo(nodeInfo, actorID.Host)
|
||||
result, err := forgefed.NewFederationHost(nodeInfo, actorID.Host)
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
err = forgefed.CreateFederationInfo(ctx, result)
|
||||
err = forgefed.CreateFederationHost(ctx, result)
|
||||
if err != nil {
|
||||
return forgefed.FederationInfo{}, err
|
||||
return forgefed.FederationHost{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue