mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-07-09 22:00:03 +02:00

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>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
// Copyright 2024, 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgefed
|
|
|
|
import (
|
|
"forgejo.org/modules/validation"
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// ForgeFollow activity data type
|
|
// swagger:model
|
|
type ForgeFollow struct {
|
|
// swagger:ignore
|
|
ap.Activity
|
|
}
|
|
|
|
func NewForgeFollowFromAp(activity ap.Activity) (ForgeFollow, error) {
|
|
result := ForgeFollow{}
|
|
result.Activity = activity
|
|
if valid, err := validation.IsValid(result); !valid {
|
|
return ForgeFollow{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func NewForgeFollow(actor, object string) (ForgeFollow, error) {
|
|
result := ForgeFollow{}
|
|
result.Type = ap.FollowType
|
|
result.ID = ap.IRI(actor + "/follows/" + uuid.New().String())
|
|
result.Actor = ap.IRI(actor)
|
|
result.Object = ap.IRI(object)
|
|
if valid, err := validation.IsValid(result); !valid {
|
|
return ForgeFollow{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (follow ForgeFollow) MarshalJSON() ([]byte, error) {
|
|
return follow.Activity.MarshalJSON()
|
|
}
|
|
|
|
func (follow *ForgeFollow) UnmarshalJSON(data []byte) error {
|
|
return follow.Activity.UnmarshalJSON(data)
|
|
}
|
|
|
|
func (follow ForgeFollow) Validate() []string {
|
|
var result []string
|
|
result = append(result, validation.ValidateNotEmpty(string(follow.Type), "type")...)
|
|
result = append(result, validation.ValidateOneOf(string(follow.Type), []any{"Follow"}, "type")...)
|
|
result = append(result, validation.ValidateIDExists(follow.Actor, "actor")...)
|
|
result = append(result, validation.ValidateIDExists(follow.Object, "object")...)
|
|
|
|
return result
|
|
}
|