Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 05:06:11 +01:00

refactoring: separaate model & module

This commit is contained in:
Michael Jerger 2024-05-03 08:00:17 +02:00
parent 42837f5dab
commit 957b1023e9
15 changed files with 36 additions and 29 deletions

View file

@ -4,11 +4,9 @@
package forgefed package forgefed
import ( import (
"fmt"
"net/url" "net/url"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
"github.com/valyala/fastjson" "github.com/valyala/fastjson"
) )
@ -82,17 +80,6 @@ func (node NodeInfoWellKnown) Validate() []string {
return result 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 ------------------------------------------------
// NodeInfo data type // NodeInfo data type

View file

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

View file

@ -8,7 +8,7 @@ import (
"net/http" "net/http"
"strings" "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/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
@ -36,7 +36,7 @@ func Repository(ctx *context.APIContext) {
// "$ref": "#/responses/ActivityPub" // "$ref": "#/responses/ActivityPub"
link := fmt.Sprintf("%s/api/v1/activitypub/repository-id/%d", strings.TrimSuffix(setting.AppURL, "/"), ctx.Repo.Repository.ID) 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() repo.Name = ap.NaturalLanguageValuesNew()
err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name)) err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name))

View file

@ -6,8 +6,8 @@ package activitypub
import ( import (
"net/http" "net/http"
"code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/context"

View file

@ -73,7 +73,6 @@ import (
actions_model "code.gitea.io/gitea/models/actions" actions_model "code.gitea.io/gitea/models/actions"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/forgefed"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
@ -81,6 +80,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" 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/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"

View file

@ -5,7 +5,7 @@
package swagger package swagger
import ( import (
ffed "code.gitea.io/gitea/models/forgefed" ffed "code.gitea.io/gitea/modules/forgefed"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/forms"
) )

View file

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/models/user" "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/auth/password" "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/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
@ -31,7 +32,7 @@ import (
// Star the repo if it wasn't already stared // Star the repo if it wasn't already stared
// Do some mitigation against out of order attacks // Do some mitigation against out of order attacks
func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int, string, error) { 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 { if res, err := validation.IsValid(activity); !res {
return http.StatusNotAcceptable, "Invalid activity", err 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) { if !activity.IsNewer(federationHost.LatestActivity) {
return http.StatusNotAcceptable, "Activity out of order.", fmt.Errorf("Activity already processed") 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 { if err != nil {
return http.StatusNotAcceptable, "Invalid PersonID", err return http.StatusNotAcceptable, "Invalid PersonID", err
} }
log.Info("Actor accepted:%v", actorID) log.Info("Actor accepted:%v", actorID)
// parse objectID (repository) // 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 { if err != nil {
return http.StatusNotAcceptable, "Invalid objectId", err return http.StatusNotAcceptable, "Invalid objectId", err
} }
@ -96,7 +97,7 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int
return 0, "", nil 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() actionsUser := user.NewActionsUser()
client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.")
if err != nil { 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) { func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) {
// parse actorID (person) // parse actorID (person)
log.Info("Input was: %v", actorURI) log.Info("Input was: %v", actorURI)
rawActorID, err := forgefed.NewActorID(actorURI) rawActorID, err := fm.NewActorID(actorURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,7 +151,7 @@ func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.Fe
return federationHost, nil 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? // ToDo: Do we get a publicKeyId from server, repo or owner or repo?
actionsUser := user.NewActionsUser() actionsUser := user.NewActionsUser()
client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") 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 return nil, nil, err
} }
person := forgefed.ForgePerson{} person := fm.ForgePerson{}
err = person.UnmarshalJSON(body) err = person.UnmarshalJSON(body)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -222,7 +223,7 @@ func StoreFollowingRepoList(ctx context.Context, localRepoID int64, followingRep
if err != nil { if err != nil {
return http.StatusInternalServerError, "Wrong FederationHost", err 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 { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err return http.StatusNotAcceptable, "Invalid federated repo", err
} }
@ -251,11 +252,11 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error
return err return err
} }
likeActivityList := make([]forgefed.ForgeLike, 0) likeActivityList := make([]fm.ForgeLike, 0)
for _, followingRepo := range followingRepos { for _, followingRepo := range followingRepos {
log.Info("Found following repo: %v", followingRepo) log.Info("Found following repo: %v", followingRepo)
target := followingRepo.URI target := followingRepo.URI
likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) likeActivity, err := fm.NewForgeLike(doer.APAPIURL(), target, time.Now())
if err != nil { if err != nil {
return err return err
} }

View file

@ -21389,7 +21389,7 @@
"ForgeLike": { "ForgeLike": {
"description": "ForgeLike activity data type", "description": "ForgeLike activity data type",
"type": "object", "type": "object",
"x-go-package": "code.gitea.io/gitea/models/forgefed" "x-go-package": "code.gitea.io/gitea/modules/forgefed"
}, },
"GPGKey": { "GPGKey": {
"description": "GPGKey a user GPG key to sign commit and tag in repository", "description": "GPGKey a user GPG key to sign commit and tag in repository",