notification.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. require 'event_buffer'
  3. require 'notification_factory'
  4. class Observer::Ticket::Notification < ActiveRecord::Observer
  5. observe :ticket, 'ticket::_article'
  6. def self.transaction
  7. # return if we run import mode
  8. return if Setting.get('import_mode')
  9. # get buffer
  10. list = EventBuffer.list
  11. # reset buffer
  12. EventBuffer.reset
  13. list.each { |event|
  14. # get current state of objects
  15. if event[:name] == 'Ticket::Article'
  16. article = Ticket::Article.lookup( :id => event[:id] )
  17. # next if article is already deleted
  18. next if !article
  19. ticket = article.ticket
  20. elsif event[:name] == 'Ticket'
  21. ticket = Ticket.lookup( :id => event[:id] )
  22. # next if ticket is already deleted
  23. next if !ticket
  24. article = ticket.articles[-1]
  25. next if !article
  26. else
  27. raise "unknown object for notification #{event[:name]}"
  28. end
  29. # send new ticket notification to agents
  30. if event[:name] == 'Ticket' && event[:type] == 'create'
  31. puts 'send new ticket notify to agent'
  32. send_notify(
  33. {
  34. :event => event,
  35. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  36. :subject => 'New Ticket (#{ticket.title})',
  37. :body => 'Hi #{recipient.firstname},
  38. a new Ticket (#{ticket.title}) via i18n(#{article.type.name}).
  39. Group: #{ticket.group.name}
  40. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  41. State: i18n(#{ticket.state.name})
  42. From: #{article.from}
  43. <snip>
  44. #{article.body}
  45. </snip>
  46. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  47. '
  48. },
  49. ticket,
  50. article
  51. )
  52. end
  53. # send new ticket notification to customers
  54. if event[:name] == 'Ticket' && event[:type] == 'create'
  55. # only for incoming emails
  56. next if article.type.name != 'email'
  57. puts 'send new ticket notify to customer'
  58. send_notify(
  59. {
  60. :event => event,
  61. :recipient => 'customer', # group|owner|to_work_on|customer
  62. :subject => 'New Ticket has been created! (#{ticket.title})',
  63. :body => 'Thanks for your email. A new ticket has been created.
  64. You wrote:
  65. <snip>
  66. #{article.body}
  67. </snip>
  68. Your email will be answered by a human ASAP
  69. Have fun with Zammad! :-)
  70. Your Zammad Team
  71. '
  72. },
  73. ticket,
  74. article
  75. )
  76. end
  77. # send follow up notification
  78. if event[:name] == 'Ticket::Article' && event[:type] == 'create'
  79. # only send article notifications after init article is created (handled by ticket create event)
  80. next if ticket.articles.count.to_i <= 1
  81. puts 'send new ticket::article notify'
  82. if article.sender.name == 'Customer'
  83. send_notify(
  84. {
  85. :event => event,
  86. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  87. :subject => 'Follow Up (#{ticket.title})',
  88. :body => 'Hi #{recipient.firstname},
  89. a follow Up (#{ticket.title}) via i18n(#{article.type.name}).
  90. Group: #{ticket.group.name}
  91. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  92. State: i18n(#{ticket.state.name})
  93. From: #{article.from}
  94. <snip>
  95. #{article.body}
  96. </snip>
  97. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  98. '
  99. },
  100. ticket,
  101. article
  102. )
  103. end
  104. # send new note notification to owner
  105. # if agent == created.id
  106. if article.sender.name == 'Agent' && article.created_by_id != article.ticket.owner_id
  107. send_notify(
  108. {
  109. :event => event,
  110. :recipient => 'owner', # group|owner|to_work_on
  111. :subject => 'Updated (#{ticket.title})',
  112. :body => 'Hi #{recipient.firstname},
  113. updated (#{ticket.title}) via i18n(#{article.type.name}).
  114. Group: #{ticket.group.name}
  115. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  116. State: i18n(#{ticket.state.name})
  117. From: #{article.from}
  118. <snip>
  119. #{article.body}
  120. </snip>
  121. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  122. '
  123. },
  124. ticket,
  125. article
  126. )
  127. end
  128. end
  129. }
  130. end
  131. def self.send_notify(data, ticket, article)
  132. # send background job
  133. params = {
  134. :ticket_id => ticket.id,
  135. :article_id => article.id,
  136. :data => data,
  137. }
  138. Delayed::Job.enqueue( Observer::Ticket::Notification::BackgroundJob.new( params ) )
  139. end
  140. def after_create(record)
  141. # return if we run import mode
  142. return if Setting.get('import_mode')
  143. # puts 'CREATED!!!!'
  144. # puts record.inspect
  145. e = {
  146. :name => record.class.name,
  147. :type => 'create',
  148. :data => record,
  149. :id => record.id,
  150. }
  151. EventBuffer.add(e)
  152. end
  153. def before_update(record)
  154. # return if we run import mode
  155. return if Setting.get('import_mode')
  156. #puts 'before_update'
  157. current = record.class.find(record.id)
  158. # do not send anything if nothing has changed
  159. return if current.attributes == record.attributes
  160. # puts 'UPDATE!!!!!!!!'
  161. # puts 'current'
  162. # puts current.inspect
  163. # puts 'record'
  164. # puts record.inspect
  165. e = {
  166. :name => record.class.name,
  167. :type => 'update',
  168. :data => record,
  169. :id => record.id,
  170. }
  171. EventBuffer.add(e)
  172. end
  173. def after_update(record)
  174. # return if we run import mode
  175. return if Setting.get('import_mode')
  176. # puts 'after_update'
  177. # puts record.inspect
  178. # puts '-----'
  179. # puts @a.inspect
  180. # AuditTrail.new(record, "UPDATED")
  181. end
  182. end