forgejo/modules/forgefed/actor_person.go
Michael Jerger 3bb6ed8f19
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
refactor & enhance AP elements used (#7728)
This PR is part of https://codeberg.org/forgejo/forgejo/pulls/4767 filed by @algernon

To keep changes isolated this might be reviewed &  merge after https://codeberg.org/forgejo/forgejo/pulls/7714

Refactor existing code to have one AP struct per file.
Enhance AP structs needed by related PR

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Co-authored-by: zam <mirco.zachmann@meissa.de>
Co-authored-by: Mirco Zachmann <nostar@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7728
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
2025-06-02 22:29:10 +02:00

122 lines
3.2 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"
)
// 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
}
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")...)
if strings.ToLower(id.Path) != personIDapiPathV1 && strings.ToLower(id.Path) != personIDapiPathV1Latest {
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(string(s.Type), "Type")...)
result = append(result, validation.ValidateOneOf(string(s.Type), []any{string(ap.PersonType)}, "Type")...)
result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "PreferredUsername")...)
return result
}