follow.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package inbox
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/go-fed/activity/streams/vocab"
  7. "github.com/owncast/owncast/activitypub/persistence"
  8. "github.com/owncast/owncast/activitypub/requests"
  9. "github.com/owncast/owncast/activitypub/resolvers"
  10. "github.com/owncast/owncast/core/chat/events"
  11. "github.com/owncast/owncast/core/data"
  12. "github.com/pkg/errors"
  13. log "github.com/sirupsen/logrus"
  14. )
  15. func handleFollowInboxRequest(c context.Context, activity vocab.ActivityStreamsFollow) error {
  16. follow, err := resolvers.MakeFollowRequest(c, activity)
  17. if err != nil {
  18. log.Errorln("unable to create follow inbox request", err)
  19. return err
  20. }
  21. if follow == nil {
  22. return fmt.Errorf("unable to handle request")
  23. }
  24. approved := !data.GetFederationIsPrivate()
  25. followRequest := *follow
  26. if err := persistence.AddFollow(followRequest, approved); err != nil {
  27. log.Errorln("unable to save follow request", err)
  28. return err
  29. }
  30. localAccountName := data.GetDefaultFederationUsername()
  31. if approved {
  32. if err := requests.SendFollowAccept(follow.Inbox, activity, localAccountName); err != nil {
  33. log.Errorln("unable to send follow accept", err)
  34. return err
  35. }
  36. }
  37. // Save as an accepted activity
  38. actorReference := activity.GetActivityStreamsActor()
  39. object := activity.GetActivityStreamsObject()
  40. objectIRI := object.At(0).GetIRI().String()
  41. actorIRI := actorReference.At(0).GetIRI().String()
  42. // If this request is approved and we have not previously sent an action to
  43. // chat due to a previous follow request, then do so.
  44. hasPreviouslyhandled := true // Default so we don't send anything if it fails.
  45. if approved {
  46. hasPreviouslyhandled, err = persistence.HasPreviouslyHandledInboundActivity(objectIRI, actorIRI, events.FediverseEngagementFollow)
  47. if err != nil {
  48. log.Errorln("error checking for previously handled follow activity", err)
  49. }
  50. }
  51. // Save this follow action to our activities table.
  52. if err := persistence.SaveInboundFediverseActivity(objectIRI, actorIRI, events.FediverseEngagementFollow, time.Now()); err != nil {
  53. return errors.Wrap(err, "unable to save inbound share/re-post activity")
  54. }
  55. // Send action to chat if it has not been previously handled.
  56. if !hasPreviouslyhandled {
  57. return handleEngagementActivity(events.FediverseEngagementFollow, false, actorReference, events.FediverseEngagementFollow)
  58. }
  59. return nil
  60. }
  61. func handleUnfollowRequest(c context.Context, activity vocab.ActivityStreamsUndo) error {
  62. request := resolvers.MakeUnFollowRequest(c, activity)
  63. if request == nil {
  64. log.Errorf("unable to handle unfollow request")
  65. return errors.New("unable to handle unfollow request")
  66. }
  67. unfollowRequest := *request
  68. log.Traceln("unfollow request:", unfollowRequest)
  69. return persistence.RemoveFollow(unfollowRequest)
  70. }