mailer.rb 6.1 KB

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