online_notification.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. after_create :notify_clients_after_change
  6. after_update :notify_clients_after_change
  7. after_destroy :notify_clients_after_change
  8. =begin
  9. add a new online notification for this user
  10. OnlineNotification.add(
  11. :type => 'Assigned to you',
  12. :object => 'Ticket',
  13. :o_id => ticket.id,
  14. :seen => false,
  15. :created_by_id => 1,
  16. :user_id => 2,
  17. )
  18. =end
  19. def self.add(data)
  20. # lookups
  21. if data[:type]
  22. type_id = TypeLookup.by_name( data[:type] )
  23. end
  24. if data[:object]
  25. object_id = ObjectLookup.by_name( data[:object] )
  26. end
  27. record = {
  28. o_id: data[:o_id],
  29. object_lookup_id: object_id,
  30. type_lookup_id: type_id,
  31. seen: data[:seen],
  32. user_id: data[:user_id],
  33. created_by_id: data[:created_by_id]
  34. }
  35. OnlineNotification.create(record)
  36. end
  37. =begin
  38. mark online notification as seen
  39. OnlineNotification.seen(
  40. :id => 2,
  41. )
  42. =end
  43. def self.seen(data)
  44. notification = OnlineNotification.find(data[:id])
  45. notification.seen = true
  46. notification.save
  47. end
  48. =begin
  49. remove whole online notifications of an object
  50. OnlineNotification.remove( 'Ticket', 123 )
  51. =end
  52. def self.remove( object_name, o_id )
  53. object_id = ObjectLookup.by_name( object_name )
  54. OnlineNotification.where(
  55. object_lookup_id: object_id,
  56. o_id: o_id,
  57. ).destroy_all
  58. end
  59. =begin
  60. return all online notifications of an user
  61. notifications = OnlineNotification.list( user, limit )
  62. =end
  63. def self.list(user, limit)
  64. notifications = OnlineNotification.where(user_id: user.id).
  65. order( 'created_at DESC, id DESC' ).
  66. limit( limit )
  67. list = []
  68. notifications.each do |item|
  69. data = item.attributes
  70. data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
  71. data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
  72. data.delete('object_lookup_id')
  73. data.delete('type_lookup_id')
  74. list.push data
  75. end
  76. list
  77. end
  78. =begin
  79. return all online notifications of an object
  80. notifications = OnlineNotification.list_by_object( 'Ticket', 123 )
  81. =end
  82. def self.list_by_object( object_name, o_id)
  83. object_id = ObjectLookup.by_name( object_name )
  84. notifications = OnlineNotification.where(
  85. object_lookup_id: object_id,
  86. o_id: o_id,
  87. ).
  88. order( 'created_at DESC, id DESC' ).
  89. limit( 10_000 )
  90. list = []
  91. notifications.each do |item|
  92. data = item.attributes
  93. data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
  94. data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
  95. data.delete('object_lookup_id')
  96. data.delete('type_lookup_id')
  97. list.push data
  98. end
  99. list
  100. end
  101. =begin
  102. mark online notification as seen by object
  103. OnlineNotification.seen_by_object( 'Ticket', 123 )
  104. =end
  105. def self.seen_by_object(object_name, o_id)
  106. object_id = ObjectLookup.by_name( object_name )
  107. notifications = OnlineNotification.where(
  108. object_lookup_id: object_id,
  109. o_id: o_id,
  110. seen: false,
  111. )
  112. notifications.each do |notification|
  113. notification.seen = true
  114. notification.save
  115. end
  116. true
  117. end
  118. =begin
  119. return all online notifications of an user with assets
  120. OnlineNotification.list_full( user )
  121. returns:
  122. list = {
  123. :stream => notifications,
  124. :assets => assets
  125. }
  126. =end
  127. def self.list_full(user, limit)
  128. notifications = OnlineNotification.list(user, limit)
  129. assets = ApplicationModel.assets_of_object_list(notifications)
  130. {
  131. stream: notifications,
  132. assets: assets
  133. }
  134. end
  135. def notify_clients_after_change
  136. Sessions.send_to(
  137. self.user_id,
  138. {
  139. event: 'OnlineNotification::changed',
  140. data: {}
  141. }
  142. )
  143. end
  144. end