actors.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package controllers
  2. import (
  3. "net/http"
  4. "strings"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/owncast/owncast/activitypub/apmodels"
  7. "github.com/owncast/owncast/activitypub/crypto"
  8. "github.com/owncast/owncast/activitypub/requests"
  9. "github.com/owncast/owncast/core/data"
  10. )
  11. // ActorHandler handles requests for a single actor.
  12. func ActorHandler(w http.ResponseWriter, r *http.Request) {
  13. if !data.GetFederationEnabled() {
  14. w.WriteHeader(http.StatusMethodNotAllowed)
  15. return
  16. }
  17. pathComponents := strings.Split(r.URL.Path, "/")
  18. accountName := pathComponents[3]
  19. if _, valid := data.GetFederatedInboxMap()[accountName]; !valid {
  20. // User is not valid
  21. w.WriteHeader(http.StatusNotFound)
  22. return
  23. }
  24. // If this request is for an actor's inbox then pass
  25. // the request to the inbox controller.
  26. if len(pathComponents) == 5 && pathComponents[4] == "inbox" {
  27. InboxHandler(w, r)
  28. return
  29. } else if len(pathComponents) == 5 && pathComponents[4] == "outbox" {
  30. OutboxHandler(w, r)
  31. return
  32. } else if len(pathComponents) == 5 && pathComponents[4] == "followers" {
  33. // followers list
  34. FollowersHandler(w, r)
  35. return
  36. } else if len(pathComponents) == 5 && pathComponents[4] == "following" {
  37. // following list (none)
  38. w.WriteHeader(http.StatusNotFound)
  39. return
  40. }
  41. actorIRI := apmodels.MakeLocalIRIForAccount(accountName)
  42. publicKey := crypto.GetPublicKey(actorIRI)
  43. person := apmodels.MakeServiceForAccount(accountName)
  44. if err := requests.WriteStreamResponse(person, w, publicKey); err != nil {
  45. log.Errorln("unable to write stream response for actor handler", err)
  46. w.WriteHeader(http.StatusInternalServerError)
  47. return
  48. }
  49. }