zammad.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Image::Zammad
  3. API_HOST = 'https://images.zammad.com'.freeze
  4. OPEN_TIMEOUT = 4
  5. READ_TIMEOUT = 6
  6. TOTAL_TIMEOUT = 6
  7. DISABLE_IN_TEST_ENV = true
  8. def self.user(email)
  9. raise Exceptions::UnprocessableEntity, 'no email given' if email.blank?
  10. return if Rails.env.test? && DISABLE_IN_TEST_ENV
  11. email.downcase!
  12. return if email.match?(%r{@example.com$})
  13. # fetch image
  14. response = UserAgent.post(
  15. "#{API_HOST}/api/v1/person/image",
  16. {
  17. email: email,
  18. },
  19. {
  20. open_timeout: OPEN_TIMEOUT,
  21. read_timeout: READ_TIMEOUT,
  22. total_timeout: TOTAL_TIMEOUT,
  23. verify_ssl: true,
  24. },
  25. )
  26. if !response.success?
  27. Rails.logger.info "Can't fetch image for '#{email}' (maybe no avatar available), http code: #{response.code}"
  28. return
  29. end
  30. Rails.logger.info "Fetched image for '#{email}', http code: #{response.code}"
  31. {
  32. content: response.body,
  33. mime_type: 'image/jpeg',
  34. }
  35. end
  36. def self.organization(domain)
  37. raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank?
  38. return if Rails.env.test? && DISABLE_IN_TEST_ENV
  39. # strip, just use domain name
  40. domain = domain.sub(%r{^.+?@(.+?)$}, '\1').downcase
  41. return if domain == 'example.com'
  42. # fetch org logo
  43. response = UserAgent.post(
  44. "#{API_HOST}/api/v1/organization/image",
  45. {
  46. domain: domain
  47. },
  48. {
  49. open_timeout: OPEN_TIMEOUT,
  50. read_timeout: READ_TIMEOUT,
  51. total_timeout: TOTAL_TIMEOUT,
  52. verify_ssl: true,
  53. },
  54. )
  55. response_code = response.code
  56. if !response.success?
  57. Rails.logger.info "Can't fetch image for '#{domain}' (maybe no avatar available), http code: #{response_code}"
  58. return
  59. end
  60. Rails.logger.info "Fetched image for '#{domain}', http code: #{response_code}"
  61. {
  62. content: response.body,
  63. mime_type: 'image/png',
  64. }
  65. end
  66. def self.organization_suggest(domain)
  67. image = organization(domain)
  68. return false if !image
  69. logo_timestamp = Service::SystemAssets::ProductLogo.store_logo(image)
  70. Setting.set('product_logo', logo_timestamp)
  71. true
  72. end
  73. end