notification_factory.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. module NotificationFactory
  2. =begin
  3. result_string = NotificationFactory.build(
  4. string: 'Hi #{recipient.firstname},',
  5. objects: {
  6. ticket : ticket,
  7. recipient: User.find(2),
  8. },
  9. locale: 'en',
  10. )
  11. =end
  12. def self.build(data)
  13. data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /xm ) { |placeholder|
  14. # store possible callback to work with
  15. # and check if it's valid for execution
  16. original_string = $&
  17. callback = $1
  18. object_name = nil
  19. object_method = nil
  20. if callback =~ /\A ( [\w]+ )\.( [\w\.]+ ) \z/x
  21. object_name = $1
  22. object_method = $2
  23. end
  24. # do validaton, ignore some methodes
  25. if callback =~ /(`|\.(|\s*)(save|destroy|delete|remove|drop|update\(|update_att|create\(|new|all|where|find))/i
  26. placeholder = "#{original_string} (not allowed)"
  27. # get value based on object_name and object_method
  28. elsif object_name && object_method
  29. # use config params
  30. if object_name == 'config'
  31. placeholder = Setting.get(object_method)
  32. # if object_name dosn't exist
  33. elsif !data[:objects][object_name.to_sym]
  34. placeholder = "\#{#{object_name} / no such object}"
  35. else
  36. value = nil
  37. object_refs = data[:objects][object_name.to_sym]
  38. object_methods = object_method.split('.')
  39. object_methods_s = ''
  40. object_methods.each {|method|
  41. if object_methods_s != ''
  42. object_methods_s += '.'
  43. end
  44. object_methods_s += method
  45. # if method exists
  46. if !object_refs.respond_to?( method.to_sym )
  47. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  48. break
  49. end
  50. object_refs = object_refs.send( method.to_sym )
  51. # add body quote
  52. next if object_name != 'article'
  53. next if method != 'body'
  54. next if data[:objects][:article].content_type != 'text/html'
  55. object_refs = object_refs.html2text.chomp
  56. }
  57. if !value
  58. placeholder = object_refs
  59. else
  60. placeholder = value
  61. end
  62. end
  63. end
  64. placeholder
  65. }
  66. # translate
  67. data[:string].gsub!( /i18n\((|.+?)\)/ ) {
  68. string = $1
  69. locale = data[:locale] || 'en'
  70. Translation.translate( locale, string )
  71. }
  72. data[:string]
  73. end
  74. =begin
  75. success = NotificationFactory.send(
  76. recipient: User.find(123),
  77. subject: 'sime subject',
  78. body: 'some body',
  79. content_type: '', # optional, e. g. 'text/html'
  80. )
  81. =end
  82. def self.send(data)
  83. sender = Setting.get('notification_sender')
  84. Rails.logger.info "Send notification to: #{data[:recipient][:email]} (from #{sender})"
  85. content_type = 'text/plain'
  86. if data[:content_type]
  87. content_type = data[:content_type]
  88. end
  89. # get active Email::Outbound Channel and send
  90. channel = Channel.find_by(area: 'Email::Notification', active: true)
  91. channel.deliver(
  92. {
  93. # in_reply_to: in_reply_to,
  94. from: sender,
  95. to: data[:recipient][:email],
  96. subject: data[:subject],
  97. body: data[:body],
  98. content_type: content_type,
  99. },
  100. true
  101. )
  102. end
  103. end