online_notification.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. notifications
  92. end
  93. =begin
  94. mark online notification as seen by object
  95. OnlineNotification.seen_by_object( 'Ticket', 123, user_id )
  96. =end
  97. def self.seen_by_object(object_name, o_id)
  98. object_id = ObjectLookup.by_name( object_name )
  99. notifications = OnlineNotification.where(
  100. object_lookup_id: object_id,
  101. o_id: o_id,
  102. seen: false,
  103. )
  104. notifications.each do |notification|
  105. notification.seen = true
  106. notification.save
  107. end
  108. true
  109. end
  110. =begin
  111. return all online notifications of an user with assets
  112. OnlineNotification.list_full( user )
  113. returns:
  114. list = {
  115. stream: notifications,
  116. assets: assets,
  117. }
  118. =end
  119. def self.list_full(user, limit)
  120. notifications = OnlineNotification.list(user, limit)
  121. assets = ApplicationModel.assets_of_object_list(notifications)
  122. {
  123. stream: notifications,
  124. assets: assets
  125. }
  126. end
  127. def notify_clients_after_change
  128. Sessions.send_to(
  129. user_id,
  130. {
  131. event: 'OnlineNotification::changed',
  132. data: {}
  133. }
  134. )
  135. end
  136. =begin
  137. cleanup old online notifications
  138. OnlineNotification.cleanup
  139. =end
  140. def self.cleanup
  141. OnlineNotification.where('created_at < ?', Time.zone.now - 6.months).delete_all
  142. OnlineNotification.where('seen = ? AND created_at < ?', true, Time.zone.now - 4.hours).delete_all
  143. OnlineNotification.where('seen = ? AND updated_at < ?', true, Time.zone.now - 1.hour).delete_all
  144. # notify all agents
  145. User.of_role('Agent').each {|user|
  146. Sessions.send_to(
  147. user.id,
  148. {
  149. event: 'OnlineNotification::changed',
  150. data: {}
  151. }
  152. )
  153. sleep 2 # slow down client requests
  154. }
  155. true
  156. end
  157. end