inbox.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package controllers
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. "github.com/owncast/owncast/activitypub/apmodels"
  7. "github.com/owncast/owncast/activitypub/inbox"
  8. "github.com/owncast/owncast/core/data"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. // InboxHandler handles inbound federated requests.
  12. func InboxHandler(w http.ResponseWriter, r *http.Request) {
  13. if r.Method == http.MethodPost {
  14. acceptInboxRequest(w, r)
  15. } else {
  16. w.WriteHeader(http.StatusMethodNotAllowed)
  17. }
  18. }
  19. func acceptInboxRequest(w http.ResponseWriter, r *http.Request) {
  20. if !data.GetFederationEnabled() {
  21. w.WriteHeader(http.StatusMethodNotAllowed)
  22. return
  23. }
  24. urlPathComponents := strings.Split(r.URL.Path, "/")
  25. var forLocalAccount string
  26. if len(urlPathComponents) == 5 {
  27. forLocalAccount = urlPathComponents[3]
  28. } else {
  29. log.Errorln("Unable to determine username from url path")
  30. w.WriteHeader(http.StatusNotFound)
  31. return
  32. }
  33. // The account this request is for must match the account name we have set
  34. // for federation.
  35. if forLocalAccount != data.GetFederationUsername() {
  36. w.WriteHeader(http.StatusNotFound)
  37. return
  38. }
  39. data, err := io.ReadAll(r.Body)
  40. if err != nil {
  41. log.Errorln("Unable to read inbox request payload", err)
  42. return
  43. }
  44. inboxRequest := apmodels.InboxRequest{Request: r, ForLocalAccount: forLocalAccount, Body: data}
  45. inbox.AddToQueue(inboxRequest)
  46. w.WriteHeader(http.StatusAccepted)
  47. }