recent_view.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class RecentView < ApplicationModel
  3. include RecentView::Assets
  4. # rubocop:disable Rails/InverseOf
  5. belongs_to :ticket, foreign_key: 'o_id'
  6. belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'recent_view_object_id'
  7. # rubocop:enable Rails/InverseOf
  8. after_create :notify_clients
  9. after_update :notify_clients
  10. after_destroy :notify_clients
  11. def self.log(object, o_id, user)
  12. return if !access(object, o_id, user)
  13. exists_by_object_and_id?(object, o_id)
  14. RecentView.create!(o_id: o_id,
  15. recent_view_object_id: ObjectLookup.by_name(object),
  16. created_by_id: user.id)
  17. end
  18. def self.log_destroy(requested_object, requested_object_id)
  19. return if requested_object == 'RecentView'
  20. RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
  21. .where(o_id: requested_object_id)
  22. .destroy_all
  23. end
  24. def self.user_log_destroy(user)
  25. RecentView.where(created_by_id: user.id).destroy_all
  26. end
  27. def self.list(user, limit = 10, object_name = nil)
  28. recent_views = RecentView.select('o_id, ' \
  29. 'recent_view_object_id, ' \
  30. 'created_by_id, ' \
  31. 'MAX(created_at) as created_at, ' \
  32. 'MAX(id) as id')
  33. .group(:o_id, :recent_view_object_id, :created_by_id)
  34. .where(created_by_id: user.id)
  35. .order('MAX(created_at) DESC, MAX(id) DESC')
  36. .limit(limit)
  37. if object_name.present?
  38. recent_views = recent_views.where(recent_view_object_id: ObjectLookup.by_name(object_name))
  39. end
  40. # hide merged / removed tickets in Ticket Merge dialog
  41. if object_name == 'Ticket'
  42. recent_views = recent_views.limit(limit * 2)
  43. viewable_ticket_ids = Ticket.where('id IN (?) AND state_id in (?)',
  44. recent_views.map(&:o_id),
  45. Ticket::State.by_category(:viewable_agent_new).pluck(:id))
  46. .pluck(:id)
  47. recent_views = recent_views.select { |rv| viewable_ticket_ids.include?(rv.o_id) }
  48. .first(limit)
  49. end
  50. recent_views.select { |rv| access(ObjectLookup.by_id(rv.recent_view_object_id), rv.o_id, user) }
  51. end
  52. def notify_clients
  53. Sessions.send_to(
  54. created_by_id,
  55. {
  56. event: 'RecentView::changed',
  57. data: {}
  58. }
  59. )
  60. end
  61. def self.access(object, o_id, user)
  62. object.to_s
  63. .constantize
  64. .try(:lookup, { id: o_id })
  65. .try(:access?, user, 'read')
  66. rescue NameError
  67. false
  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