forgejo/modules/forgefed/activity_user_activity.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

77 lines
2 KiB
Go

// Copyright 2024, 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
"fmt"
"time"
user_model "forgejo.org/models/user"
"forgejo.org/modules/validation"
ap "github.com/go-ap/activitypub"
)
// ForgeFollow activity data type
// swagger:model
type ForgeUserActivity struct {
ap.Activity
Note ForgeUserActivityNote
}
func NewForgeUserActivityFromAp(activity ap.Activity) (ForgeUserActivity, error) {
result := ForgeUserActivity{}
result.Activity = activity
note, err := NewForgeUserActivityNoteFromAp(activity.Object)
if err != nil {
return ForgeUserActivity{}, err
}
result.Note = note
if valid, err := validation.IsValid(result); !valid {
return ForgeUserActivity{}, err
}
return result, nil
}
func NewForgeUserActivity(doer *user_model.User, actionID int64, content string) (ForgeUserActivity, error) {
id := fmt.Sprintf("%s/activities/%d", doer.APActorID(), actionID)
published := time.Now()
result := ForgeUserActivity{}
result.ID = ap.IRI(id + "/activity")
result.Type = ap.CreateType
result.Actor = ap.IRI(doer.APActorID())
result.Published = published
result.To = ap.ItemCollection{
ap.IRI("https://www.w3.org/ns/activitystreams#Public"),
}
result.CC = ap.ItemCollection{
ap.IRI(doer.APActorID() + "/followers"),
}
note, err := newNote(doer, content, id, published)
if err != nil {
return ForgeUserActivity{}, err
}
result.Object = note
return result, nil
}
func (userActivity ForgeUserActivity) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(string(userActivity.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(userActivity.Type), []any{"Create"}, "type")...)
result = append(result, validation.ValidateIDExists(userActivity.Actor, "actor")...)
if len(userActivity.To) == 0 {
result = append(result, "Missing to")
}
if len(userActivity.CC) == 0 {
result = append(result, "Missing cc")
}
result = append(result, userActivity.Note.Validate()...)
return result
}