activity_stream.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ActivityStream < ApplicationModel
  3. include ActivityStream::Assets
  4. self.table_name = 'activity_streams'
  5. # rubocop:disable Rails/InverseOf
  6. belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'activity_stream_object_id'
  7. belongs_to :type, class_name: 'TypeLookup', foreign_key: 'activity_stream_type_id'
  8. # rubocop:enable Rails/InverseOf
  9. # the noop is needed since Layout/EmptyLines detects
  10. # the block commend below wrongly as the measurement of
  11. # the wanted indentation of the rubocop re-enabling above
  12. def noop; end
  13. =begin
  14. add a new activity entry for an object
  15. ActivityStream.add(
  16. type: 'update',
  17. object: 'Ticket',
  18. permission: 'admin.user',
  19. o_id: ticket.id,
  20. created_by_id: 1,
  21. created_at: '2013-06-04 10:00:00',
  22. )
  23. =end
  24. def self.add(data)
  25. # lookups
  26. if data[:type]
  27. type_id = TypeLookup.by_name(data[:type])
  28. end
  29. if data[:object]
  30. object_id = ObjectLookup.by_name(data[:object])
  31. end
  32. permission_id = nil
  33. if data[:permission]
  34. permission = Permission.lookup(name: data[:permission])
  35. if !permission
  36. raise "No such Permission #{data[:permission]}"
  37. end
  38. permission_id = permission.id
  39. end
  40. # check newest entry - is needed
  41. result = ActivityStream.where(
  42. o_id: data[:o_id],
  43. #:activity_stream_type_id => type_id,
  44. permission_id: permission_id,
  45. activity_stream_object_id: object_id,
  46. created_by_id: data[:created_by_id]
  47. ).order('created_at DESC, id DESC').first
  48. # resturn if old entry is really fresh
  49. if result
  50. activity_record_delay = 90.seconds
  51. return result if result.created_at.to_i >= ( data[:created_at].to_i - activity_record_delay )
  52. end
  53. # create history
  54. record = {
  55. o_id: data[:o_id],
  56. activity_stream_type_id: type_id,
  57. activity_stream_object_id: object_id,
  58. permission_id: permission_id,
  59. group_id: data[:group_id],
  60. created_at: data[:created_at],
  61. created_by_id: data[:created_by_id]
  62. }
  63. ActivityStream.create(record)
  64. end
  65. =begin
  66. remove whole activity entries of an object
  67. ActivityStream.remove('Ticket', 123)
  68. =end
  69. def self.remove(object_name, o_id)
  70. object_id = ObjectLookup.by_name(object_name)
  71. ActivityStream.where(
  72. activity_stream_object_id: object_id,
  73. o_id: o_id,
  74. ).destroy_all
  75. end
  76. =begin
  77. return all activity entries of an user
  78. activity_stream = ActivityStream.list(user, limit)
  79. =end
  80. def self.list(user, limit)
  81. # do not return an activity stream for customers
  82. return [] if !user.permissions?('ticket.agent') && !user.permissions?('admin')
  83. permission_ids = user.permissions_with_child_ids
  84. group_ids = user.group_ids_access('read')
  85. stream = if group_ids.blank?
  86. ActivityStream.where('(permission_id IN (?) AND group_id is NULL)', permission_ids)
  87. .order('created_at DESC, id DESC')
  88. .limit(limit)
  89. else
  90. 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)
  91. .order('created_at DESC, id DESC')
  92. .limit(limit)
  93. end
  94. stream
  95. end
  96. =begin
  97. cleanup old stream messages
  98. ActivityStream.cleanup
  99. optional you can put the max oldest stream entries as argument
  100. ActivityStream.cleanup(3.months)
  101. =end
  102. def self.cleanup(diff = 3.months)
  103. ActivityStream.where('created_at < ?', Time.zone.now - diff).delete_all
  104. true
  105. end
  106. end