From 957b1023e96de43e8467e3e589a8501980169b02 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 3 May 2024 08:00:17 +0200 Subject: [PATCH] refactoring: separaate model & module --- models/forgefed/nodeinfo.go | 13 ------------ {models => modules}/forgefed/activity.go | 0 {models => modules}/forgefed/activity_test.go | 0 {models => modules}/forgefed/actor.go | 0 {models => modules}/forgefed/actor_test.go | 0 {models => modules}/forgefed/forgefed.go | 0 modules/forgefed/nodeinfo.go | 19 +++++++++++++++++ {models => modules}/forgefed/repository.go | 0 .../forgefed/repository_test.go | 0 routers/api/v1/activitypub/repository.go | 4 ++-- routers/api/v1/activitypub/response.go | 2 +- routers/api/v1/api.go | 2 +- routers/api/v1/swagger/options.go | 2 +- services/federation/federation_service.go | 21 ++++++++++--------- templates/swagger/v1_json.tmpl | 2 +- 15 files changed, 36 insertions(+), 29 deletions(-) rename {models => modules}/forgefed/activity.go (100%) rename {models => modules}/forgefed/activity_test.go (100%) rename {models => modules}/forgefed/actor.go (100%) rename {models => modules}/forgefed/actor_test.go (100%) rename {models => modules}/forgefed/forgefed.go (100%) create mode 100644 modules/forgefed/nodeinfo.go rename {models => modules}/forgefed/repository.go (100%) rename {models => modules}/forgefed/repository_test.go (100%) diff --git a/models/forgefed/nodeinfo.go b/models/forgefed/nodeinfo.go index a1373c6829..292c658cc9 100644 --- a/models/forgefed/nodeinfo.go +++ b/models/forgefed/nodeinfo.go @@ -4,11 +4,9 @@ package forgefed import ( - "fmt" "net/url" "code.gitea.io/gitea/modules/validation" - "github.com/valyala/fastjson" ) @@ -82,17 +80,6 @@ func (node NodeInfoWellKnown) Validate() []string { return result } -func (id ActorID) AsWellKnownNodeInfoURI() string { - wellKnownPath := ".well-known/nodeinfo" - var result string - if id.Port == "" { - result = fmt.Sprintf("%s://%s/%s", id.Schema, id.Host, wellKnownPath) - } else { - result = fmt.Sprintf("%s://%s:%s/%s", id.Schema, id.Host, id.Port, wellKnownPath) - } - return result -} - // ------------------------------------------------ NodeInfo ------------------------------------------------ // NodeInfo data type diff --git a/models/forgefed/activity.go b/modules/forgefed/activity.go similarity index 100% rename from models/forgefed/activity.go rename to modules/forgefed/activity.go diff --git a/models/forgefed/activity_test.go b/modules/forgefed/activity_test.go similarity index 100% rename from models/forgefed/activity_test.go rename to modules/forgefed/activity_test.go diff --git a/models/forgefed/actor.go b/modules/forgefed/actor.go similarity index 100% rename from models/forgefed/actor.go rename to modules/forgefed/actor.go diff --git a/models/forgefed/actor_test.go b/modules/forgefed/actor_test.go similarity index 100% rename from models/forgefed/actor_test.go rename to modules/forgefed/actor_test.go diff --git a/models/forgefed/forgefed.go b/modules/forgefed/forgefed.go similarity index 100% rename from models/forgefed/forgefed.go rename to modules/forgefed/forgefed.go diff --git a/modules/forgefed/nodeinfo.go b/modules/forgefed/nodeinfo.go new file mode 100644 index 0000000000..b22d2959d4 --- /dev/null +++ b/modules/forgefed/nodeinfo.go @@ -0,0 +1,19 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "fmt" +) + +func (id ActorID) AsWellKnownNodeInfoURI() string { + wellKnownPath := ".well-known/nodeinfo" + var result string + if id.Port == "" { + result = fmt.Sprintf("%s://%s/%s", id.Schema, id.Host, wellKnownPath) + } else { + result = fmt.Sprintf("%s://%s:%s/%s", id.Schema, id.Host, id.Port, wellKnownPath) + } + return result +} diff --git a/models/forgefed/repository.go b/modules/forgefed/repository.go similarity index 100% rename from models/forgefed/repository.go rename to modules/forgefed/repository.go diff --git a/models/forgefed/repository_test.go b/modules/forgefed/repository_test.go similarity index 100% rename from models/forgefed/repository_test.go rename to modules/forgefed/repository_test.go diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 65d5fc9074..a9e94f289a 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -8,7 +8,7 @@ import ( "net/http" "strings" - forgefed_model "code.gitea.io/gitea/models/forgefed" + "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" @@ -36,7 +36,7 @@ func Repository(ctx *context.APIContext) { // "$ref": "#/responses/ActivityPub" link := fmt.Sprintf("%s/api/v1/activitypub/repository-id/%d", strings.TrimSuffix(setting.AppURL, "/"), ctx.Repo.Repository.ID) - repo := forgefed_model.RepositoryNew(ap.IRI(link)) + repo := forgefed.RepositoryNew(ap.IRI(link)) repo.Name = ap.NaturalLanguageValuesNew() err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name)) diff --git a/routers/api/v1/activitypub/response.go b/routers/api/v1/activitypub/response.go index 1eca027f30..a2a053b34a 100644 --- a/routers/api/v1/activitypub/response.go +++ b/routers/api/v1/activitypub/response.go @@ -6,8 +6,8 @@ package activitypub import ( "net/http" - "code.gitea.io/gitea/models/forgefed" "code.gitea.io/gitea/modules/activitypub" + "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/services/context" diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 4a1d1785eb..0911c868fd 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -73,7 +73,6 @@ import ( actions_model "code.gitea.io/gitea/models/actions" auth_model "code.gitea.io/gitea/models/auth" - "code.gitea.io/gitea/models/forgefed" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" @@ -81,6 +80,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 55effaab6f..e6f6f009ce 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -5,7 +5,7 @@ package swagger import ( - ffed "code.gitea.io/gitea/models/forgefed" + ffed "code.gitea.io/gitea/modules/forgefed" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/forms" ) diff --git a/services/federation/federation_service.go b/services/federation/federation_service.go index 3de5e8e1ba..68e9fbfde8 100644 --- a/services/federation/federation_service.go +++ b/services/federation/federation_service.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/auth/password" + fm "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/validation" @@ -31,7 +32,7 @@ import ( // Star the repo if it wasn't already stared // Do some mitigation against out of order attacks func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int, string, error) { - activity := form.(*forgefed.ForgeLike) + activity := form.(*fm.ForgeLike) if res, err := validation.IsValid(activity); !res { return http.StatusNotAcceptable, "Invalid activity", err } @@ -47,14 +48,14 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int if !activity.IsNewer(federationHost.LatestActivity) { return http.StatusNotAcceptable, "Activity out of order.", fmt.Errorf("Activity already processed") } - actorID, err := forgefed.NewPersonID(actorURI, string(federationHost.NodeInfo.SoftwareName)) + actorID, err := fm.NewPersonID(actorURI, string(federationHost.NodeInfo.SoftwareName)) if err != nil { return http.StatusNotAcceptable, "Invalid PersonID", err } log.Info("Actor accepted:%v", actorID) // parse objectID (repository) - objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType)) + objectID, err := fm.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType)) if err != nil { return http.StatusNotAcceptable, "Invalid objectId", err } @@ -96,7 +97,7 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int return 0, "", nil } -func CreateFederationHostFromAP(ctx context.Context, actorID forgefed.ActorID) (*forgefed.FederationHost, error) { +func CreateFederationHostFromAP(ctx context.Context, actorID fm.ActorID) (*forgefed.FederationHost, error) { actionsUser := user.NewActionsUser() client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") if err != nil { @@ -132,7 +133,7 @@ func CreateFederationHostFromAP(ctx context.Context, actorID forgefed.ActorID) ( func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) { // parse actorID (person) log.Info("Input was: %v", actorURI) - rawActorID, err := forgefed.NewActorID(actorURI) + rawActorID, err := fm.NewActorID(actorURI) if err != nil { return nil, err } @@ -150,7 +151,7 @@ func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.Fe return federationHost, nil } -func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federationHostID int64) (*user.User, *user.FederatedUser, error) { +func CreateUserFromAP(ctx context.Context, personID fm.PersonID, federationHostID int64) (*user.User, *user.FederatedUser, error) { // ToDo: Do we get a publicKeyId from server, repo or owner or repo? actionsUser := user.NewActionsUser() client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") @@ -163,7 +164,7 @@ func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federatio return nil, nil, err } - person := forgefed.ForgePerson{} + person := fm.ForgePerson{} err = person.UnmarshalJSON(body) if err != nil { return nil, nil, err @@ -222,7 +223,7 @@ func StoreFollowingRepoList(ctx context.Context, localRepoID int64, followingRep if err != nil { return http.StatusInternalServerError, "Wrong FederationHost", err } - followingRepoID, err := forgefed.NewRepositoryID(uri, string(federationHost.NodeInfo.SoftwareName)) + followingRepoID, err := fm.NewRepositoryID(uri, string(federationHost.NodeInfo.SoftwareName)) if err != nil { return http.StatusNotAcceptable, "Invalid federated repo", err } @@ -251,11 +252,11 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error return err } - likeActivityList := make([]forgefed.ForgeLike, 0) + likeActivityList := make([]fm.ForgeLike, 0) for _, followingRepo := range followingRepos { log.Info("Found following repo: %v", followingRepo) target := followingRepo.URI - likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) + likeActivity, err := fm.NewForgeLike(doer.APAPIURL(), target, time.Now()) if err != nil { return err } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index de293b2170..b8ef5668db 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -21389,7 +21389,7 @@ "ForgeLike": { "description": "ForgeLike activity data type", "type": "object", - "x-go-package": "code.gitea.io/gitea/models/forgefed" + "x-go-package": "code.gitea.io/gitea/modules/forgefed" }, "GPGKey": { "description": "GPGKey a user GPG key to sign commit and tag in repository",