recent_view.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class RecentView < ApplicationModel
  3. include RecentView::Assets
  4. # rubocop:disable Rails/InverseOf
  5. belongs_to :ticket, foreign_key: 'o_id', optional: true
  6. belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'recent_view_object_id', optional: true
  7. belongs_to :created_by, class_name: 'User'
  8. # rubocop:enable Rails/InverseOf
  9. after_create :notify_clients
  10. after_update :notify_clients
  11. after_destroy :notify_clients
  12. association_attributes_ignored :created_by
  13. def self.log(object, o_id, user)
  14. return if !access(object, o_id, user)
  15. exists_by_object_and_id?(object, o_id)
  16. RecentView.create!(o_id: o_id,
  17. recent_view_object_id: ObjectLookup.by_name(object),
  18. created_by_id: user.id)
  19. end
  20. def self.log_destroy(requested_object, requested_object_id)
  21. return if requested_object == 'RecentView'
  22. RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
  23. .where(o_id: requested_object_id)
  24. .destroy_all
  25. end
  26. def self.user_log_destroy(user)
  27. RecentView.where(created_by_id: user.id).destroy_all
  28. end
  29. def self.list(user, limit = 10, object_name = nil)
  30. recent_views = RecentView.select('o_id, ' \
  31. 'recent_view_object_id, ' \
  32. 'created_by_id, ' \
  33. 'MAX(created_at) as created_at, ' \
  34. 'MAX(id) as id')
  35. .group(:o_id, :recent_view_object_id, :created_by_id)
  36. .where(created_by_id: user.id)
  37. .reorder(Arel.sql('MAX(created_at) DESC, MAX(id) DESC'))
  38. .limit(limit)
  39. if object_name.present?
  40. recent_views = recent_views.where(recent_view_object_id: ObjectLookup.by_name(object_name))
  41. end
  42. # hide merged / removed tickets in Ticket Merge dialog
  43. if object_name == 'Ticket'
  44. recent_views = recent_views.limit(limit * 2)
  45. viewable_ticket_ids = Ticket.where('id IN (?) AND state_id in (?)',
  46. recent_views.map(&:o_id),
  47. Ticket::State.by_category_ids(:viewable_agent_new))
  48. .pluck(:id)
  49. recent_views = recent_views.select { |rv| viewable_ticket_ids.include?(rv.o_id) }
  50. .first(limit)
  51. end
  52. recent_views.select { |rv| access(ObjectLookup.by_id(rv.recent_view_object_id), rv.o_id, user) }
  53. end
  54. def notify_clients
  55. Sessions.send_to(
  56. created_by_id,
  57. {
  58. event: 'RecentView::changed',
  59. data: {}
  60. }
  61. )
  62. end
  63. def self.access(object, o_id, user)
  64. record = object.to_s
  65. .safe_constantize
  66. .try(:lookup, id: o_id)
  67. Pundit.policy(user, record).try(:show?)
  68. end
  69. =begin
  70. cleanup old entries
  71. RecentView.cleanup
  72. optional you can put the max oldest entries as argument
  73. RecentView.cleanup(3.month)
  74. =end
  75. def self.cleanup(diff = 3.months)
  76. RecentView.where('created_at < ?', Time.zone.now - diff).delete_all
  77. true
  78. end
  79. end