webhooks.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package webhooks
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/owncast/owncast/core/data"
  6. "github.com/owncast/owncast/core/user"
  7. "github.com/owncast/owncast/models"
  8. )
  9. // WebhookEvent represents an event sent as a webhook.
  10. type WebhookEvent struct {
  11. EventData interface{} `json:"eventData,omitempty"`
  12. Type models.EventType `json:"type"` // messageSent | userJoined | userNameChange
  13. }
  14. // WebhookChatMessage represents a single chat message sent as a webhook payload.
  15. type WebhookChatMessage struct {
  16. User *user.User `json:"user,omitempty"`
  17. Timestamp *time.Time `json:"timestamp,omitempty"`
  18. Body string `json:"body,omitempty"`
  19. RawBody string `json:"rawBody,omitempty"`
  20. ID string `json:"id,omitempty"`
  21. ClientID uint `json:"clientId,omitempty"`
  22. Visible bool `json:"visible"`
  23. }
  24. // SendEventToWebhooks will send a single webhook event to all webhook destinations.
  25. func SendEventToWebhooks(payload WebhookEvent) {
  26. sendEventToWebhooks(payload, nil)
  27. }
  28. func sendEventToWebhooks(payload WebhookEvent, wg *sync.WaitGroup) {
  29. webhooks := data.GetWebhooksForEvent(payload.Type)
  30. for _, webhook := range webhooks {
  31. // Use wg to track the number of notifications to be sent.
  32. if wg != nil {
  33. wg.Add(1)
  34. }
  35. addToQueue(webhook, payload, wg)
  36. }
  37. }