online_notification.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class OnlineNotification < ApplicationModel
  3. belongs_to :type_lookup, class_name: 'TypeLookup'
  4. belongs_to :object_lookup, class_name: 'ObjectLookup'
  5. belongs_to :user
  6. after_create :notify_clients_after_change
  7. after_update :notify_clients_after_change
  8. after_destroy :notify_clients_after_change
  9. =begin
  10. add a new online notification for this user
  11. OnlineNotification.add(
  12. :type => 'Assigned to you',
  13. :object => 'Ticket',
  14. :o_id => ticket.id,
  15. :seen => false,
  16. :created_by_id => 1,
  17. :user_id => 2,
  18. )
  19. =end
  20. def self.add(data)
  21. # lookups
  22. if data[:type]
  23. type_id = TypeLookup.by_name( data[:type] )
  24. end
  25. if data[:object]
  26. object_id = ObjectLookup.by_name( data[:object] )
  27. end
  28. record = {
  29. o_id: data[:o_id],
  30. object_lookup_id: object_id,
  31. type_lookup_id: type_id,
  32. seen: data[:seen],
  33. user_id: data[:user_id],
  34. created_by_id: data[:created_by_id]
  35. }
  36. OnlineNotification.create(record)
  37. end
  38. =begin
  39. mark online notification as seen
  40. OnlineNotification.seen(
  41. :id => 2,
  42. )
  43. =end
  44. def self.seen(data)
  45. notification = OnlineNotification.find(data[:id])
  46. notification.seen = true
  47. notification.save
  48. end
  49. =begin
  50. remove whole online notifications of an object
  51. OnlineNotification.remove( 'Ticket', 123 )
  52. =end
  53. def self.remove( object_name, o_id )
  54. object_id = ObjectLookup.by_name( object_name )
  55. OnlineNotification.where(
  56. object_lookup_id: object_id,
  57. o_id: o_id,
  58. ).destroy_all
  59. end
  60. =begin
  61. return all online notifications of an user
  62. notifications = OnlineNotification.list( user, limit )
  63. =end
  64. def self.list(user, limit)
  65. notifications = OnlineNotification.where(user_id: user.id)
  66. .order( 'created_at DESC, id DESC' )
  67. .limit( limit )
  68. list = []
  69. notifications.each do |item|
  70. data = item.attributes
  71. data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
  72. data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
  73. data.delete('object_lookup_id')
  74. data.delete('type_lookup_id')
  75. list.push data
  76. end
  77. list
  78. end
  79. =begin
  80. return all online notifications of an object
  81. notifications = OnlineNotification.list_by_object( 'Ticket', 123 )
  82. =end
  83. def self.list_by_object( object_name, o_id)
  84. object_id = ObjectLookup.by_name( object_name )
  85. notifications = OnlineNotification.where(
  86. object_lookup_id: object_id,
  87. o_id: o_id,
  88. )
  89. .order( 'created_at DESC, id DESC' ) # rubocop:disable Style/MultilineOperationIndentation
  90. .limit( 10_000 ) # rubocop:disable Style/MultilineOperationIndentation
  91. list = []
  92. notifications.each do |item|
  93. data = item.attributes
  94. data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
  95. data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
  96. data.delete('object_lookup_id')
  97. data.delete('type_lookup_id')
  98. list.push data
  99. end
  100. list
  101. end
  102. =begin
  103. mark online notification as seen by object
  104. OnlineNotification.seen_by_object( 'Ticket', 123 )
  105. =end
  106. def self.seen_by_object(object_name, o_id)
  107. object_id = ObjectLookup.by_name( object_name )
  108. notifications = OnlineNotification.where(
  109. object_lookup_id: object_id,
  110. o_id: o_id,
  111. seen: false,
  112. )
  113. notifications.each do |notification|
  114. notification.seen = true
  115. notification.save
  116. end
  117. true
  118. end
  119. =begin
  120. return all online notifications of an user with assets
  121. OnlineNotification.list_full( user )
  122. returns:
  123. list = {
  124. :stream => notifications,
  125. :assets => assets
  126. }
  127. =end
  128. def self.list_full(user, limit)
  129. notifications = OnlineNotification.list(user, limit)
  130. assets = ApplicationModel.assets_of_object_list(notifications)
  131. {
  132. stream: notifications,
  133. assets: assets
  134. }
  135. end
  136. def notify_clients_after_change
  137. Sessions.send_to(
  138. user_id,
  139. {
  140. event: 'OnlineNotification::changed',
  141. data: {}
  142. }
  143. )
  144. end
  145. =begin
  146. cleanup old online notifications
  147. OnlineNotification.cleanup
  148. =end
  149. def self.cleanup
  150. OnlineNotification.where('created_at < ?', Time.zone.now - 12.months).delete_all
  151. OnlineNotification.where('seen = ? AND created_at < ?', true, Time.zone.now - 4.hours).delete_all
  152. OnlineNotification.where('seen = ? AND updated_at < ?', true, Time.zone.now - 1.hours).delete_all
  153. # notify all agents
  154. User.of_role('Agent').each {|user|
  155. Sessions.send_to(
  156. user.id,
  157. {
  158. event: 'OnlineNotification::changed',
  159. data: {}
  160. }
  161. )
  162. sleep 2 # slow down client requests
  163. }
  164. true
  165. end
  166. end