activity_stream.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright (C) 2012-2014 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 => 'ObjectLookup'
  6. @@cache_type = {}
  7. =begin
  8. add a new activity entry for an object
  9. ActivityStream.add(
  10. :type => 'updated',
  11. :object => 'Ticket',
  12. :role => 'Admin',
  13. :o_id => ticket.id,
  14. :created_by_id => 1,
  15. :created_at => '2013-06-04 10:00:00',
  16. )
  17. =end
  18. def self.add(data)
  19. # lookups
  20. if data[:type]
  21. type = self.type_lookup( data[:type] )
  22. end
  23. if data[:object]
  24. object_id = ObjectLookup.by_name( data[:object] )
  25. end
  26. role_id = nil
  27. if data[:role]
  28. role = Role.lookup( :name => data[:role] )
  29. if !role
  30. raise "No such Role #{data[:role]}"
  31. end
  32. role_id = role.id
  33. end
  34. # check newest entry - is needed
  35. result = ActivityStream.where(
  36. :o_id => data[:o_id],
  37. # :activity_stream_type_id => type.id,
  38. :role_id => role_id,
  39. :activity_stream_object_id => object_id,
  40. :created_by_id => data[:created_by_id]
  41. ).order('created_at DESC, id DESC').first
  42. # resturn if old entry is really fresh
  43. return result if result && result.created_at.to_i >= ( data[:created_at].to_i - 12 )
  44. # create history
  45. record = {
  46. :o_id => data[:o_id],
  47. :activity_stream_type_id => type.id,
  48. :activity_stream_object_id => object_id,
  49. :role_id => role_id,
  50. :group_id => data[:group_id],
  51. :created_at => data[:created_at],
  52. :created_by_id => data[:created_by_id]
  53. }
  54. ActivityStream.create(record)
  55. end
  56. =begin
  57. remove whole activity entries of an object
  58. ActivityStream.remove( 'Ticket', 123 )
  59. =end
  60. def self.remove( object_name, o_id )
  61. object_id = ObjectLookup.by_name( object_name )
  62. ActivityStream.where(
  63. :activity_stream_object_id => object_id,
  64. :o_id => o_id,
  65. ).destroy_all
  66. end
  67. =begin
  68. return all activity entries of an user
  69. activity_stream = ActivityStream.list( user )
  70. =end
  71. def self.list(user,limit)
  72. role_ids = user.role_ids
  73. group_ids = user.group_ids
  74. # do not return an activity stream for custoers
  75. customer_role = Role.lookup( :name => 'Customer' )
  76. return [] if role_ids.include?(customer_role.id)
  77. if group_ids.empty?
  78. stream = ActivityStream.where('(role_id IN (?) AND group_id is NULL)', role_ids ).
  79. order( 'created_at DESC, id DESC' ).
  80. limit( limit )
  81. else
  82. 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 ).
  83. order( 'created_at DESC, id DESC' ).
  84. limit( limit )
  85. end
  86. list = []
  87. stream.each do |item|
  88. data = item.attributes
  89. data['object'] = ObjectLookup.by_id( data['activity_stream_object_id'] )
  90. data['type'] = self.type_lookup_id( data['activity_stream_type_id'] ).name
  91. data.delete('activity_stream_object_id')
  92. data.delete('activity_stream_type_id')
  93. list.push data
  94. end
  95. list
  96. end
  97. private
  98. def self.type_lookup_id( id )
  99. # use cache
  100. return @@cache_type[ id ] if @@cache_type[ id ]
  101. # lookup
  102. type = ActivityStream::Type.lookup( :id => id )
  103. @@cache_type[ id ] = type
  104. type
  105. end
  106. def self.type_lookup( name )
  107. # use cache
  108. return @@cache_type[ name ] if @@cache_type[ name ]
  109. # lookup
  110. type = ActivityStream::Type.lookup( :name => name )
  111. if type
  112. @@cache_type[ name ] = type
  113. return type
  114. end
  115. # create
  116. type = ActivityStream::Type.create(
  117. :name => name
  118. )
  119. @@cache_type[ name ] = type
  120. type
  121. end
  122. class Object < ApplicationModel
  123. end
  124. class Type < ApplicationModel
  125. end
  126. end