diff --git a/app/models/recent_view.rb b/app/models/recent_view.rb index 19d900463..8cb226f90 100644 --- a/app/models/recent_view.rb +++ b/app/models/recent_view.rb @@ -2,6 +2,7 @@ class RecentView < ApplicationModel belongs_to :object_lookup, class_name: 'ObjectLookup' + belongs_to :ticket, class_name: 'Ticket', foreign_key: 'o_id' after_create :notify_clients after_update :notify_clients @@ -37,12 +38,21 @@ class RecentView < ApplicationModel def self.list(user, limit = 10, type = nil) recent_views = if !type - RecentView.where(created_by_id: user.id) - .order('created_at DESC, id DESC') + RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id') + .group(:o_id, :recent_view_object_id) + .where(created_by_id: user.id) + .limit(limit) + elsif type == 'Ticket' + state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id) + RecentView.joins(:ticket) + .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') + .group(:o_id, :recent_view_object_id) + .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 ) .limit(limit) else - RecentView.select('DISTINCT(o_id), recent_view_object_id, created_at, id').where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(type)) - .order('created_at DESC, id DESC') + RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id') + .group(:o_id, :recent_view_object_id) + .where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(type)) .limit(limit) end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 863338ca8..9c49b0e83 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -57,6 +57,7 @@ class Ticket < ApplicationModel belongs_to :group, class_name: 'Group' has_many :articles, class_name: 'Ticket::Article', after_add: :cache_update, after_remove: :cache_update has_many :ticket_time_accounting, class_name: 'Ticket::TimeAccounting', dependent: :destroy + has_many :recent_views, class_name: 'RecentView', foreign_key: 'o_id', dependent: :destroy belongs_to :organization, class_name: 'Organization' belongs_to :state, class_name: 'Ticket::State' belongs_to :priority, class_name: 'Ticket::Priority' diff --git a/test/unit/recent_view_test.rb b/test/unit/recent_view_test.rb index 610bc3596..912ca6931 100644 --- a/test/unit/recent_view_test.rb +++ b/test/unit/recent_view_test.rb @@ -40,14 +40,9 @@ class RecentViewTest < ActiveSupport::TestCase assert(list[0]['o_id'], ticket1.id) assert(list[0]['object'], 'Ticket') - assert(list[1]['o_id'], ticket1.id) + assert(list[1]['o_id'], ticket2.id) assert(list[1]['object'], 'Ticket') - - assert(list[2]['o_id'], ticket2.id) - assert(list[2]['object'], 'Ticket') - - assert(list[3]['o_id'], ticket1.id) - assert(list[3]['object'], 'Ticket') + assert_equal(2, list.count) ticket1.destroy ticket2.destroy