object.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package controllers
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/owncast/owncast/activitypub/apmodels"
  6. "github.com/owncast/owncast/activitypub/crypto"
  7. "github.com/owncast/owncast/activitypub/persistence"
  8. "github.com/owncast/owncast/activitypub/requests"
  9. "github.com/owncast/owncast/core/data"
  10. log "github.com/sirupsen/logrus"
  11. )
  12. // ObjectHandler handles requests for a single federated ActivityPub object.
  13. func ObjectHandler(w http.ResponseWriter, r *http.Request) {
  14. if !data.GetFederationEnabled() {
  15. w.WriteHeader(http.StatusMethodNotAllowed)
  16. return
  17. }
  18. // If private federation mode is enabled do not allow access to objects.
  19. if data.GetFederationIsPrivate() {
  20. w.WriteHeader(http.StatusNotFound)
  21. return
  22. }
  23. iri := strings.Join([]string{strings.TrimSuffix(data.GetServerURL(), "/"), r.URL.Path}, "")
  24. object, _, _, err := persistence.GetObjectByIRI(iri)
  25. if err != nil {
  26. w.WriteHeader(http.StatusNotFound)
  27. return
  28. }
  29. accountName := data.GetDefaultFederationUsername()
  30. actorIRI := apmodels.MakeLocalIRIForAccount(accountName)
  31. publicKey := crypto.GetPublicKey(actorIRI)
  32. if err := requests.WriteResponse([]byte(object), w, publicKey); err != nil {
  33. log.Errorln(err)
  34. }
  35. }