mailer.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. class NotificationFactory::Mailer
  2. =begin
  3. get notification settings for user and notification type
  4. result = NotificationFactory::Mailer.notification_settings(user, ticket, type)
  5. type: create | update | reminder_reached | escalation (escalation_warning)
  6. returns
  7. {
  8. user: user,
  9. channels: {
  10. online: true,
  11. email: true,
  12. },
  13. }
  14. =end
  15. def self.notification_settings(user, ticket, type)
  16. # map types if needed
  17. map = {
  18. 'escalation_warning' => 'escalation'
  19. }
  20. if map[type]
  21. type = map[type]
  22. end
  23. return if !user.preferences
  24. return if !user.preferences['notification_config']
  25. matrix = user.preferences['notification_config']['matrix']
  26. return if !matrix
  27. # check if group is in selecd groups
  28. if ticket.owner_id != user.id
  29. selected_group_ids = user.preferences['notification_config']['group_ids']
  30. if selected_group_ids
  31. if selected_group_ids.class == Array
  32. hit = nil
  33. if selected_group_ids.empty?
  34. hit = true
  35. elsif selected_group_ids[0] == '-' && selected_group_ids.count == 1
  36. hit = true
  37. else
  38. hit = false
  39. selected_group_ids.each { |selected_group_id|
  40. if selected_group_id.to_s == ticket.group_id.to_s
  41. hit = true
  42. break
  43. end
  44. }
  45. end
  46. return if !hit
  47. end
  48. end
  49. end
  50. return if !matrix[type]
  51. data = matrix[type]
  52. return if !data
  53. return if !data['criteria']
  54. channels = data['channel']
  55. return if !channels
  56. if data['criteria']['owned_by_me'] && ticket.owner_id == user.id
  57. return {
  58. user: user,
  59. channels: channels
  60. }
  61. end
  62. if data['criteria']['owned_by_nobody'] && ticket.owner_id == 1
  63. return {
  64. user: user,
  65. channels: channels
  66. }
  67. end
  68. return if !data['criteria']['no']
  69. {
  70. user: user,
  71. channels: channels
  72. }
  73. end
  74. =begin
  75. success = NotificationFactory::Mailer.send(
  76. recipient: User.find(123),
  77. subject: 'sime subject',
  78. body: 'some body',
  79. content_type: '', # optional, e. g. 'text/html'
  80. message_id: '<some_message_id@fqdn>', # optional
  81. references: ['message-id123', 'message-id456'], # optional
  82. attachments: [attachments...], # optional
  83. )
  84. =end
  85. def self.send(data)
  86. sender = Setting.get('notification_sender')
  87. Rails.logger.info "Send notification to: #{data[:recipient][:email]} (from #{sender})"
  88. content_type = 'text/plain'
  89. if data[:content_type]
  90. content_type = data[:content_type]
  91. end
  92. # get active Email::Outbound Channel and send
  93. channel = Channel.find_by(area: 'Email::Notification', active: true)
  94. channel.deliver(
  95. {
  96. # in_reply_to: in_reply_to,
  97. from: sender,
  98. to: data[:recipient][:email],
  99. subject: data[:subject],
  100. message_id: data[:message_id],
  101. references: data[:references],
  102. body: data[:body],
  103. content_type: content_type,
  104. attachments: data[:attachments],
  105. },
  106. true
  107. )
  108. end
  109. =begin
  110. NotificationFactory::Mailer.notification(
  111. template: 'password_reset',
  112. user: User.find(2),
  113. objects: {
  114. recipient: User.find(2),
  115. },
  116. main_object: ticket.find(123), # optional
  117. message_id: '<some_message_id@fqdn>', # optional
  118. references: ['message-id123', 'message-id456'], # optional
  119. standalone: true, # default: false - will send header & footer
  120. attachments: [attachments...], # optional
  121. )
  122. =end
  123. def self.notification(data)
  124. # get subject
  125. result = NotificationFactory::Mailer.template(
  126. template: data[:template],
  127. locale: data[:user][:preferences][:locale],
  128. objects: data[:objects],
  129. standalone: data[:standalone],
  130. )
  131. # rebuild subject
  132. if data[:main_object] && data[:main_object].respond_to?(:subject_build)
  133. result[:subject] = data[:main_object].subject_build(result[:subject])
  134. end
  135. # prepare scaling of images
  136. if result[:body]
  137. result[:body] = HtmlSanitizer.dynamic_image_size(result[:body])
  138. end
  139. NotificationFactory::Mailer.send(
  140. recipient: data[:user],
  141. subject: result[:subject],
  142. body: result[:body],
  143. content_type: 'text/html',
  144. message_id: data[:message_id],
  145. references: data[:references],
  146. attachments: data[:attachments],
  147. )
  148. end
  149. =begin
  150. get count of already sent notifications
  151. count = NotificationFactory::Mailer.already_sent?(ticket, recipient_user, type)
  152. retunes
  153. 8
  154. =end
  155. def self.already_sent?(ticket, recipient, type)
  156. result = ticket.history_get()
  157. count = 0
  158. result.each { |item|
  159. next if item['type'] != 'notification'
  160. next if item['object'] != 'Ticket'
  161. next if item['value_to'] !~ /#{recipient.email}/i
  162. next if item['value_to'] !~ /#{type}/i
  163. count += 1
  164. }
  165. count
  166. end
  167. =begin
  168. result = NotificationFactory::Mailer.template(
  169. template: 'password_reset',
  170. locale: 'en-us',
  171. objects: {
  172. recipient: User.find(2),
  173. },
  174. )
  175. result = NotificationFactory::Mailer.template(
  176. templateInline: "Invitation to \#{config.product_name} at \#{config.fqdn}",
  177. locale: 'en-us',
  178. objects: {
  179. recipient: User.find(2),
  180. },
  181. quote: true, # html quoting
  182. )
  183. only raw subject/body
  184. result = NotificationFactory::Mailer.template(
  185. template: 'password_reset',
  186. locale: 'en-us',
  187. objects: {
  188. recipient: User.find(2),
  189. },
  190. raw: true, # will not add application template
  191. standalone: true, # default: false - will send header & footer
  192. )
  193. returns
  194. {
  195. subject: 'some subject',
  196. body: 'some body',
  197. }
  198. =end
  199. def self.template(data)
  200. if data[:templateInline]
  201. return NotificationFactory::Renderer.new(data[:objects], data[:locale], data[:templateInline], data[:quote]).render
  202. end
  203. template = NotificationFactory.template_read(
  204. locale: data[:locale] || 'en',
  205. template: data[:template],
  206. format: 'html',
  207. type: 'mailer',
  208. )
  209. message_subject = NotificationFactory::Renderer.new(data[:objects], data[:locale], template[:subject], false).render
  210. message_body = NotificationFactory::Renderer.new(data[:objects], data[:locale], template[:body]).render
  211. if !data[:raw]
  212. application_template = NotificationFactory.application_template_read(
  213. format: 'html',
  214. type: 'mailer',
  215. )
  216. data[:objects][:message] = message_body
  217. data[:objects][:standalone] = data[:standalone]
  218. message_body = NotificationFactory::Renderer.new(data[:objects], data[:locale], application_template).render
  219. end
  220. {
  221. subject: message_subject,
  222. body: message_body,
  223. }
  224. end
  225. end