ActorId -> PersonId

This commit is contained in:
Michael Jerger 2023-12-08 19:43:49 +01:00
parent 3151c8fe81
commit e8371ca94c
3 changed files with 24 additions and 24 deletions

View file

@ -18,7 +18,7 @@ type Validatable interface { // ToDo: What is the right package for this interfa
PanicIfInvalid() PanicIfInvalid()
} }
type ActorId struct { type PersonId struct {
userId string userId string
source string source string
schema 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 Validate collects error strings in a slice and returns this
*/ */
func (value ActorId) Validate() []string { func (value PersonId) Validate() []string {
var result = []string{} var result = []string{}
result = append(result, validation.ValidateNotEmpty(value.userId, "userId")...) result = append(result, validation.ValidateNotEmpty(value.userId, "userId")...)
result = append(result, validation.ValidateNotEmpty(value.source, "source")...) 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 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 { 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)
@ -69,13 +69,13 @@ func (a ActorId) IsValid() (bool, error) {
return true, nil return true, nil
} }
func (a ActorId) PanicIfInvalid() { func (a PersonId) PanicIfInvalid() {
if valid, err := a.IsValid(); !valid { if valid, err := a.IsValid(); !valid {
panic(err) panic(err)
} }
} }
func (a ActorId) GetUserId() int { func (a PersonId) GetUserId() int {
result, err := strconv.Atoi(a.userId) result, err := strconv.Atoi(a.userId)
if err != nil { if err != nil {
@ -85,13 +85,13 @@ func (a ActorId) GetUserId() int {
return result 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) result := fmt.Sprintf("%s://%s:%s/%s/%s", a.schema, a.host, a.port, a.path, a.userId)
return result return result
} }
// Returns the combination of host:port if port exists, host otherwise // Returns the combination of host:port if port exists, host otherwise
func (a ActorId) GetHostAndPort() string { func (a PersonId) GetHostAndPort() string {
if a.port != "" { if a.port != "" {
return strings.Join([]string{a.host, 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?) // 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, "/") 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: pathWithoutUserID: %s", pathWithoutUserID)
log.Info("Actor: UserID: %s", userId) 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, userId: userId,
source: source, source: source,
schema: validatedURL.Scheme, 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) { 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) validatedUri, _ := url.Parse(uri)
@ -180,7 +180,7 @@ func NewActorId(uri string, source string) (ActorId, error) {
pathWithoutUserID := strings.Join(pathWithUserID[0:length-1], "/") pathWithoutUserID := strings.Join(pathWithUserID[0:length-1], "/")
userId := pathWithUserID[length-1] userId := pathWithUserID[length-1]
actorId := ActorId{ actorId := PersonId{
userId: userId, userId: userId,
source: source, source: source,
schema: validatedUri.Scheme, schema: validatedUri.Scheme,
@ -190,7 +190,7 @@ func NewActorId(uri string, source string) (ActorId, error) {
unvalidatedInput: uri, unvalidatedInput: uri,
} }
if valid, err := actorId.IsValid(); !valid { if valid, err := actorId.IsValid(); !valid {
return ActorId{}, err return PersonId{}, err
} }
return actorId, nil return actorId, nil

View file

@ -59,7 +59,7 @@ func TestValidateAndParseIRINoPath(t *testing.T) {
func TestActorParserValid(t *testing.T) { func TestActorParserValid(t *testing.T) {
item, _ := ValidateAndParseIRI(mockStar.Actor.GetID().String()) item, _ := ValidateAndParseIRI(mockStar.Actor.GetID().String())
want := ActorId{ want := PersonId{
userId: "1", userId: "1",
source: "forgejo", source: "forgejo",
schema: "https", schema: "https",
@ -76,7 +76,7 @@ func TestActorParserValid(t *testing.T) {
} }
func TestValidateValid(t *testing.T) { func TestValidateValid(t *testing.T) {
item := ActorId{ item := PersonId{
userId: "1", userId: "1",
source: "forgejo", source: "forgejo",
schema: "https", schema: "https",
@ -101,7 +101,7 @@ func TestValidateInvalid(t *testing.T) {
} }
func TestGetHostAndPort(t *testing.T) { func TestGetHostAndPort(t *testing.T) {
item := ActorId{ item := PersonId{
schema: "https", schema: "https",
userId: "1", userId: "1",
path: "/api/v1/activitypub/user-id/1", path: "/api/v1/activitypub/user-id/1",
@ -119,33 +119,33 @@ func TestGetHostAndPort(t *testing.T) {
} }
func TestShouldThrowErrorOnInvalidInput(t *testing.T) { func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
_, err := NewActorId("", "forgejo") _, err := NewPersonId("", "forgejo")
if err == nil { if err == nil {
t.Errorf("empty input should be invalid.") 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 { if err == nil {
t.Errorf("localhost uris are not external") t.Errorf("localhost uris are not external")
} }
_, err = NewActorId("./api/v1/something", "forgejo") _, err = NewPersonId("./api/v1/something", "forgejo")
if err == nil { if err == nil {
t.Errorf("relative uris are not alowed") 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 { if err == nil {
t.Errorf("uri may not be ip-4 based") 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 { if err == nil {
t.Errorf("uri may not be ip-6 based") 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 { if err == nil {
t.Errorf("uri may not contain relative path elements") 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 { if err != nil {
t.Errorf("this uri should be valid but was: %v", err) t.Errorf("this uri should be valid but was: %v", err)
} }

View file

@ -254,7 +254,7 @@ func RepositoryInbox(ctx *context.APIContext) {
} }
actorId := activitypub.ParseActorID(validatedActorId, string(activity.Source)) actorId := activitypub.ParseActorID(validatedActorId, string(activity.Source))
// Is the ActorId Struct valid? // Is the PersonId Struct valid?
actorId.PanicIfInvalid() actorId.PanicIfInvalid()
log.Info("RepositoryInbox: Actor parsed. %v", actorId) log.Info("RepositoryInbox: Actor parsed. %v", actorId)