shared_draft_start.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::SharedDraftStart < ApplicationModel
  3. include HasRichText
  4. include HasDefaultModelUserRelations
  5. include CanCloneAttachments
  6. include ChecksClientNotification
  7. belongs_to :group
  8. validates :name, presence: true
  9. before_validation :clear_group_id
  10. after_commit :trigger_subscriptions
  11. store :content
  12. # don't include content into assets which may be huge
  13. # assets are used to load the whole list of available drafts
  14. # content is loaded separately
  15. def filter_attributes(attributes)
  16. super.except! 'content'
  17. end
  18. # required by CanCloneAttachments
  19. def content_type
  20. 'text/html'
  21. end
  22. # Process inline images
  23. has_rich_text :body
  24. # has_rich_text cannot process data inside hashes
  25. # Using a meta attribute instead
  26. def body
  27. content[:body]
  28. end
  29. # has_rich_text cannot process data inside hashes
  30. # Using a meta attribute instead
  31. def body=(input)
  32. content[:body] = input
  33. end
  34. # Adds backwards compatibility for the old desktop app
  35. def body_with_base64
  36. scrubber = HtmlSanitizer::Scrubber::InsertInlineImages.new(attachments)
  37. sanitized = Loofah
  38. .fragment(body)
  39. .scrub!(scrubber)
  40. sanitized.to_s
  41. end
  42. # Adds backwards compatibility for the old desktop app
  43. def content_with_base64
  44. output = content.deep_dup
  45. output[:body] = body_with_base64
  46. output
  47. end
  48. # Returns images with src=/api/v1/attachments/1337
  49. def content_with_body_urls
  50. output = content.deep_dup
  51. output[:body] = body_with_urls
  52. output
  53. end
  54. # Returns content prepared to be applied to the ticket
  55. #
  56. # @param form_id [String] id of the form to attach to
  57. def content_with_form_id_body_urls(form_id)
  58. cache = UploadCache.new(form_id)
  59. output = content.deep_dup
  60. output[:body] = HasRichText.insert_urls(output[:body], cache.attachments)
  61. output
  62. end
  63. private
  64. def clear_group_id
  65. content.delete :group_id
  66. end
  67. def trigger_subscriptions
  68. [group_id, group_id_previously_was]
  69. .compact
  70. .uniq
  71. .each do |elem|
  72. Gql::Subscriptions::Ticket::SharedDraft::Start::UpdateByGroup
  73. .trigger(nil, arguments: { group_id: Gql::ZammadSchema.id_from_internal_id('Group', elem) })
  74. end
  75. end
  76. end