activity_stream.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2016 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: 'update',
  10. object: 'Ticket',
  11. permission: 'admin.user',
  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. permission_id = nil
  26. if data[:permission]
  27. permission = Permission.lookup(name: data[:permission])
  28. if !permission
  29. raise "No such Permission #{data[:permission]}"
  30. end
  31. permission_id = permission.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. permission_id: permission_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. if result
  43. activity_record_delay = 90.seconds
  44. return result if result.created_at.to_i >= ( data[:created_at].to_i - activity_record_delay )
  45. end
  46. # create history
  47. record = {
  48. o_id: data[:o_id],
  49. activity_stream_type_id: type_id,
  50. activity_stream_object_id: object_id,
  51. permission_id: permission_id,
  52. group_id: data[:group_id],
  53. created_at: data[:created_at],
  54. created_by_id: data[:created_by_id]
  55. }
  56. ActivityStream.create(record)
  57. end
  58. =begin
  59. remove whole activity entries of an object
  60. ActivityStream.remove('Ticket', 123)
  61. =end
  62. def self.remove(object_name, o_id)
  63. object_id = ObjectLookup.by_name(object_name)
  64. ActivityStream.where(
  65. activity_stream_object_id: object_id,
  66. o_id: o_id,
  67. ).destroy_all
  68. end
  69. =begin
  70. return all activity entries of an user
  71. activity_stream = ActivityStream.list(user, limit)
  72. =end
  73. def self.list(user, limit)
  74. # do not return an activity stream for customers
  75. return [] if !user.permissions?('ticket.agent') && !user.permissions?('admin')
  76. permission_ids = user.permissions_with_child_ids
  77. group_ids = user.group_ids_access('read')
  78. stream = if group_ids.blank?
  79. ActivityStream.where('(permission_id IN (?) AND group_id is NULL)', permission_ids)
  80. .order('created_at DESC, id DESC')
  81. .limit(limit)
  82. else
  83. ActivityStream.where('(permission_id IN (?) AND group_id is NULL) OR (permission_id IN (?) AND group_id IN (?)) OR (permission_id is NULL AND group_id IN (?))', permission_ids, permission_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'] = ObjectLookup.by_id( data['activity_stream_object_id'] )
  91. data['type'] = TypeLookup.by_id( data['activity_stream_type_id'] )
  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. =begin
  99. cleanup old stream messages
  100. ActivityStream.cleanup
  101. optional you can put the max oldest stream entries as argument
  102. ActivityStream.cleanup(3.months)
  103. =end
  104. def self.cleanup(diff = 3.months)
  105. ActivityStream.where('created_at < ?', Time.zone.now - diff).delete_all
  106. true
  107. end
  108. end