mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
First round of patches to re-enable some lints from my side. This PR also refactors the general key fetching code quite a bit due to the way it currently worked with relying on some values being nil sometimes. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11253 Reviewed-by: elle <0xllx0@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: famfo <famfo@famfo.xyz> Co-committed-by: famfo <famfo@famfo.xyz>
128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
// Copyright 2023, 2024, 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgefed
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"forgejo.org/modules/validation"
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
)
|
|
|
|
// ----------------------------- PersonID --------------------------------------------
|
|
type PersonID struct {
|
|
ActorID
|
|
}
|
|
|
|
const (
|
|
personIDapiPathV1 = "api/v1/activitypub/user-id"
|
|
personIDapiPathV1Latest = "api/activitypub/user-id"
|
|
actorIDapiPathV1 = "api/v1/activitypub"
|
|
actorIDapiPathLatest = "api/activitypub"
|
|
)
|
|
|
|
// Factory function for PersonID. Created struct is asserted to be valid
|
|
func NewPersonID(uri, source string) (PersonID, error) {
|
|
result, err := newActorID(uri)
|
|
if err != nil {
|
|
return PersonID{}, err
|
|
}
|
|
result.Source = source
|
|
|
|
// validate Person specific path
|
|
personID := PersonID{result}
|
|
if valid, err := validation.IsValid(personID); !valid {
|
|
return PersonID{}, err
|
|
}
|
|
|
|
return personID, nil
|
|
}
|
|
|
|
func NewPersonIDFromModel(host, schema string, port uint16, softwareName, id string) (PersonID, error) {
|
|
result := PersonID{}
|
|
result.ID = id
|
|
result.Source = softwareName
|
|
result.Host = host
|
|
result.HostSchema = schema
|
|
result.HostPort = port
|
|
result.IsPortSupplemented = false
|
|
|
|
if softwareName == "forgejo" {
|
|
result.Path = personIDapiPathV1
|
|
}
|
|
result.UnvalidatedInput = result.AsURI()
|
|
|
|
// validate Person specific path
|
|
if valid, err := validation.IsValid(result); !valid {
|
|
return PersonID{}, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (id PersonID) AsWebfinger() string {
|
|
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.ID), strings.ToLower(id.Host))
|
|
return result
|
|
}
|
|
|
|
func (id PersonID) AsLoginName() string {
|
|
result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
|
|
return result
|
|
}
|
|
|
|
// HostSuffix returns the host part of a handle, i.e. @host.tld (if port is supplemented) or @host.tld:1234
|
|
func (id PersonID) HostSuffix() string {
|
|
var result string
|
|
if !id.IsPortSupplemented {
|
|
result = fmt.Sprintf("@%s:%d", strings.ToLower(id.Host), id.HostPort)
|
|
} else {
|
|
result = fmt.Sprintf("@%s", strings.ToLower(id.Host))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (id PersonID) Validate() []string {
|
|
result := id.ActorID.Validate()
|
|
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
|
|
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea", "mastodon", "gotosocial"}, "Source")...)
|
|
if id.Source == "forgejo" {
|
|
result = append(result, validation.ValidateNotEmpty(id.Path, "path")...)
|
|
lowerPath := strings.ToLower(id.Path)
|
|
if lowerPath != personIDapiPathV1 && lowerPath != personIDapiPathV1Latest {
|
|
if lowerPath != actorIDapiPathV1 && lowerPath != actorIDapiPathLatest || id.ID != "actor" {
|
|
result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", id.Path))
|
|
}
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// ----------------------------- ForgePerson -------------------------------------
|
|
|
|
// ForgePerson activity data type
|
|
// swagger:model
|
|
type ForgePerson struct {
|
|
// swagger:ignore
|
|
ap.Actor
|
|
}
|
|
|
|
func (s ForgePerson) MarshalJSON() ([]byte, error) {
|
|
return s.Actor.MarshalJSON()
|
|
}
|
|
|
|
func (s *ForgePerson) UnmarshalJSON(data []byte) error {
|
|
return s.Actor.UnmarshalJSON(data)
|
|
}
|
|
|
|
func (s ForgePerson) Validate() []string {
|
|
var result []string
|
|
result = append(result, validation.ValidateNotEmpty(s.Type, "Type")...)
|
|
result = append(result, validation.ValidateOneOf(s.Type, []any{ap.PersonType}, "Type")...)
|
|
result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "PreferredUsername")...)
|
|
|
|
return result
|
|
}
|