Import parser from model and call validation

This commit is contained in:
erik 2023-11-16 14:50:01 +01:00 committed by Michael Jerger
parent 273ca49e22
commit 91dcd59e68

View file

@ -6,9 +6,9 @@ package activitypub
import (
"fmt"
"net/http"
"net/url"
"strings"
"code.gitea.io/gitea/models/activitypub"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/log"
@ -19,47 +19,6 @@ import (
//f3 "lab.forgefriends.org/friendlyforgeformat/gof3"
)
type (
Schema string
UserID string
Host string
Port string
)
type ActorData struct {
schema string
userId string
host string
port string // optional
}
func parseActor(actor string) (ActorData, error) {
u, err := url.Parse(actor)
// check if userID IRI is well formed url
if err != nil {
return ActorData{}, fmt.Errorf("the actor ID was not valid: %v", err)
}
if u.Scheme == "" || u.Host == "" {
return ActorData{}, fmt.Errorf("the actor ID was not valid: Invalid Schema or Host")
}
if !strings.Contains(u.Path, "api/v1/activitypub/user-id") {
return ActorData{}, fmt.Errorf("the Path to the API was invalid: %v\n the full URL is: %v", u.Path, actor)
}
pathWithUserID := strings.Split(u.Path, "/")
userId := pathWithUserID[len(pathWithUserID)-1]
return ActorData{
schema: u.Scheme,
userId: userId,
host: u.Host,
port: u.Port(),
}, nil
}
// Repository function returns the Repository actor for a repo
func Repository(ctx *context.APIContext) {
// swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository
@ -119,9 +78,16 @@ func RepositoryInbox(ctx *context.APIContext) {
// assume actor is: "actor": "https://codeberg.org/api/v1/activitypub/user-id/12345" - NB: This might be actually the ID? Maybe check vocabulary.
// parse actor
actor, err := parseActor(opt.Actor.GetID().String())
actor, err := activitypub.ParseActor(opt.Actor.GetID().String())
// Is the actor IRI well formed?
if err != nil {
panic(err)
}
// Is the ActorData Struct valid?
err = actor.ValidateActor()
// if not actor.isValid() then exit_with_error
if err != nil {
panic(err)
}