mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 14:53:34 +01:00
Import parser from model and call validation
This commit is contained in:
parent
273ca49e22
commit
91dcd59e68
1 changed files with 10 additions and 44 deletions
|
@ -6,9 +6,9 @@ package activitypub
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/activitypub"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/forgefed"
|
"code.gitea.io/gitea/modules/forgefed"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
@ -19,47 +19,6 @@ import (
|
||||||
//f3 "lab.forgefriends.org/friendlyforgeformat/gof3"
|
//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
|
// Repository function returns the Repository actor for a repo
|
||||||
func Repository(ctx *context.APIContext) {
|
func Repository(ctx *context.APIContext) {
|
||||||
// swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository
|
// 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.
|
// 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
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue