From c58a995fb24bbffa5545e751c106b29f37efb6d6 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 19 Jan 2024 16:26:16 +0100 Subject: [PATCH] refactor FederationInfo -> FederationHost --- .../{federationinfo.go => federationhost.go} | 22 +++---- models/forgefed/federationhost_repository.go | 61 +++++++++++++++++++ ...ioninfo_test.go => federationhost_test.go} | 8 +-- models/forgefed/federationinfo_repository.go | 61 ------------------- routers/api/v1/activitypub/repository.go | 24 ++++---- 5 files changed, 88 insertions(+), 88 deletions(-) rename models/forgefed/{federationinfo.go => federationhost.go} (66%) create mode 100644 models/forgefed/federationhost_repository.go rename models/forgefed/{federationinfo_test.go => federationhost_test.go} (86%) delete mode 100644 models/forgefed/federationinfo_repository.go diff --git a/models/forgefed/federationinfo.go b/models/forgefed/federationhost.go similarity index 66% rename from models/forgefed/federationinfo.go rename to models/forgefed/federationhost.go index e85a005dfb..f820a1708b 100644 --- a/models/forgefed/federationinfo.go +++ b/models/forgefed/federationhost.go @@ -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 diff --git a/models/forgefed/federationhost_repository.go b/models/forgefed/federationhost_repository.go new file mode 100644 index 0000000000..fa9ab67c8c --- /dev/null +++ b/models/forgefed/federationhost_repository.go @@ -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 +} diff --git a/models/forgefed/federationinfo_test.go b/models/forgefed/federationhost_test.go similarity index 86% rename from models/forgefed/federationinfo_test.go rename to models/forgefed/federationhost_test.go index 4928890f8d..88eecf511a 100644 --- a/models/forgefed/federationinfo_test.go +++ b/models/forgefed/federationhost_test.go @@ -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", diff --git a/models/forgefed/federationinfo_repository.go b/models/forgefed/federationinfo_repository.go deleted file mode 100644 index 6db92c2981..0000000000 --- a/models/forgefed/federationinfo_repository.go +++ /dev/null @@ -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 -} diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 0935f4bec4..2f6fe52897 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -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 }