From e8371ca94c1a34dc9eb68b573f215bec2ad23ae2 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 8 Dec 2023 19:43:49 +0100 Subject: [PATCH] ActorId -> PersonId --- models/activitypub/actor.go | 26 ++++++++++++------------ models/activitypub/actor_test.go | 20 +++++++++--------- routers/api/v1/activitypub/repository.go | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/models/activitypub/actor.go b/models/activitypub/actor.go index b794adc247..52c6a6494a 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 PersonId struct { userId string source string schema string @@ -38,7 +38,7 @@ func validate_is_not_empty(str string) error { /* Validate collects error strings in a slice and returns this */ -func (value ActorId) Validate() []string { +func (value PersonId) Validate() []string { var result = []string{} result = append(result, validation.ValidateNotEmpty(value.userId, "userId")...) result = append(result, validation.ValidateNotEmpty(value.source, "source")...) @@ -61,7 +61,7 @@ func (value 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 PersonId) IsValid() (bool, error) { if err := a.Validate(); len(err) > 0 { errString := strings.Join(err, "\n") return false, fmt.Errorf(errString) @@ -69,13 +69,13 @@ func (a ActorId) IsValid() (bool, error) { return true, nil } -func (a ActorId) PanicIfInvalid() { +func (a PersonId) PanicIfInvalid() { if valid, err := a.IsValid(); !valid { panic(err) } } -func (a ActorId) GetUserId() int { +func (a PersonId) GetUserId() int { result, err := strconv.Atoi(a.userId) if err != nil { @@ -85,13 +85,13 @@ func (a ActorId) GetUserId() int { return result } -func (a ActorId) GetNormalizedUri() string { +func (a PersonId) 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 PersonId) GetHostAndPort() string { if a.port != "" { return strings.Join([]string{a.host, a.port}, ":") @@ -138,7 +138,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) PersonId { // ToDo: Turn this into a factory function and do not split parsing and validation rigurously pathWithUserID := strings.Split(validatedURL.Path, "/") @@ -154,7 +154,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 PersonId{ // ToDo: maybe keep original input to validate against (maybe extra method) userId: userId, source: source, schema: validatedURL.Scheme, @@ -164,9 +164,9 @@ func ParseActorID(validatedURL url.URL, source string) ActorId { // ToDo: Turn t } } -func NewActorId(uri string, source string) (ActorId, error) { +func NewPersonId(uri string, source string) (PersonId, error) { if !validation.IsValidExternalURL(uri) { - return ActorId{}, fmt.Errorf("uri %s is not a valid external url", uri) + return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri) } validatedUri, _ := url.Parse(uri) @@ -180,7 +180,7 @@ func NewActorId(uri string, source string) (ActorId, error) { pathWithoutUserID := strings.Join(pathWithUserID[0:length-1], "/") userId := pathWithUserID[length-1] - actorId := ActorId{ + actorId := PersonId{ userId: userId, source: source, schema: validatedUri.Scheme, @@ -190,7 +190,7 @@ func NewActorId(uri string, source string) (ActorId, error) { unvalidatedInput: uri, } if valid, err := actorId.IsValid(); !valid { - return ActorId{}, err + return PersonId{}, err } return actorId, nil diff --git a/models/activitypub/actor_test.go b/models/activitypub/actor_test.go index 4ac241e5d2..6959d8772a 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 := PersonId{ userId: "1", source: "forgejo", schema: "https", @@ -76,7 +76,7 @@ func TestActorParserValid(t *testing.T) { } func TestValidateValid(t *testing.T) { - item := ActorId{ + item := PersonId{ userId: "1", source: "forgejo", schema: "https", @@ -101,7 +101,7 @@ func TestValidateInvalid(t *testing.T) { } func TestGetHostAndPort(t *testing.T) { - item := ActorId{ + item := PersonId{ schema: "https", userId: "1", path: "/api/v1/activitypub/user-id/1", @@ -119,33 +119,33 @@ func TestGetHostAndPort(t *testing.T) { } func TestShouldThrowErrorOnInvalidInput(t *testing.T) { - _, err := NewActorId("", "forgejo") + _, err := NewPersonId("", "forgejo") if err == nil { t.Errorf("empty input should be invalid.") } - _, err = NewActorId("http://localhost:3000/api/v1/something", "forgejo") + _, err = NewPersonId("http://localhost:3000/api/v1/something", "forgejo") if err == nil { t.Errorf("localhost uris are not external") } - _, err = NewActorId("./api/v1/something", "forgejo") + _, err = NewPersonId("./api/v1/something", "forgejo") if err == nil { t.Errorf("relative uris are not alowed") } - _, err = NewActorId("http://1.2.3.4/api/v1/something", "forgejo") + _, err = NewPersonId("http://1.2.3.4/api/v1/something", "forgejo") if err == nil { t.Errorf("uri may not be ip-4 based") } - _, err = NewActorId("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo") + _, err = NewPersonId("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo") if err == nil { t.Errorf("uri may not be ip-6 based") } - _, err = NewActorId("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo") + _, err = NewPersonId("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo") if err == nil { t.Errorf("uri may not contain relative path elements") } - _, err = NewActorId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo") + _, err = NewPersonId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo") if err != nil { t.Errorf("this uri should be valid but was: %v", err) } diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 9082e4143b..867dabeb2f 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 PersonId Struct valid? actorId.PanicIfInvalid() log.Info("RepositoryInbox: Actor parsed. %v", actorId)