chat.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package inbox
  2. import (
  3. "fmt"
  4. "github.com/go-fed/activity/streams/vocab"
  5. "github.com/owncast/owncast/activitypub/resolvers"
  6. "github.com/owncast/owncast/core/chat"
  7. "github.com/owncast/owncast/core/chat/events"
  8. "github.com/owncast/owncast/core/data"
  9. )
  10. func handleEngagementActivity(eventType events.EventType, isLiveNotification bool, actorReference vocab.ActivityStreamsActorProperty, action string) error {
  11. // Do nothing if displaying engagement actions has been turned off.
  12. if !data.GetFederationShowEngagement() {
  13. return nil
  14. }
  15. // Do nothing if chat is disabled
  16. if data.GetChatDisabled() {
  17. return nil
  18. }
  19. // Get actor of the action
  20. actor, _ := resolvers.GetResolvedActorFromActorProperty(actorReference)
  21. // Send chat message
  22. actorName := actor.Name
  23. if actorName == "" {
  24. actorName = actor.Username
  25. }
  26. actorIRI := actorReference.Begin().GetIRI().String()
  27. userPrefix := fmt.Sprintf("%s ", actorName)
  28. var suffix string
  29. if isLiveNotification && action == events.FediverseEngagementLike {
  30. suffix = "liked that this stream went live."
  31. } else if action == events.FediverseEngagementLike {
  32. suffix = fmt.Sprintf("liked a post from %s.", data.GetServerName())
  33. } else if isLiveNotification && action == events.FediverseEngagementRepost {
  34. suffix = "shared this stream with their followers."
  35. } else if action == events.FediverseEngagementRepost {
  36. suffix = fmt.Sprintf("shared a post from %s.", data.GetServerName())
  37. } else if action == events.FediverseEngagementFollow {
  38. suffix = "followed this stream."
  39. } else {
  40. return fmt.Errorf("could not handle event for sending to chat: %s", action)
  41. }
  42. body := fmt.Sprintf("%s %s", userPrefix, suffix)
  43. var image *string
  44. if actor.Image != nil {
  45. s := actor.Image.String()
  46. image = &s
  47. }
  48. if err := chat.SendFediverseAction(eventType, actor.FullUsername, image, body, actorIRI); err != nil {
  49. return err
  50. }
  51. return nil
  52. }