whatsapp_message.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Validations::TicketArticleValidator
  3. class WhatsappMessage < Backend
  4. MATCHING_TYPES = ['whatsapp message'].freeze
  5. CONTENT_TYPE_OPTIONS = [
  6. { size: 16 * 1024 * 1024, identifier: :audio, label: __('Audio file'), no_caption: true },
  7. { size: 5 * 1024 * 1024, identifier: :image, label: __('Image file') },
  8. { size: 16 * 1024 * 1024, identifier: :video, label: __('Video file') },
  9. { size: 500 * 1024, identifier: :sticker, label: __('Sticker file'), no_caption: true },
  10. { size: 100 * 1024 * 1024, identifier: :document, label: __('Document file') },
  11. ].freeze
  12. def validate_attachments_limit
  13. return if !all_attachments.many?
  14. @record.errors.add :base, format(__('Only %s attachment allowed'), 1)
  15. end
  16. def validate_attachment_content_type
  17. return if !attachment
  18. return if attachment_options
  19. message = format(__('File format is not allowed: %s'), attachment.preferences['Content-Type'] || attachment.preferences['Mime-Type'])
  20. @record.errors.add :base, message
  21. end
  22. def validate_attachments_size
  23. return if !attachment_options
  24. attachment_size = attachment.size.to_i
  25. return if attachment_size <= attachment_options[:size]
  26. size = ActiveSupport::NumberHelper.number_to_human_size attachment_options[:size]
  27. message = format(__('File is too big. %s has to be %s or smaller.'), attachment_options[:label], size)
  28. @record.errors.add :base, message
  29. end
  30. def validate_body
  31. return if attachment
  32. return if @record.body.present?
  33. @record.errors.add :base, __('Text or attachment is required')
  34. end
  35. def validate_body_no_caption
  36. return if !attachment_options&.dig(:no_caption)
  37. return if @record.body.blank?
  38. message = "#{attachment_options[:label]} is sent without text caption"
  39. @record.errors.add :base, message
  40. end
  41. def validate_ticket_state
  42. return if Ticket::State.where(name: %w[closed merged removed]).pluck(:id).exclude?(@record.ticket.state_id)
  43. @record.errors.add :base, __('Reply allowed only for open tickets')
  44. end
  45. private
  46. def attachment
  47. return @attachment if defined?(@attachment)
  48. @attachment = all_attachments.first
  49. end
  50. def attachment_options
  51. return @attachment_options if defined?(@attachment_options)
  52. return if !attachment
  53. attachment_type = attachment.preferences['Content-Type'] || attachment.preferences['Mime-Type']
  54. @attachment_options = CONTENT_TYPE_OPTIONS.find do |elem|
  55. Whatsapp::Outgoing::Message::Media::SUPPORTED_MEDIA_TYPES[elem[:identifier]]
  56. .include? attachment_type
  57. end
  58. end
  59. def all_attachments
  60. @all_attachments ||= @record.attachments + (@record.instance_variable_get(:@attachments_buffer) || [])
  61. end
  62. def validator_applies?
  63. sender = Ticket::Article::Sender.lookup id: @record.sender_id
  64. sender.name == 'Agent'
  65. end
  66. end
  67. end