1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-10-20 19:52:04 +00:00
forgejo/routers/api/v1/activitypub/person.go
toras9000 5c6645a8af Update Swagger annotations to reflect actual behavior (#9138)
## 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...
  - [ ] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] 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.

----

This is a fix for the Swagger annotations reported in #8918.
Most of the changes are corrections to annotation comments, with some additions of wrapper struct definitions for Swagger auto‑generation.

Co-authored-by: toras9000 <toras9000@example.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9138
Reviewed-by: Lucas <sclu1034@noreply.codeberg.org>
Co-authored-by: toras9000 <toras9000@noreply.codeberg.org>
Co-committed-by: toras9000 <toras9000@noreply.codeberg.org>
2025-09-15 11:55:49 +02:00

244 lines
6.9 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package activitypub
import (
"net/http"
"forgejo.org/models/activities"
"forgejo.org/modules/activitypub"
"forgejo.org/modules/forgefed"
"forgejo.org/modules/log"
"forgejo.org/modules/web"
"forgejo.org/routers/api/v1/utils"
"forgejo.org/services/context"
"forgejo.org/services/convert"
"forgejo.org/services/federation"
ap "github.com/go-ap/activitypub"
"github.com/go-ap/jsonld"
)
// Person function returns the Person actor for a user
func Person(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user-id/{user-id} activitypub activitypubPerson
// ---
// summary: Returns the Person actor for a user
// produces:
// - application/json
// parameters:
// - name: user-id
// in: path
// description: user ID of the user
// type: integer
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActivityPub"
person, err := convert.ToActivityPubPerson(ctx, ctx.ContextUser)
if err != nil {
ctx.ServerError("convert.ToActivityPubPerson", err)
return
}
binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(person)
if err != nil {
ctx.ServerError("MarshalJSON", err)
return
}
ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
ctx.Resp.WriteHeader(http.StatusOK)
if _, err = ctx.Resp.Write(binary); err != nil {
log.Error("write to resp err: %v", err)
}
}
// PersonInbox function handles the incoming data for a user inbox
func PersonInbox(ctx *context.APIContext) {
// swagger:operation POST /activitypub/user-id/{user-id}/inbox activitypub activitypubPersonInbox
// ---
// summary: Send to the inbox
// produces:
// - application/json
// parameters:
// - name: user-id
// in: path
// description: user ID of the user
// type: integer
// format: int64
// required: true
// responses:
// "202":
// "$ref": "#/responses/empty"
form := web.GetForm(ctx)
activity := form.(*ap.Activity)
result, err := federation.ProcessPersonInbox(ctx, ctx.ContextUser, activity)
if err != nil {
ctx.Error(federation.HTTPStatus(err), "PersonInbox", err)
return
}
responseServiceResult(ctx, result)
}
// PersonFeed returns the recorded activities in the user's feed
func PersonFeed(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user-id/{user-id}/outbox activitypub activitypubPersonFeed
// ---
// summary: List the user's recorded activity
// produces:
// - application/json
// parameters:
// - name: user-id
// in: path
// description: user ID of the user
// type: integer
// required: true
// responses:
// "200":
// "$ref": "#/responses/PersonFeed"
// "403":
// "$ref": "#/responses/forbidden"
listOptions := utils.GetListOptions(ctx)
opts := activities.GetFollowingFeedsOptions{
ListOptions: listOptions,
}
items, count, err := activities.GetFollowingFeeds(ctx, ctx.ContextUser.ID, opts)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetFollowingFeeds", err)
return
}
ctx.SetTotalCountHeader(count)
feed := ap.OrderedCollectionNew(ap.IRI(ctx.ContextUser.APActorID() + "/outbox"))
feed.AttributedTo = ap.IRI(ctx.ContextUser.APActorID())
for _, item := range items {
if err := feed.OrderedItems.Append(convert.ToActivityPubPersonFeedItem(item)); err != nil {
ctx.Error(http.StatusInternalServerError, "OrderedItems.Append", err)
return
}
}
binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(feed)
if err != nil {
ctx.ServerError("MarshalJSON", err)
return
}
ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
ctx.Resp.WriteHeader(http.StatusOK)
if _, err = ctx.Resp.Write(binary); err != nil {
log.Error("write to resp err: %v", err)
}
}
func getActivity(ctx *context.APIContext, id int64) (*forgefed.ForgeUserActivity, error) {
action, err := activities.GetActivityByID(ctx, id)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetActivityByID", err.Error())
return nil, err
}
if action.UserID != action.ActUserID || action.ActUserID != ctx.ContextUser.ID {
ctx.NotFound()
return nil, err
}
actions := activities.ActionList{action}
if err := actions.LoadAttributes(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "action.LoadAttributes", err.Error())
return nil, err
}
activity, err := convert.ActionToForgeUserActivity(ctx, actions[0])
if err != nil {
ctx.Error(http.StatusInternalServerError, "ActionToForgeUserActivity", err.Error())
return nil, err
}
return &activity, nil
}
// PersonActivity returns a user's given activity
func PersonActivity(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user-id/{user-id}/activities/{activity-id}/activity activitypub activitypubPersonActivity
// ---
// summary: Get a specific activity of the user
// produces:
// - application/json
// parameters:
// - name: user-id
// in: path
// description: user ID of the user
// type: integer
// required: true
// - name: activity-id
// in: path
// description: activity ID of the sought activity
// type: integer
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActivityPub"
id := ctx.ParamsInt64("activity-id")
activity, err := getActivity(ctx, id)
if err != nil {
return
}
binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(activity)
if err != nil {
ctx.ServerError("MarshalJSON", err)
return
}
ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
ctx.Resp.WriteHeader(http.StatusOK)
if _, err = ctx.Resp.Write(binary); err != nil {
log.Error("write to resp err: %v", err)
}
}
// PersonActivity returns the Object part of a user's given activity
func PersonActivityNote(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user-id/{user-id}/activities/{activity-id} activitypub activitypubPersonActivityNote
// ---
// summary: Get a specific activity object of the user
// produces:
// - application/json
// parameters:
// - name: user-id
// in: path
// description: user ID of the user
// type: integer
// required: true
// - name: activity-id
// in: path
// description: activity ID of the sought activity
// type: integer
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActivityPub"
id := ctx.ParamsInt64("activity-id")
activity, err := getActivity(ctx, id)
if err != nil {
return
}
binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(activity.Object)
if err != nil {
ctx.ServerError("MarshalJSON", err)
return
}
ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
ctx.Resp.WriteHeader(http.StatusOK)
if _, err = ctx.Resp.Write(binary); err != nil {
log.Error("write to resp err: %v", err)
}
}