mailer.rb 7.1 KB

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