webhook.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Webhook < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksHtmlSanitized
  5. include HasCollectionUpdate
  6. before_save :reset_custom_payload
  7. before_destroy Webhook::EnsureNoRelatedObjects
  8. validates :name, presence: true
  9. validate :validate_endpoint
  10. validate :validate_custom_payload
  11. validates :note, length: { maximum: 500 }
  12. sanitized_html :note
  13. store :preferences
  14. private
  15. def reset_custom_payload
  16. return true if customized_payload
  17. self.custom_payload = nil
  18. true
  19. end
  20. def validate_endpoint
  21. uri = URI.parse(endpoint)
  22. errors.add(:endpoint, __('The provided endpoint is invalid, no http or https protocol was specified.')) if !uri.is_a?(URI::HTTP)
  23. errors.add(:endpoint, __('The provided endpoint is invalid, no hostname was specified.')) if uri.host.blank?
  24. rescue URI::InvalidURIError
  25. errors.add :endpoint, __('The provided endpoint is invalid.')
  26. end
  27. def validate_custom_payload
  28. return true if custom_payload.blank?
  29. begin
  30. JSON.parse(custom_payload)
  31. rescue
  32. errors.add :custom_payload, __('The provided payload is invalid. Please check your syntax.')
  33. end
  34. true
  35. end
  36. end