email_forward_reply.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Ticket::Article::EmailForwardReply < BaseMutation
  4. description 'Prepare for a new forward or reply email article'
  5. argument :article_id, GraphQL::Types::ID, loads: Gql::Types::Ticket::ArticleType, description: 'The article to be forwarded or replied to'
  6. argument :form_id, Gql::Types::FormIdType, 'Form identifier of the form for the new article to copy attachments to'
  7. field :quotable_from, String, description: "'From' information of the original email to be inserted in the quoted email block"
  8. field :quotable_to, String, description: "'To' information of the original email to be inserted in the quoted email block"
  9. field :quotable_cc, String, description: "'Cc' information of the original email to be inserted in the quoted email block"
  10. field :attachments, [Gql::Types::StoredFileType, { null: false }], null: false, description: 'Cloned attachments for the new article.'
  11. def resolve(article:, form_id:)
  12. result = { attachments: clone_attachments(article:, form_id:) }
  13. return result if !Setting.get('ui_ticket_zoom_article_email_full_quote_header')
  14. result.merge(
  15. {
  16. quotable_from: from(article),
  17. quotable_to: to(article),
  18. quotable_cc: cc(article),
  19. }
  20. )
  21. end
  22. def from(article)
  23. [
  24. ::User.find_by(id: article.origin_by_id || article.created_by_id),
  25. find_user_by_raw_email(article.from)
  26. ].compact.each do |user|
  27. result = filtered_user_info(user)
  28. return result if result.present?
  29. end
  30. nil
  31. end
  32. def to(article)
  33. %i[to_email_web to_customer to_agent to_default].each do |func|
  34. result = send(func, article)
  35. return result if result.present?
  36. end
  37. nil
  38. end
  39. def to_email_web(article)
  40. return if article.type.name != 'email' && article.type.name != 'web'
  41. filtered_user_info(find_user_by_raw_email(article.to))
  42. end
  43. def to_customer(article)
  44. return if article.sender.name != 'Customer' || article.type.name != 'phone'
  45. group = Group.find_by(name: find_user_by_raw_email(article.to))
  46. return article.to if !group
  47. ::Channel::EmailBuild.recipient_line(group.fullname, group.email)
  48. end
  49. def to_agent(article)
  50. return if article.sender.name != 'Agent' || article.type.name != 'phone'
  51. customer = ::User.find_by(id: article.ticket.customer_id)
  52. filtered_user_info(customer) || filtered_user_info(find_user_by_raw_email(article.to))
  53. end
  54. def to_default(article)
  55. article.to
  56. end
  57. def cc(article)
  58. filtered_user_info(find_user_by_raw_email(article.cc))
  59. end
  60. def clone_attachments(article:, form_id:)
  61. article.clone_attachments('UploadCache', form_id, only_attached_attachments: true)
  62. end
  63. private
  64. def find_user_by_raw_email(string)
  65. if string =~ %r{<?(\S+@\S[^>]+)(>?)}
  66. return ::User.find_by(email: $1)
  67. end
  68. nil
  69. end
  70. def filtered_user_info(user)
  71. return if !user
  72. if !user.permissions?('ticket.agent') && user.email
  73. ::Channel::EmailBuild.recipient_line(user.fullname, user.email)
  74. else
  75. user.fullname.presence
  76. end
  77. end
  78. end
  79. end