creates_ticket_articles.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. module CreatesTicketArticles
  2. extend ActiveSupport::Concern
  3. private
  4. def article_create(ticket, params)
  5. # create article if given
  6. form_id = params[:form_id]
  7. params.delete(:form_id)
  8. # check min. params
  9. raise Exceptions::UnprocessableEntity, 'Need at least article: { body: "some text" }' if !params[:body]
  10. # fill default values
  11. if params[:type_id].empty? && params[:type].empty?
  12. params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
  13. end
  14. if params[:sender_id].empty? && params[:sender].empty?
  15. sender = 'Customer'
  16. if current_user.permissions?('ticket.agent')
  17. sender = 'Agent'
  18. end
  19. params[:sender_id] = Ticket::Article::Sender.lookup(name: sender).id
  20. end
  21. # remember time accounting
  22. time_unit = params[:time_unit]
  23. clean_params = Ticket::Article.association_name_to_id_convert(params)
  24. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  25. # overwrite params
  26. if !current_user.permissions?('ticket.agent')
  27. clean_params[:sender_id] = Ticket::Article::Sender.lookup(name: 'Customer').id
  28. clean_params.delete(:sender)
  29. clean_params.delete(:origin_by_id)
  30. type = Ticket::Article::Type.lookup(id: clean_params[:type_id])
  31. if type.name !~ /^(note|web)$/
  32. clean_params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
  33. end
  34. clean_params.delete(:type)
  35. clean_params[:internal] = false
  36. end
  37. article = Ticket::Article.new(clean_params)
  38. article.ticket_id = ticket.id
  39. # store dataurl images to store
  40. attachments_inline = []
  41. if article.body && article.content_type =~ %r{text/html}i
  42. (article.body, attachments_inline) = HtmlSanitizer.replace_inline_images(article.body, ticket.id)
  43. end
  44. # find attachments in upload cache
  45. if form_id
  46. article.attachments = Store.list(
  47. object: 'UploadCache',
  48. o_id: form_id,
  49. )
  50. end
  51. article.save!
  52. # store inline attachments
  53. attachments_inline.each { |attachment|
  54. Store.add(
  55. object: 'Ticket::Article',
  56. o_id: article.id,
  57. data: attachment[:data],
  58. filename: attachment[:filename],
  59. preferences: attachment[:preferences],
  60. )
  61. }
  62. # add attachments as param
  63. if params[:attachments]
  64. params[:attachments].each_with_index { |attachment, index|
  65. # validation
  66. ['mime-type', 'filename', 'data'].each { |key|
  67. next if attachment[key]
  68. raise Exceptions::UnprocessableEntity, "Attachment needs '#{key}' param for attachment with index '#{index}'"
  69. }
  70. preferences = {}
  71. ['charset', 'mime-type'].each { |key|
  72. next if !attachment[key]
  73. store_key = key.tr('-', '_').camelize.gsub(/(.+)([A-Z])/, '\1_\2').tr('_', '-')
  74. preferences[store_key] = attachment[key]
  75. }
  76. if attachment[:data] !~ %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$}
  77. raise Exceptions::UnprocessableEntity, "Invalid base64 for attachment with index '#{index}'"
  78. end
  79. Store.add(
  80. object: 'Ticket::Article',
  81. o_id: article.id,
  82. data: Base64.decode64(attachment[:data]),
  83. filename: attachment[:filename],
  84. preferences: preferences,
  85. )
  86. }
  87. end
  88. # account time
  89. if time_unit.present?
  90. Ticket::TimeAccounting.create!(
  91. ticket_id: article.ticket_id,
  92. ticket_article_id: article.id,
  93. time_unit: time_unit
  94. )
  95. end
  96. return article if !form_id
  97. # remove attachments from upload cache
  98. Store.remove(
  99. object: 'UploadCache',
  100. o_id: form_id,
  101. )
  102. article
  103. end
  104. end