mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
Refactor ActorID -> ActorId
This commit is contained in:
parent
7c86f13728
commit
be4d3544ae
3 changed files with 20 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue