mailer.rb 9.2 KB

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