recent_view.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class RecentView < ApplicationModel
  3. belongs_to :object_lookup, class_name: 'ObjectLookup'
  4. belongs_to :ticket, class_name: 'Ticket', foreign_key: 'o_id'
  5. after_create :notify_clients
  6. after_update :notify_clients
  7. after_destroy :notify_clients
  8. def self.log(object, o_id, user)
  9. # access check
  10. return if !access(object, o_id, user)
  11. # lookups
  12. object_lookup_id = ObjectLookup.by_name(object)
  13. # create entry
  14. record = {
  15. o_id: o_id,
  16. recent_view_object_id: object_lookup_id.to_i,
  17. created_by_id: user.id,
  18. }
  19. RecentView.create(record)
  20. end
  21. def self.log_destroy(requested_object, requested_object_id)
  22. return if requested_object == 'RecentView'
  23. RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
  24. .where(o_id: requested_object_id)
  25. .destroy_all
  26. end
  27. def self.user_log_destroy(user)
  28. RecentView.where(created_by_id: user.id).destroy_all
  29. end
  30. def self.list(user, limit = 10, type = nil)
  31. recent_views = if !type
  32. RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id')
  33. .group(:o_id, :recent_view_object_id)
  34. .where(created_by_id: user.id)
  35. .limit(limit)
  36. elsif type == 'Ticket'
  37. state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id)
  38. RecentView.joins(:ticket)
  39. .select('recent_views.o_id as o_id, recent_views.recent_view_object_id as recent_view_object_id, MAX(recent_views.created_at) as created_at, MAX(recent_views.id) as id')
  40. .group(:o_id, :recent_view_object_id)
  41. .where('recent_views.created_by_id = ? AND recent_views.recent_view_object_id = ? AND tickets.state_id IN (?)', user.id, ObjectLookup.by_name('Ticket'), state_ids )
  42. .limit(limit)
  43. else
  44. RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id')
  45. .group(:o_id, :recent_view_object_id)
  46. .where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(type))
  47. .limit(limit)
  48. end
  49. list = []
  50. recent_views.each do |item|
  51. data = item.attributes
  52. data['object'] = ObjectLookup.by_id(data['recent_view_object_id'])
  53. data.delete('recent_view_object_id')
  54. # access check
  55. next if !access(data['object'], data['o_id'], user)
  56. # add to result list
  57. list.push data
  58. end
  59. list
  60. end
  61. def self.list_full(user, limit = 10)
  62. recent_viewed = list(user, limit)
  63. # get related object
  64. assets = ApplicationModel.assets_of_object_list(recent_viewed)
  65. {
  66. stream: recent_viewed,
  67. assets: assets,
  68. }
  69. end
  70. def notify_clients
  71. Sessions.send_to(
  72. created_by_id,
  73. {
  74. event: 'RecentView::changed',
  75. data: {}
  76. }
  77. )
  78. end
  79. def self.access(object, o_id, user)
  80. # check if object exists
  81. begin
  82. return if !Kernel.const_get(object)
  83. record = Kernel.const_get(object).lookup(id: o_id)
  84. return if !record
  85. rescue
  86. return
  87. end
  88. # check permission
  89. return if !record.respond_to?(:access?)
  90. record.access?(user, 'read')
  91. end
  92. =begin
  93. cleanup old entries
  94. RecentView.cleanup
  95. optional you can put the max oldest entries as argument
  96. RecentView.cleanup(1.month)
  97. =end
  98. def self.cleanup(diff = 1.month)
  99. RecentView.where('created_at < ?', Time.zone.now - diff).delete_all
  100. true
  101. end
  102. end