activity_stream.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class ActivityStream < ApplicationModel
  3. self.table_name = 'activity_streams'
  4. belongs_to :activity_stream_type, :class_name => 'ActivityStream::Type'
  5. belongs_to :activity_stream_object, :class_name => 'ActivityStream::Object'
  6. @@cache_type = {}
  7. @@cache_object = {}
  8. =begin
  9. add a new activity entry for an object
  10. ActivityStream.add(
  11. :type => 'updated',
  12. :object => 'Ticket',
  13. :role => 'Admin',
  14. :o_id => ticket.id,
  15. :created_by_id => 1,
  16. :created_at => '2013-06-04 10:00:00',
  17. )
  18. =end
  19. def self.add(data)
  20. # lookups
  21. if data[:type]
  22. type = self.type_lookup( data[:type] )
  23. end
  24. if data[:object]
  25. object = self.object_lookup( data[:object] )
  26. end
  27. role_id = nil
  28. if data[:role]
  29. role = Role.lookup( :name => data[:role] )
  30. if !role
  31. raise "No such Role #{data[:role]}"
  32. end
  33. role_id = role.id
  34. end
  35. # check if entry is needed
  36. result = ActivityStream.where(
  37. :o_id => data[:o_id],
  38. # :activity_stream_type_id => type.id,
  39. :role_id => role_id,
  40. :activity_stream_object_id => object.id,
  41. :created_by_id => data[:created_by_id]
  42. ).last
  43. # resturn if old entry is really freash
  44. return result if result && result.created_at >= (data[:created_at] - 10.seconds)
  45. # create history
  46. record = {
  47. :o_id => data[:o_id],
  48. :activity_stream_type_id => type.id,
  49. :activity_stream_object_id => object.id,
  50. :role_id => role_id,
  51. :group_id => data[:group_id],
  52. :created_at => data[:created_at],
  53. :created_by_id => data[:created_by_id]
  54. }
  55. ActivityStream.create(record)
  56. end
  57. =begin
  58. remove whole activity entries of an object
  59. ActivityStream.remove( 'Ticket', 123 )
  60. =end
  61. def self.remove( object_name, o_id )
  62. object = self.object_lookup( object_name )
  63. ActivityStream.where(
  64. :activity_stream_object_id => object.id,
  65. :o_id => o_id,
  66. ).destroy_all
  67. end
  68. =begin
  69. return all activity entries of an user
  70. activity_stream = ActivityStream.list( user )
  71. =end
  72. def self.list(user,limit)
  73. role_ids = user.role_ids
  74. group_ids = user.group_ids
  75. # do not return an activity stream for custoers
  76. customer_role = Role.lookup( :name => 'Customer' )
  77. return [] if role_ids.include?(customer_role.id)
  78. if group_ids.empty?
  79. stream = ActivityStream.where('(role_id IN (?) AND group_id is NULL)', role_ids ).
  80. order( 'created_at DESC, id DESC' ).
  81. limit( limit )
  82. else
  83. stream = ActivityStream.where('(role_id IN (?) AND group_id is NULL) OR ( role_id IN (?) AND group_id IN (?) ) OR ( role_id is NULL AND group_id IN (?) )', role_ids, role_ids, group_ids, group_ids ).
  84. order( 'created_at DESC, id DESC' ).
  85. limit( limit )
  86. end
  87. list = []
  88. stream.each do |item|
  89. data = item.attributes
  90. data['object'] = self.object_lookup_id( data['activity_stream_object_id'] ).name
  91. data['type'] = self.type_lookup_id( data['activity_stream_type_id'] ).name
  92. data.delete('activity_stream_object_id')
  93. data.delete('activity_stream_type_id')
  94. list.push data
  95. end
  96. list
  97. end
  98. private
  99. def self.type_lookup_id( id )
  100. # use cache
  101. return @@cache_type[ id ] if @@cache_type[ id ]
  102. # lookup
  103. type = ActivityStream::Type.lookup( :id => id )
  104. @@cache_type[ id ] = type
  105. type
  106. end
  107. def self.type_lookup( name )
  108. # use cache
  109. return @@cache_type[ name ] if @@cache_type[ name ]
  110. # lookup
  111. type = ActivityStream::Type.lookup( :name => name )
  112. if type
  113. @@cache_type[ name ] = type
  114. return type
  115. end
  116. # create
  117. type = ActivityStream::Type.create(
  118. :name => name
  119. )
  120. @@cache_type[ name ] = type
  121. type
  122. end
  123. def self.object_lookup_id( id )
  124. # use cache
  125. return @@cache_object[ id ] if @@cache_object[ id ]
  126. # lookup
  127. object = ActivityStream::Object.lookup( :id => id )
  128. @@cache_object[ id ] = object
  129. object
  130. end
  131. def self.object_lookup( name )
  132. # use cache
  133. return @@cache_object[ name ] if @@cache_object[ name ]
  134. # lookup
  135. object = ActivityStream::Object.lookup( :name => name )
  136. if object
  137. @@cache_object[ name ] = object
  138. return object
  139. end
  140. # create
  141. object = ActivityStream::Object.create(
  142. :name => name
  143. )
  144. @@cache_object[ name ] = object
  145. object
  146. end
  147. class Object < ApplicationModel
  148. end
  149. class Type < ApplicationModel
  150. end
  151. end