activity_stream.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://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', optional: true
  7. belongs_to :type, class_name: 'TypeLookup', foreign_key: 'activity_stream_type_id', optional: true
  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 if object for online notification exists
  41. exists_by_object_and_id?(data[:object], data[:o_id])
  42. # check newest entry - is needed
  43. result = ActivityStream.where(
  44. o_id: data[:o_id],
  45. # :activity_stream_type_id => type_id,
  46. permission_id: permission_id,
  47. activity_stream_object_id: object_id,
  48. created_by_id: data[:created_by_id]
  49. ).reorder(created_at: :desc).first
  50. # return if old entry is really fresh
  51. if result
  52. activity_record_delay = 90.seconds
  53. return result if result.created_at.to_i >= (data[:created_at].to_i - activity_record_delay)
  54. end
  55. # create history
  56. record = {
  57. o_id: data[:o_id],
  58. activity_stream_type_id: type_id,
  59. activity_stream_object_id: object_id,
  60. permission_id: permission_id,
  61. group_id: data[:group_id],
  62. created_at: data[:created_at],
  63. created_by_id: data[:created_by_id]
  64. }
  65. ActivityStream.create(record)
  66. end
  67. =begin
  68. remove whole activity entries of an object
  69. ActivityStream.remove('Ticket', 123)
  70. =end
  71. def self.remove(object_name, o_id)
  72. object_id = ObjectLookup.by_name(object_name)
  73. ActivityStream.where(
  74. activity_stream_object_id: object_id,
  75. o_id: o_id,
  76. ).destroy_all
  77. end
  78. =begin
  79. return all activity entries of an user
  80. activity_stream = ActivityStream.list(user, limit)
  81. =end
  82. def self.list(user, limit)
  83. ActivityStreamPolicy::Scope.new(user, self).resolve
  84. .reorder(created_at: :desc)
  85. .limit(limit)
  86. end
  87. =begin
  88. cleanup old stream messages
  89. ActivityStream.cleanup
  90. optional you can put the max oldest stream entries as argument
  91. ActivityStream.cleanup(3.months)
  92. =end
  93. def self.cleanup(diff = 3.months)
  94. ActivityStream.where('created_at < ?', Time.zone.now - diff).delete_all
  95. true
  96. end
  97. end