Refactor ActorID -> ActorId
This commit is contained in:
parent
7c86f13728
commit
be4d3544ae
|
@ -18,7 +18,7 @@ type Validatable interface { // ToDo: What is the right package for this interfa
|
||||||
PanicIfInvalid()
|
PanicIfInvalid()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActorID struct {
|
type ActorId struct {
|
||||||
userId string
|
userId string
|
||||||
source string
|
source string
|
||||||
schema 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
|
Validate collects error strings in a slice and returns this
|
||||||
*/
|
*/
|
||||||
func (a ActorID) Validate() []string {
|
func (a ActorId) Validate() []string {
|
||||||
|
|
||||||
var err = []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
|
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 {
|
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)
|
||||||
|
@ -74,13 +74,13 @@ func (a ActorID) IsValid() (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ActorID) PanicIfInvalid() {
|
func (a ActorId) PanicIfInvalid() {
|
||||||
if valid, err := a.IsValid(); !valid {
|
if valid, err := a.IsValid(); !valid {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ActorID) GetUserId() int {
|
func (a ActorId) GetUserId() int {
|
||||||
result, err := strconv.Atoi(a.userId)
|
result, err := strconv.Atoi(a.userId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -90,13 +90,13 @@ func (a ActorID) GetUserId() int {
|
||||||
return result
|
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)
|
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 ActorId) GetHostAndPort() string {
|
||||||
|
|
||||||
if a.port != "" {
|
if a.port != "" {
|
||||||
return strings.Join([]string{a.host, 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?)
|
// 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, "/")
|
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: 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 ActorId{ // 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,
|
||||||
|
@ -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) {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 := ActorId{
|
||||||
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 := ActorId{
|
||||||
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 := ActorId{
|
||||||
schema: "https",
|
schema: "https",
|
||||||
userId: "1",
|
userId: "1",
|
||||||
path: "/api/v1/activitypub/user-id/1",
|
path: "/api/v1/activitypub/user-id/1",
|
||||||
|
@ -128,6 +128,10 @@ func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
|
||||||
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")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("relative uris are not alowed.")
|
||||||
|
}
|
||||||
|
|
||||||
_, err = NewActorId("https://an.other.host/api/v1/activitypub/person-id/1", "forgejo")
|
_, err = NewActorId("https://an.other.host/api/v1/activitypub/person-id/1", "forgejo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -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 ActorId Struct valid?
|
||||||
actorId.PanicIfInvalid()
|
actorId.PanicIfInvalid()
|
||||||
log.Info("RepositoryInbox: Actor parsed. %v", actorId)
|
log.Info("RepositoryInbox: Actor parsed. %v", actorId)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue