mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
Attempt generalization of Id creation
This commit is contained in:
parent
15775ad891
commit
c887bddb72
1 changed files with 58 additions and 61 deletions
|
@ -12,7 +12,12 @@ import (
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Validateable interface {
|
||||||
|
Validate() []string
|
||||||
|
}
|
||||||
|
|
||||||
type ActorId struct {
|
type ActorId struct {
|
||||||
|
Validateable
|
||||||
Id string
|
Id string
|
||||||
Source string
|
Source string
|
||||||
Schema string
|
Schema string
|
||||||
|
@ -30,12 +35,7 @@ type RepositoryId struct {
|
||||||
ActorId
|
ActorId
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPersonId(uri string, source string) (PersonId, error) {
|
func newActorId(uri, source string) (ActorId, error) {
|
||||||
// TODO: remove after test
|
|
||||||
//if !validation.IsValidExternalURL(uri) {
|
|
||||||
// return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
|
|
||||||
//}
|
|
||||||
|
|
||||||
validatedUri, _ := url.Parse(uri) // ToDo: Why no err treatment at this place?
|
validatedUri, _ := url.Parse(uri) // ToDo: Why no err treatment at this place?
|
||||||
pathWithActorID := strings.Split(validatedUri.Path, "/")
|
pathWithActorID := strings.Split(validatedUri.Path, "/")
|
||||||
if containsEmptyString(pathWithActorID) {
|
if containsEmptyString(pathWithActorID) {
|
||||||
|
@ -45,7 +45,7 @@ func NewPersonId(uri string, source string) (PersonId, error) {
|
||||||
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
||||||
id := pathWithActorID[length-1]
|
id := pathWithActorID[length-1]
|
||||||
|
|
||||||
result := PersonId{}
|
result := ActorId{}
|
||||||
result.Id = id
|
result.Id = id
|
||||||
result.Source = source
|
result.Source = source
|
||||||
result.Schema = validatedUri.Scheme
|
result.Schema = validatedUri.Scheme
|
||||||
|
@ -53,41 +53,52 @@ func NewPersonId(uri string, source string) (PersonId, error) {
|
||||||
result.Path = pathWithoutActorID
|
result.Path = pathWithoutActorID
|
||||||
result.Port = validatedUri.Port()
|
result.Port = validatedUri.Port()
|
||||||
result.UnvalidatedInput = uri
|
result.UnvalidatedInput = uri
|
||||||
|
|
||||||
if valid, err := result.IsValid(); !valid {
|
if valid, err := result.IsValid(); !valid {
|
||||||
return PersonId{}, err
|
return ActorId{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPersonId(uri string, source string) (PersonId, error) {
|
||||||
|
// TODO: remove after test
|
||||||
|
//if !validation.IsValidExternalURL(uri) {
|
||||||
|
// return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
|
||||||
|
//}
|
||||||
|
|
||||||
|
actorId, err := newActorId(uri, source)
|
||||||
|
if err != nil {
|
||||||
|
return PersonId{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate Person specific path
|
||||||
|
personId := PersonId{actorId}
|
||||||
|
if valid, outcome := personId.IsValid(); !valid {
|
||||||
|
return PersonId{}, outcome
|
||||||
|
}
|
||||||
|
|
||||||
|
return personId, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: tbd how an which parts can be generalized
|
// TODO: tbd how an which parts can be generalized
|
||||||
func NewRepositoryId(uri string, source string) (RepositoryId, error) {
|
func NewRepositoryId(uri string, source string) (RepositoryId, error) {
|
||||||
if !validation.IsAPIURL(uri) {
|
if !validation.IsAPIURL(uri) {
|
||||||
return RepositoryId{}, fmt.Errorf("uri %s is not a valid repo url on this host %s", uri, setting.AppURL+"api")
|
return RepositoryId{}, fmt.Errorf("uri %s is not a valid repo url on this host %s", uri, setting.AppURL+"api")
|
||||||
}
|
}
|
||||||
|
|
||||||
validatedUri, _ := url.Parse(uri) // ToDo: Why no err treatment at this place?
|
actorId, err := newActorId(uri, source)
|
||||||
pathWithActorID := strings.Split(validatedUri.Path, "/")
|
if err != nil {
|
||||||
if containsEmptyString(pathWithActorID) {
|
|
||||||
pathWithActorID = removeEmptyStrings(pathWithActorID)
|
|
||||||
}
|
|
||||||
length := len(pathWithActorID)
|
|
||||||
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
|
||||||
id := pathWithActorID[length-1]
|
|
||||||
|
|
||||||
result := RepositoryId{}
|
|
||||||
result.Id = id
|
|
||||||
result.Source = source
|
|
||||||
result.Schema = validatedUri.Scheme
|
|
||||||
result.Host = validatedUri.Hostname()
|
|
||||||
result.Path = pathWithoutActorID
|
|
||||||
result.Port = validatedUri.Port()
|
|
||||||
result.UnvalidatedInput = uri
|
|
||||||
if valid, err := result.IsValid(); !valid {
|
|
||||||
return RepositoryId{}, err
|
return RepositoryId{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
// validate Person specific path
|
||||||
|
repoId := RepositoryId{actorId}
|
||||||
|
if valid, outcome := repoId.IsValid(); !valid {
|
||||||
|
return RepositoryId{}, outcome
|
||||||
|
}
|
||||||
|
|
||||||
|
return repoId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (id ActorId) AsUri() string {
|
func (id ActorId) AsUri() string {
|
||||||
|
@ -118,7 +129,7 @@ func (id PersonId) HostSuffix() string {
|
||||||
/*
|
/*
|
||||||
Validate collects error strings in a slice and returns this
|
Validate collects error strings in a slice and returns this
|
||||||
*/
|
*/
|
||||||
func (value PersonId) Validate() []string {
|
func (value ActorId) Validate() []string {
|
||||||
var result = []string{}
|
var result = []string{}
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
|
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Source, "source")...)
|
result = append(result, validation.ValidateNotEmpty(value.Source, "source")...)
|
||||||
|
@ -126,37 +137,8 @@ func (value PersonId) Validate() []string {
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Path, "path")...)
|
result = append(result, validation.ValidateNotEmpty(value.Path, "path")...)
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Host, "host")...)
|
result = append(result, validation.ValidateNotEmpty(value.Host, "host")...)
|
||||||
result = append(result, validation.ValidateNotEmpty(value.UnvalidatedInput, "unvalidatedInput")...)
|
result = append(result, validation.ValidateNotEmpty(value.UnvalidatedInput, "unvalidatedInput")...)
|
||||||
|
|
||||||
result = append(result, validation.ValidateOneOf(value.Source, []string{"forgejo", "gitea"})...)
|
result = append(result, validation.ValidateOneOf(value.Source, []string{"forgejo", "gitea"})...)
|
||||||
switch value.Source {
|
|
||||||
case "forgejo", "gitea":
|
|
||||||
if strings.ToLower(value.Path) != "api/v1/activitypub/user-id" && strings.ToLower(value.Path) != "api/activitypub/user-id" {
|
|
||||||
result = append(result, fmt.Sprintf("path: %q has to be a api path", value.Path))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if value.UnvalidatedInput != value.AsUri() {
|
|
||||||
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", value.UnvalidatedInput, value.AsUri()))
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (value RepositoryId) Validate() []string {
|
|
||||||
var result = []string{}
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Source, "source")...)
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Schema, "schema")...)
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Path, "path")...)
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Host, "host")...)
|
|
||||||
result = append(result, validation.ValidateNotEmpty(value.UnvalidatedInput, "unvalidatedInput")...)
|
|
||||||
|
|
||||||
result = append(result, validation.ValidateOneOf(value.Source, []string{"forgejo", "gitea"})...)
|
|
||||||
switch value.Source {
|
|
||||||
case "forgejo", "gitea":
|
|
||||||
if strings.ToLower(value.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(value.Path) != "api/activitypub/repository-id" {
|
|
||||||
result = append(result, fmt.Sprintf("path: %q has to be a api path", value.Path))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if value.UnvalidatedInput != value.AsUri() {
|
if value.UnvalidatedInput != value.AsUri() {
|
||||||
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", value.UnvalidatedInput, value.AsUri()))
|
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", value.UnvalidatedInput, value.AsUri()))
|
||||||
}
|
}
|
||||||
|
@ -168,18 +150,33 @@ func (value RepositoryId) Validate() []string {
|
||||||
/*
|
/*
|
||||||
IsValid concatenates the error messages with newlines and returns them if there are any
|
IsValid concatenates the error messages with newlines and returns them if there are any
|
||||||
*/
|
*/
|
||||||
func (a PersonId) IsValid() (bool, error) {
|
func (a ActorId) IsValid() (bool, error) {
|
||||||
if err := a.Validate(); len(err) > 0 {
|
if err := a.Validate(); len(err) > 0 {
|
||||||
errString := strings.Join(err, "\n")
|
errString := strings.Join(err, "\n")
|
||||||
return false, fmt.Errorf(errString)
|
return false, fmt.Errorf(errString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a PersonId) IsValid() (bool, error) {
|
||||||
|
switch a.Source {
|
||||||
|
case "forgejo", "gitea":
|
||||||
|
if strings.ToLower(a.Path) != "api/v1/activitypub/user-id" && strings.ToLower(a.Path) != "api/activitypub/user-id" {
|
||||||
|
err := fmt.Errorf("path: %q has to be an api path", a.Path)
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a RepositoryId) IsValid() (bool, error) {
|
func (a RepositoryId) IsValid() (bool, error) {
|
||||||
if err := a.Validate(); len(err) > 0 {
|
switch a.Source {
|
||||||
errString := strings.Join(err, "\n")
|
case "forgejo", "gitea":
|
||||||
return false, fmt.Errorf(errString)
|
if strings.ToLower(a.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(a.Path) != "api/activitypub/repository-id" {
|
||||||
|
err := fmt.Errorf("path: %q has to be an api path", a.Path)
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue