forgejo/routers/api/v1/activitypub/response.go

36 lines
893 B
Go
Raw Normal View History

2023-11-06 17:29:48 +00:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package activitypub
import (
"net/http"
"code.gitea.io/gitea/modules/activitypub"
2024-05-03 06:00:17 +00:00
"code.gitea.io/gitea/modules/forgefed"
2023-11-06 17:29:48 +00:00
"code.gitea.io/gitea/modules/log"
2024-04-09 14:00:28 +00:00
"code.gitea.io/gitea/services/context"
2023-11-06 17:29:48 +00:00
ap "github.com/go-ap/activitypub"
"github.com/go-ap/jsonld"
)
// Respond with an ActivityStreams object
2024-05-06 14:01:23 +00:00
func response(ctx *context.APIContext, v any) {
2023-11-06 17:29:48 +00:00
binary, err := jsonld.WithContext(
jsonld.IRI(ap.ActivityBaseURI),
jsonld.IRI(ap.SecurityContextURI),
jsonld.IRI(forgefed.ForgeFedNamespaceURI),
).Marshal(v)
if err != nil {
ctx.ServerError("Marshal", 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)
}
}