adds_metadata_general.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds certain (missing) meta data when creating articles.
  3. # This module depends on AddsMetadataOriginById to run before it.
  4. module Ticket::Article::AddsMetadataGeneral
  5. extend ActiveSupport::Concern
  6. TYPE_NO_METADATA = [
  7. 'email',
  8. 'twitter status',
  9. 'twitter direct-message',
  10. 'facebook feed post',
  11. 'facebook feed comment',
  12. 'sms',
  13. 'whatsapp message',
  14. ].freeze
  15. included do
  16. before_create :ticket_article_add_metadata_general
  17. end
  18. private
  19. def ticket_article_add_metadata_general
  20. return if !neither_importing_nor_postmaster?
  21. return if !type_uses_metadata_general?
  22. metadata_general_process_origin_by
  23. return if author.blank?
  24. metadata_general_process_from
  25. end
  26. def type_uses_metadata_general?
  27. return if type_id.blank?
  28. type = Ticket::Article::Type.lookup(id: type_id)
  29. return if TYPE_NO_METADATA.include? type.name
  30. true
  31. end
  32. def metadata_general_process_origin_by
  33. return if origin_by_id.blank?
  34. # in case the customer is using origin_by_id, force it to current session user
  35. # and set sender to Customer
  36. if !created_by.permissions?('ticket.agent')
  37. self.origin_by_id = created_by_id
  38. self.sender = Ticket::Article::Sender.lookup(name: 'Customer')
  39. end
  40. # in case origin_by is different than created_by, set sender to Customer
  41. # Customer in context of this conversation, not as a permission
  42. return if origin_by == created_by_id
  43. self.sender = Ticket::Article::Sender.lookup(name: 'Customer')
  44. end
  45. def metadata_general_process_from
  46. type = Ticket::Article::Type.lookup(id: type_id)
  47. is_customer = !author.permissions?('ticket.agent')
  48. fullname = author.fullname(email_fallback: false).presence
  49. self.from = if %w[web phone].include?(type.name) && is_customer && author.email.present?
  50. Channel::EmailBuild.recipient_line fullname, author.email
  51. else
  52. fullname
  53. end
  54. end
  55. end