activity_stream.rb 3.5 KB

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