mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 08:31:27 -05:00
refactoring: separaate model & module
This commit is contained in:
parent
42837f5dab
commit
957b1023e9
15 changed files with 36 additions and 29 deletions
|
@ -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
|
||||
|
|
19
modules/forgefed/nodeinfo.go
Normal file
19
modules/forgefed/nodeinfo.go
Normal 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
|
||||
}
|
|
@ -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))
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
2
templates/swagger/v1_json.tmpl
generated
2
templates/swagger/v1_json.tmpl
generated
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue