mailer.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. # this cache will optimize the preference catch performance
  24. # because of the yaml deserialization its pretty slow
  25. # on many tickets you we cache it.
  26. user_preferences = Cache.get("NotificationFactory::Mailer.notification_settings::#{user.id}")
  27. if user_preferences.blank?
  28. user_preferences = user.preferences
  29. Cache.write("NotificationFactory::Mailer.notification_settings::#{user.id}", user_preferences, expires_in: 20.seconds)
  30. end
  31. return if !user_preferences
  32. return if !user_preferences['notification_config']
  33. matrix = user_preferences['notification_config']['matrix']
  34. return if !matrix
  35. owned_by_nobody = false
  36. owned_by_me = false
  37. case ticket.owner_id
  38. when 1
  39. owned_by_nobody = true
  40. when user.id
  41. owned_by_me = true
  42. else
  43. # check the replacement chain of max 10
  44. # if the current user is in it
  45. check_for = ticket.owner
  46. 10.times do
  47. replacement = check_for.out_of_office_agent
  48. break if !replacement
  49. check_for = replacement
  50. next if replacement.id != user.id
  51. owned_by_me = true
  52. break
  53. end
  54. end
  55. # check if group is in selected groups
  56. if !owned_by_me
  57. selected_group_ids = user_preferences['notification_config']['group_ids']
  58. if selected_group_ids.is_a?(Array)
  59. hit = nil
  60. if selected_group_ids.blank?
  61. hit = true
  62. elsif selected_group_ids[0] == '-' && selected_group_ids.count == 1
  63. hit = true
  64. else
  65. hit = false
  66. selected_group_ids.each do |selected_group_id|
  67. if selected_group_id.to_s == ticket.group_id.to_s
  68. hit = true
  69. break
  70. end
  71. end
  72. end
  73. return if !hit # no group access
  74. end
  75. end
  76. return if !matrix[type]
  77. data = matrix[type]
  78. return if !data
  79. return if !data['criteria']
  80. channels = data['channel']
  81. return if !channels
  82. if data['criteria']['owned_by_me'] && owned_by_me
  83. return {
  84. user: user,
  85. channels: channels
  86. }
  87. end
  88. if data['criteria']['owned_by_nobody'] && owned_by_nobody
  89. return {
  90. user: user,
  91. channels: channels
  92. }
  93. end
  94. return if !data['criteria']['no']
  95. {
  96. user: user,
  97. channels: channels
  98. }
  99. end
  100. =begin
  101. success = NotificationFactory::Mailer.send(
  102. recipient: User.find(123),
  103. subject: 'some subject',
  104. body: 'some body',
  105. content_type: '', # optional, e. g. 'text/html'
  106. message_id: '<some_message_id@fqdn>', # optional
  107. references: ['message-id123', 'message-id456'], # optional
  108. attachments: [attachments...], # optional
  109. )
  110. =end
  111. def self.send(data)
  112. raise Exceptions::UnprocessableEntity, "Unable to send mail to user with id #{data[:recipient][:id]} because there is no email available." if data[:recipient][:email].blank?
  113. sender = Setting.get('notification_sender')
  114. Rails.logger.info "Send notification to: #{data[:recipient][:email]} (from:#{sender}/subject:#{data[:subject]})"
  115. content_type = 'text/plain'
  116. if data[:content_type]
  117. content_type = data[:content_type]
  118. end
  119. # get active Email::Outbound Channel and send
  120. channel = Channel.find_by(area: 'Email::Notification', active: true)
  121. if channel.blank?
  122. Rails.logger.info "Can't find an active 'Email::Notification' channel. Canceling notification sending."
  123. return false
  124. end
  125. channel.deliver(
  126. {
  127. # in_reply_to: in_reply_to,
  128. from: sender,
  129. to: data[:recipient][:email],
  130. subject: data[:subject],
  131. message_id: data[:message_id],
  132. references: data[:references],
  133. body: data[:body],
  134. content_type: content_type,
  135. attachments: data[:attachments],
  136. },
  137. true
  138. )
  139. end
  140. =begin
  141. NotificationFactory::Mailer.notification(
  142. template: 'password_reset',
  143. user: User.find(2),
  144. objects: {
  145. recipient: User.find(2),
  146. },
  147. main_object: ticket.find(123), # optional
  148. message_id: '<some_message_id@fqdn>', # optional
  149. references: ['message-id123', 'message-id456'], # optional
  150. standalone: true, # default: false - will send header & footer
  151. attachments: [attachments...], # optional
  152. )
  153. =end
  154. def self.notification(data)
  155. # get subject
  156. result = NotificationFactory::Mailer.template(
  157. template: data[:template],
  158. locale: data[:user][:preferences][:locale],
  159. objects: data[:objects],
  160. standalone: data[:standalone],
  161. )
  162. # rebuild subject
  163. if data[:main_object].respond_to?(:subject_build)
  164. result[:subject] = data[:main_object].subject_build(result[:subject])
  165. end
  166. # prepare scaling of images
  167. if result[:body]
  168. result[:body] = HtmlSanitizer.dynamic_image_size(result[:body])
  169. end
  170. NotificationFactory::Mailer.send(
  171. recipient: data[:user],
  172. subject: result[:subject],
  173. body: result[:body],
  174. content_type: 'text/html',
  175. message_id: data[:message_id],
  176. references: data[:references],
  177. attachments: data[:attachments],
  178. )
  179. end
  180. =begin
  181. get count of already sent notifications
  182. count = NotificationFactory::Mailer.already_sent?(ticket, recipient_user, type)
  183. retunes
  184. 8
  185. =end
  186. def self.already_sent?(ticket, recipient, type)
  187. result = ticket.history_get
  188. count = 0
  189. result.each do |item|
  190. next if item['type'] != 'notification'
  191. next if item['object'] != 'Ticket'
  192. next if !item['value_to'].match?(/#{recipient.email}/i)
  193. next if !item['value_to'].match?(/#{type}/i)
  194. count += 1
  195. end
  196. count
  197. end
  198. =begin
  199. result = NotificationFactory::Mailer.template(
  200. template: 'password_reset',
  201. locale: 'en-us',
  202. timezone: 'America/Santiago',
  203. objects: {
  204. recipient: User.find(2),
  205. },
  206. )
  207. result = NotificationFactory::Mailer.template(
  208. templateInline: "Invitation to \#{config.product_name} at \#{config.fqdn}",
  209. locale: 'en-us',
  210. timezone: 'America/Santiago',
  211. objects: {
  212. recipient: User.find(2),
  213. },
  214. quote: true, # html quoting
  215. )
  216. only raw subject/body
  217. result = NotificationFactory::Mailer.template(
  218. template: 'password_reset',
  219. locale: 'en-us',
  220. timezone: 'America/Santiago',
  221. objects: {
  222. recipient: User.find(2),
  223. },
  224. raw: true, # will not add application template
  225. standalone: true, # default: false - will send header & footer
  226. )
  227. returns
  228. {
  229. subject: 'some subject',
  230. body: 'some body',
  231. }
  232. =end
  233. def self.template(data)
  234. if data[:templateInline]
  235. return NotificationFactory::Renderer.new(
  236. objects: data[:objects],
  237. locale: data[:locale],
  238. timezone: data[:timezone],
  239. template: data[:templateInline],
  240. escape: data[:quote]
  241. ).render
  242. end
  243. template = NotificationFactory.template_read(
  244. locale: data[:locale] || Locale.default,
  245. template: data[:template],
  246. format: data[:format] || 'html',
  247. type: 'mailer',
  248. )
  249. message_subject = NotificationFactory::Renderer.new(
  250. objects: data[:objects],
  251. locale: data[:locale],
  252. timezone: data[:timezone],
  253. template: template[:subject],
  254. escape: false
  255. ).render
  256. # strip off the extra newline at the end of the subject to avoid =0A suffixes (see #2726)
  257. message_subject.chomp!
  258. message_body = NotificationFactory::Renderer.new(
  259. objects: data[:objects],
  260. locale: data[:locale],
  261. timezone: data[:timezone],
  262. template: template[:body]
  263. ).render
  264. if !data[:raw]
  265. application_template = NotificationFactory.application_template_read(
  266. format: 'html',
  267. type: 'mailer',
  268. )
  269. data[:objects][:message] = message_body
  270. data[:objects][:standalone] = data[:standalone]
  271. message_body = NotificationFactory::Renderer.new(
  272. objects: data[:objects],
  273. locale: data[:locale],
  274. timezone: data[:timezone],
  275. template: application_template
  276. ).render
  277. end
  278. {
  279. subject: message_subject,
  280. body: message_body,
  281. }
  282. end
  283. end