From be4d3544aea5fa4a9d73b9ef2a58c1d54b2cb629 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 8 Dec 2023 18:33:26 +0100 Subject: [PATCH] Refactor ActorID -> ActorId --- models/activitypub/actor.go | 24 ++++++++++++------------ models/activitypub/actor_test.go | 10 +++++++--- routers/api/v1/activitypub/repository.go | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/models/activitypub/actor.go b/models/activitypub/actor.go index 2c877a6ef5..88d295e91b 100644 --- a/models/activitypub/actor.go +++ b/models/activitypub/actor.go @@ -18,7 +18,7 @@ type Validatable interface { // ToDo: What is the right package for this interfa PanicIfInvalid() } -type ActorID struct { +type ActorId struct { userId string source string schema string @@ -37,7 +37,7 @@ func validate_is_not_empty(str string) error { /* Validate collects error strings in a slice and returns this */ -func (a ActorID) Validate() []string { +func (a ActorId) Validate() []string { var err = []string{} @@ -66,7 +66,7 @@ func (a ActorID) Validate() []string { /* IsValid concatenates the error messages with newlines and returns them if there are any */ -func (a ActorID) IsValid() (bool, error) { +func (a ActorId) IsValid() (bool, error) { if err := a.Validate(); len(err) > 0 { errString := strings.Join(err, "\n") return false, fmt.Errorf(errString) @@ -74,13 +74,13 @@ func (a ActorID) IsValid() (bool, error) { return true, nil } -func (a ActorID) PanicIfInvalid() { +func (a ActorId) PanicIfInvalid() { if valid, err := a.IsValid(); !valid { panic(err) } } -func (a ActorID) GetUserId() int { +func (a ActorId) GetUserId() int { result, err := strconv.Atoi(a.userId) if err != nil { @@ -90,13 +90,13 @@ func (a ActorID) GetUserId() int { return result } -func (a ActorID) GetNormalizedUri() string { +func (a ActorId) GetNormalizedUri() string { result := fmt.Sprintf("%s://%s:%s/%s/%s", a.schema, a.host, a.port, a.path, a.userId) return result } // Returns the combination of host:port if port exists, host otherwise -func (a ActorID) GetHostAndPort() string { +func (a ActorId) GetHostAndPort() string { if a.port != "" { return strings.Join([]string{a.host, a.port}, ":") @@ -143,7 +143,7 @@ func ValidateAndParseIRI(unvalidatedIRI string) (url.URL, error) { // ToDo: Vali } // TODO: This parsing is very Person-Specific. We should adjust the name & move to a better location (maybe forgefed package?) -func ParseActorID(validatedURL url.URL, source string) ActorID { // ToDo: Turn this into a factory function and do not split parsing and validation rigurously +func ParseActorID(validatedURL url.URL, source string) ActorId { // ToDo: Turn this into a factory function and do not split parsing and validation rigurously pathWithUserID := strings.Split(validatedURL.Path, "/") @@ -159,7 +159,7 @@ func ParseActorID(validatedURL url.URL, source string) ActorID { // ToDo: Turn t log.Info("Actor: pathWithoutUserID: %s", pathWithoutUserID) log.Info("Actor: UserID: %s", userId) - return ActorID{ // ToDo: maybe keep original input to validate against (maybe extra method) + return ActorId{ // ToDo: maybe keep original input to validate against (maybe extra method) userId: userId, source: source, schema: validatedURL.Scheme, @@ -169,9 +169,9 @@ func ParseActorID(validatedURL url.URL, source string) ActorID { // ToDo: Turn t } } -func NewActorId(uri string, source string) (ActorID, error) { +func NewActorId(uri string, source string) (ActorId, error) { if !validation.IsValidExternalURL(uri) { - return ActorID{}, fmt.Errorf("uri %s is not a valid external url.", uri) + return ActorId{}, fmt.Errorf("uri %s is not a valid external url", uri) } - return ActorID{}, nil + return ActorId{}, nil } diff --git a/models/activitypub/actor_test.go b/models/activitypub/actor_test.go index e8eb6e2d81..0d6123f359 100644 --- a/models/activitypub/actor_test.go +++ b/models/activitypub/actor_test.go @@ -59,7 +59,7 @@ func TestValidateAndParseIRINoPath(t *testing.T) { func TestActorParserValid(t *testing.T) { item, _ := ValidateAndParseIRI(mockStar.Actor.GetID().String()) - want := ActorID{ + want := ActorId{ userId: "1", source: "forgejo", schema: "https", @@ -76,7 +76,7 @@ func TestActorParserValid(t *testing.T) { } func TestValidateValid(t *testing.T) { - item := ActorID{ + item := ActorId{ userId: "1", source: "forgejo", schema: "https", @@ -101,7 +101,7 @@ func TestValidateInvalid(t *testing.T) { } func TestGetHostAndPort(t *testing.T) { - item := ActorID{ + item := ActorId{ schema: "https", userId: "1", path: "/api/v1/activitypub/user-id/1", @@ -128,6 +128,10 @@ func TestShouldThrowErrorOnInvalidInput(t *testing.T) { if err == nil { t.Errorf("localhost uris are not external") } + _, err = NewActorId("./api/v1/something", "forgejo") + if err == nil { + t.Errorf("relative uris are not alowed.") + } _, err = NewActorId("https://an.other.host/api/v1/activitypub/person-id/1", "forgejo") if err != nil { diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index e599133843..9082e4143b 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -254,7 +254,7 @@ func RepositoryInbox(ctx *context.APIContext) { } actorId := activitypub.ParseActorID(validatedActorId, string(activity.Source)) - // Is the ActorID Struct valid? + // Is the ActorId Struct valid? actorId.PanicIfInvalid() log.Info("RepositoryInbox: Actor parsed. %v", actorId)