notification.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Transaction::Notification
  3. =begin
  4. {
  5. object: 'Ticket',
  6. type: 'update',
  7. object_id: 123,
  8. interface_handle: 'application_server', # application_server|websocket|scheduler
  9. changes: {
  10. 'attribute1' => [before, now],
  11. 'attribute2' => [before, now],
  12. },
  13. created_at: Time.zone.now,
  14. user_id: 123,
  15. },
  16. =end
  17. def initialize(item, params = {})
  18. @item = item
  19. @params = params
  20. end
  21. def perform
  22. # return if we run import mode
  23. return if Setting.get('import_mode')
  24. return if @item[:object] != 'Ticket'
  25. return if @params[:disable_notification]
  26. ticket = Ticket.find_by(id: @item[:object_id])
  27. return if !ticket
  28. if @item[:article_id]
  29. article = Ticket::Article.find(@item[:article_id])
  30. # ignore notifications
  31. sender = Ticket::Article::Sender.lookup(id: article.sender_id)
  32. if sender&.name == 'System'
  33. return if @item[:changes].blank? && article.preferences[:notification] != true
  34. if article.preferences[:notification] != true
  35. article = nil
  36. end
  37. end
  38. end
  39. # find recipients
  40. recipients_and_channels = []
  41. recipients_reason = {}
  42. # loop through all users
  43. possible_recipients = User.group_access(ticket.group_id, 'full').sort_by(&:login)
  44. # apply owner
  45. if ticket.owner_id != 1
  46. possible_recipients.push ticket.owner
  47. recipients_reason[ticket.owner_id] = 'are assigned'
  48. end
  49. # apply out of office agents
  50. possible_recipients_additions = Set.new
  51. possible_recipients.each do |user|
  52. recursive_ooo_replacements(
  53. user: user,
  54. replacements: possible_recipients_additions,
  55. reasons: recipients_reason,
  56. )
  57. end
  58. if possible_recipients_additions.present?
  59. # join unique entries
  60. possible_recipients = possible_recipients | possible_recipients_additions.to_a
  61. end
  62. already_checked_recipient_ids = {}
  63. possible_recipients.each do |user|
  64. result = NotificationFactory::Mailer.notification_settings(user, ticket, @item[:type])
  65. next if !result
  66. next if already_checked_recipient_ids[user.id]
  67. already_checked_recipient_ids[user.id] = true
  68. recipients_and_channels.push result
  69. next if recipients_reason[user.id]
  70. recipients_reason[user.id] = 'are in group'
  71. end
  72. # send notifications
  73. recipient_list = ''
  74. recipients_and_channels.each do |item|
  75. user = item[:user]
  76. channels = item[:channels]
  77. # ignore user who changed it by him self via web
  78. if @params[:interface_handle] == 'application_server'
  79. next if article&.updated_by_id == user.id
  80. next if !article && @item[:user_id] == user.id
  81. end
  82. # ignore inactive users
  83. next if !user.active?
  84. # ignore if no changes has been done
  85. changes = human_changes(user, ticket)
  86. next if @item[:type] == 'update' && !article && changes.blank?
  87. # check if today already notified
  88. if @item[:type] == 'reminder_reached' || @item[:type] == 'escalation' || @item[:type] == 'escalation_warning'
  89. identifier = user.email
  90. if !identifier || identifier == ''
  91. identifier = user.login
  92. end
  93. already_notified = false
  94. History.list('Ticket', ticket.id).each do |history|
  95. next if history['type'] != 'notification'
  96. next if history['value_to'] !~ /\(#{Regexp.escape(@item[:type])}:/
  97. next if history['value_to'] !~ /#{Regexp.escape(identifier)}\(/
  98. next if !history['created_at'].today?
  99. already_notified = true
  100. end
  101. next if already_notified
  102. end
  103. # create online notification
  104. used_channels = []
  105. if channels['online']
  106. used_channels.push 'online'
  107. created_by_id = @item[:user_id] || 1
  108. # delete old notifications
  109. if @item[:type] == 'reminder_reached'
  110. seen = false
  111. created_by_id = 1
  112. OnlineNotification.remove_by_type('Ticket', ticket.id, @item[:type], user)
  113. elsif @item[:type] == 'escalation' || @item[:type] == 'escalation_warning'
  114. seen = false
  115. created_by_id = 1
  116. OnlineNotification.remove_by_type('Ticket', ticket.id, 'escalation', user)
  117. OnlineNotification.remove_by_type('Ticket', ticket.id, 'escalation_warning', user)
  118. # on updates without state changes create unseen messages
  119. elsif @item[:type] != 'create' && (@item[:changes].blank? || @item[:changes]['state_id'].blank?)
  120. seen = false
  121. else
  122. seen = ticket.online_notification_seen_state(user.id)
  123. end
  124. OnlineNotification.add(
  125. type: @item[:type],
  126. object: 'Ticket',
  127. o_id: ticket.id,
  128. seen: seen,
  129. created_by_id: created_by_id,
  130. user_id: user.id,
  131. )
  132. Rails.logger.debug { "sent ticket online notifiaction to agent (#{@item[:type]}/#{ticket.id}/#{user.email})" }
  133. end
  134. # ignore email channel notificaiton and empty emails
  135. if !channels['email'] || !user.email || user.email == ''
  136. add_recipient_list(ticket, user, used_channels, @item[:type])
  137. next
  138. end
  139. used_channels.push 'email'
  140. add_recipient_list(ticket, user, used_channels, @item[:type])
  141. # get user based notification template
  142. # if create, send create message / block update messages
  143. template = nil
  144. if @item[:type] == 'create'
  145. template = 'ticket_create'
  146. elsif @item[:type] == 'update'
  147. template = 'ticket_update'
  148. elsif @item[:type] == 'reminder_reached'
  149. template = 'ticket_reminder_reached'
  150. elsif @item[:type] == 'escalation'
  151. template = 'ticket_escalation'
  152. elsif @item[:type] == 'escalation_warning'
  153. template = 'ticket_escalation_warning'
  154. else
  155. raise "unknown type for notification #{@item[:type]}"
  156. end
  157. current_user = User.lookup(id: @item[:user_id])
  158. if !current_user
  159. current_user = User.lookup(id: 1)
  160. end
  161. attachments = []
  162. if article
  163. attachments = article.attachments_inline
  164. end
  165. NotificationFactory::Mailer.notification(
  166. template: template,
  167. user: user,
  168. objects: {
  169. ticket: ticket,
  170. article: article,
  171. recipient: user,
  172. current_user: current_user,
  173. changes: changes,
  174. reason: recipients_reason[user.id],
  175. },
  176. message_id: "<notification.#{DateTime.current.to_s(:number)}.#{ticket.id}.#{user.id}.#{rand(999_999)}@#{Setting.get('fqdn')}>",
  177. references: ticket.get_references,
  178. main_object: ticket,
  179. attachments: attachments,
  180. )
  181. Rails.logger.debug { "sent ticket email notifiaction to agent (#{@item[:type]}/#{ticket.id}/#{user.email})" }
  182. end
  183. end
  184. def add_recipient_list(ticket, user, channels, type)
  185. return if channels.blank?
  186. identifier = user.email
  187. if !identifier || identifier == ''
  188. identifier = user.login
  189. end
  190. recipient_list = "#{identifier}(#{type}:#{channels.join(',')})"
  191. History.add(
  192. o_id: ticket.id,
  193. history_type: 'notification',
  194. history_object: 'Ticket',
  195. value_to: recipient_list,
  196. created_by_id: @item[:user_id] || 1
  197. )
  198. end
  199. def human_changes(user, record)
  200. return {} if !@item[:changes]
  201. locale = user.preferences[:locale] || Setting.get('locale_default') || 'en-us'
  202. # only show allowed attributes
  203. attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
  204. #puts "AL #{attribute_list.inspect}"
  205. user_related_changes = {}
  206. @item[:changes].each do |key, value|
  207. # if no config exists, use all attributes
  208. if attribute_list.blank?
  209. user_related_changes[key] = value
  210. # if config exists, just use existing attributes for user
  211. elsif attribute_list[key.to_s]
  212. user_related_changes[key] = value
  213. end
  214. end
  215. changes = {}
  216. user_related_changes.each do |key, value|
  217. # get attribute name
  218. attribute_name = key.to_s
  219. object_manager_attribute = attribute_list[attribute_name]
  220. if attribute_name[-3, 3] == '_id'
  221. attribute_name = attribute_name[ 0, attribute_name.length - 3 ].to_s
  222. end
  223. # add item to changes hash
  224. if key.to_s == attribute_name
  225. changes[attribute_name] = value
  226. end
  227. # if changed item is an _id field/reference, do an lookup for the realy values
  228. value_id = []
  229. value_str = [ value[0], value[1] ]
  230. if key.to_s[-3, 3] == '_id'
  231. value_id[0] = value[0]
  232. value_id[1] = value[1]
  233. if record.respond_to?(attribute_name) && record.send(attribute_name)
  234. relation_class = record.send(attribute_name).class
  235. if relation_class && value_id[0]
  236. relation_model = relation_class.lookup(id: value_id[0])
  237. if relation_model
  238. if relation_model['name']
  239. value_str[0] = relation_model['name']
  240. elsif relation_model.respond_to?('fullname')
  241. value_str[0] = relation_model.send('fullname')
  242. end
  243. end
  244. end
  245. if relation_class && value_id[1]
  246. relation_model = relation_class.lookup(id: value_id[1])
  247. if relation_model
  248. if relation_model['name']
  249. value_str[1] = relation_model['name']
  250. elsif relation_model.respond_to?('fullname')
  251. value_str[1] = relation_model.send('fullname')
  252. end
  253. end
  254. end
  255. end
  256. end
  257. # check if we have an dedcated display name for it
  258. display = attribute_name
  259. if object_manager_attribute && object_manager_attribute[:display]
  260. # delete old key
  261. changes.delete(display)
  262. # set new key
  263. display = object_manager_attribute[:display].to_s
  264. end
  265. changes[display] = if object_manager_attribute && object_manager_attribute[:translate]
  266. from = Translation.translate(locale, value_str[0])
  267. to = Translation.translate(locale, value_str[1])
  268. [from, to]
  269. else
  270. [value_str[0].to_s, value_str[1].to_s]
  271. end
  272. end
  273. changes
  274. end
  275. private
  276. def recursive_ooo_replacements(user:, replacements:, reasons:, level: 0)
  277. if level == 10
  278. Rails.logger.warn("Found more than 10 replacement levels for agent #{user}.")
  279. return
  280. end
  281. replacement = user.out_of_office_agent
  282. return if !replacement
  283. # return for already found, added and checked users
  284. # to prevent re-doing complete lookup paths
  285. return if !replacements.add?(replacement)
  286. reasons[replacement.id] = 'are the out-of-office replacement of the owner'
  287. recursive_ooo_replacements(
  288. user: replacement,
  289. replacements: replacements,
  290. reasons: reasons,
  291. level: level + 1
  292. )
  293. end
  294. end