attachments.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Ticket::Attachments < BaseQuery
  4. description 'Fetch ticket attachments by ticket ID'
  5. argument :ticket_id, GraphQL::Types::ID, loads: Gql::Types::TicketType, description: 'The ticket to fetch attachments for'
  6. type [Gql::Types::StoredFileType, { null: false }], null: false
  7. def resolve(ticket:)
  8. articles = Service::Ticket::Article::List
  9. .new(current_user: context.current_user)
  10. .execute(ticket:)
  11. return [] if articles.blank?
  12. inline_attachments = articles.map { |x| x.attachments_inline.map(&:id) }.flatten.uniq
  13. articles
  14. .map(&:attachments)
  15. .flatten
  16. .reject { |f| inline_attachment?(inline_attachments, f) || original_format?(f) }
  17. .uniq(&:store_file_id)
  18. .sort_by(&:created_at).reverse
  19. end
  20. private
  21. def inline_attachment?(inline_attachments, file)
  22. inline_attachments.include?(file.id)
  23. end
  24. def original_format?(file)
  25. return false if file.preferences.blank?
  26. return false if !file.preferences.key?('original-format')
  27. return false if file.preferences['original-format'].blank?
  28. file.preferences['original-format']
  29. end
  30. end
  31. end