shared_draft_zoom.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::SharedDraftZoom < ApplicationModel
  3. include HasRichText
  4. include HasDefaultModelUserRelations
  5. include CanCloneAttachments
  6. include ChecksClientNotification
  7. include HasHistory
  8. belongs_to :ticket, touch: true
  9. store :new_article
  10. store :ticket_attributes
  11. history_attributes_ignored :new_article,
  12. :ticket_attributes
  13. # required by CanCloneAttachments
  14. def content_type
  15. 'text/html'
  16. end
  17. # Process inline images
  18. has_rich_text :body
  19. # has_rich_text cannot process data inside hashes
  20. # Using a meta attribute instead
  21. def body
  22. new_article[:body]
  23. end
  24. # has_rich_text cannot process data inside hashes
  25. # Using a meta attribute instead
  26. def body=(input)
  27. new_article[:body] = input
  28. end
  29. # Adds backwards compatibility for the old desktop app
  30. def body_with_base64
  31. scrubber = HtmlSanitizer::Scrubber::InsertInlineImages.new(attachments)
  32. sanitized = Loofah
  33. .fragment(body)
  34. .scrub!(scrubber)
  35. sanitized.to_s
  36. end
  37. # Returns images with src=/api/v1/attachments/1337
  38. def content_with_body_urls
  39. # TODO: new_article + ticket_attributes must be put together.
  40. output = new_article.deep_dup
  41. output[:body] = body_with_urls
  42. output
  43. end
  44. # Returns content prepared to be applied to the ticket
  45. #
  46. # @param form_id [String] id of the form to attach to
  47. def content_with_form_id_body_urls(form_id)
  48. cache = UploadCache.new(form_id)
  49. article = new_article.deep_dup
  50. article[:body] = HasRichText.insert_urls(article[:body], cache.attachments)
  51. {
  52. article: article,
  53. ticket: ticket_attributes,
  54. }
  55. end
  56. def history_log_attributes
  57. {
  58. related_o_id: self['ticket_id'],
  59. related_history_object: 'Ticket',
  60. }
  61. end
  62. def history_destroy
  63. history_log('removed', created_by_id)
  64. end
  65. def attributes_with_association_ids
  66. attrs = super
  67. attrs.delete 'body'
  68. attrs['new_article']['body'] = body_with_base64 if attrs.dig('new_article', 'body').present?
  69. attrs
  70. end
  71. end