messaging.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class NotificationFactory::Messaging
  3. =begin
  4. result = NotificationFactory::Messaging.template(
  5. template: 'ticket_update',
  6. locale: 'en-us',
  7. timezone: 'Europe/Berlin',
  8. objects: {
  9. recipient: User.find(2),
  10. ticket: Ticket.find(1)
  11. },
  12. )
  13. returns
  14. {
  15. subject: 'some subject',
  16. body: 'some body',
  17. }
  18. =end
  19. def self.template(data)
  20. return render_inline(data) if data[:templateInline]
  21. messaging_template = messaging_template(data)
  22. message_body = render_template(messaging_template[:body], data)
  23. if !data[:raw]
  24. data = adjust_data(data, message_body)
  25. message_body = render_template(application_template, data)
  26. end
  27. {
  28. subject: render_template(messaging_template[:subject], data).strip!,
  29. body: message_body.strip!,
  30. }
  31. end
  32. def self.messaging_template(data)
  33. NotificationFactory.template_read(
  34. locale: data[:locale] || Locale.default,
  35. template: data[:template],
  36. format: 'md',
  37. type: 'messaging',
  38. )
  39. end
  40. def self.application_template
  41. NotificationFactory.application_template_read(
  42. format: 'md',
  43. type: 'messaging',
  44. )
  45. end
  46. def self.render_inline(data)
  47. NotificationFactory::Renderer.new(
  48. objects: data[:objects],
  49. locale: data[:locale],
  50. timezone: data[:timezone],
  51. template: data[:templateInline]
  52. ).render
  53. end
  54. def self.render_template(template, data)
  55. NotificationFactory::Renderer.new(
  56. objects: data[:objects],
  57. locale: data[:locale],
  58. timezone: data[:timezone],
  59. template: template,
  60. escape: false,
  61. trusted: true
  62. ).render
  63. end
  64. def self.adjust_data(data, message_body)
  65. data[:objects][:message] = message_body
  66. data[:objects][:standalone] = data[:standalone]
  67. data
  68. end
  69. end