recent_view.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # access check
  13. return if !access(object, o_id, user)
  14. # lookups
  15. object_lookup_id = ObjectLookup.by_name(object)
  16. # create entry
  17. record = {
  18. o_id: o_id,
  19. recent_view_object_id: object_lookup_id.to_i,
  20. created_by_id: user.id,
  21. }
  22. RecentView.create(record)
  23. end
  24. def self.log_destroy(requested_object, requested_object_id)
  25. return if requested_object == 'RecentView'
  26. RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
  27. .where(o_id: requested_object_id)
  28. .destroy_all
  29. end
  30. def self.user_log_destroy(user)
  31. RecentView.where(created_by_id: user.id).destroy_all
  32. end
  33. def self.list(user, limit = 10, object_name = nil)
  34. recent_views = if !object_name
  35. RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
  36. .group(:o_id, :recent_view_object_id, :created_by_id)
  37. .where(created_by_id: user.id)
  38. .limit(limit)
  39. elsif object_name == 'Ticket'
  40. state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id)
  41. local_recent_views = RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
  42. .group(:o_id, :recent_view_object_id, :created_by_id)
  43. .where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
  44. .limit(limit + 10)
  45. clear_list = []
  46. local_recent_views.each do |item|
  47. ticket = Ticket.find_by(id: item.o_id)
  48. next if !ticket
  49. next if !state_ids.include?(ticket.state_id)
  50. clear_list.push item
  51. break if clear_list.count == limit
  52. end
  53. clear_list
  54. else
  55. RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
  56. .group(:o_id, :recent_view_object_id, :created_by_id)
  57. .where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
  58. .limit(limit)
  59. end
  60. list = []
  61. recent_views.each do |item|
  62. # access check
  63. next if !access(ObjectLookup.by_id(item['recent_view_object_id']), item['o_id'], user)
  64. # add to result list
  65. list.push item
  66. end
  67. list
  68. end
  69. def notify_clients
  70. Sessions.send_to(
  71. created_by_id,
  72. {
  73. event: 'RecentView::changed',
  74. data: {}
  75. }
  76. )
  77. end
  78. def self.access(object, o_id, user)
  79. # check if object exists
  80. begin
  81. return if !Kernel.const_get(object)
  82. record = Kernel.const_get(object).lookup(id: o_id)
  83. return if !record
  84. rescue
  85. return
  86. end
  87. # check permission
  88. return if !record.respond_to?(:access?)
  89. record.access?(user, 'read')
  90. end
  91. =begin
  92. cleanup old entries
  93. RecentView.cleanup
  94. optional you can put the max oldest entries as argument
  95. RecentView.cleanup(3.month)
  96. =end
  97. def self.cleanup(diff = 3.months)
  98. RecentView.where('created_at < ?', Time.zone.now - diff).delete_all
  99. true
  100. end
  101. end