2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
class RecentView < ApplicationModel
|
2018-03-20 12:16:17 +00:00
|
|
|
include RecentView::Assets
|
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:disable Rails/InverseOf
|
|
|
|
belongs_to :ticket, foreign_key: 'o_id'
|
2018-03-20 12:16:17 +00:00
|
|
|
belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'recent_view_object_id'
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:enable Rails/InverseOf
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2016-01-24 18:27:36 +00:00
|
|
|
after_create :notify_clients
|
|
|
|
after_update :notify_clients
|
|
|
|
after_destroy :notify_clients
|
2014-08-25 23:29:28 +00:00
|
|
|
|
2016-01-24 18:27:36 +00:00
|
|
|
def self.log(object, o_id, user)
|
|
|
|
return if !access(object, o_id, user)
|
2014-12-31 13:56:37 +00:00
|
|
|
|
2018-12-18 07:33:54 +00:00
|
|
|
exists_by_object_and_id?(object, o_id)
|
|
|
|
|
|
|
|
RecentView.create!(o_id: o_id,
|
|
|
|
recent_view_object_id: ObjectLookup.by_name(object),
|
|
|
|
created_by_id: user.id)
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
2016-01-24 18:27:36 +00:00
|
|
|
def self.log_destroy(requested_object, requested_object_id)
|
2014-08-26 14:24:44 +00:00
|
|
|
return if requested_object == 'RecentView'
|
2018-09-11 05:47:30 +00:00
|
|
|
|
2016-01-24 18:27:36 +00:00
|
|
|
RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
|
|
|
|
.where(o_id: requested_object_id)
|
2016-01-15 17:22:57 +00:00
|
|
|
.destroy_all
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
2016-01-24 18:27:36 +00:00
|
|
|
def self.user_log_destroy(user)
|
|
|
|
RecentView.where(created_by_id: user.id).destroy_all
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
def self.list(user, limit = 10, object_name = nil)
|
2018-09-11 05:47:30 +00:00
|
|
|
recent_views = RecentView.select('o_id, ' \
|
|
|
|
'recent_view_object_id, ' \
|
|
|
|
'created_by_id, ' \
|
|
|
|
'MAX(created_at) as created_at, ' \
|
|
|
|
'MAX(id) as id')
|
|
|
|
.group(:o_id, :recent_view_object_id, :created_by_id)
|
|
|
|
.where(created_by_id: user.id)
|
|
|
|
.order('MAX(created_at) DESC, MAX(id) DESC')
|
|
|
|
.limit(limit)
|
|
|
|
|
|
|
|
if object_name.present?
|
|
|
|
recent_views = recent_views.where(recent_view_object_id: ObjectLookup.by_name(object_name))
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2018-09-11 05:47:30 +00:00
|
|
|
|
|
|
|
# hide merged / removed tickets in Ticket Merge dialog
|
|
|
|
if object_name == 'Ticket'
|
|
|
|
recent_views = recent_views.limit(limit * 2)
|
|
|
|
|
|
|
|
viewable_ticket_ids = Ticket.where('id IN (?) AND state_id in (?)',
|
|
|
|
recent_views.map(&:o_id),
|
|
|
|
Ticket::State.by_category(:viewable_agent_new).pluck(:id))
|
|
|
|
.pluck(:id)
|
|
|
|
|
|
|
|
recent_views = recent_views.select { |rv| viewable_ticket_ids.include?(rv.o_id) }
|
|
|
|
.first(limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
recent_views.select { |rv| access(ObjectLookup.by_id(rv.recent_view_object_id), rv.o_id, user) }
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
2014-08-25 23:29:28 +00:00
|
|
|
def notify_clients
|
|
|
|
Sessions.send_to(
|
2015-05-07 12:10:38 +00:00
|
|
|
created_by_id,
|
2014-08-25 23:29:28 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
event: 'RecentView::changed',
|
|
|
|
data: {}
|
2014-08-25 23:29:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-12-31 13:56:37 +00:00
|
|
|
def self.access(object, o_id, user)
|
2018-09-11 05:47:30 +00:00
|
|
|
object.to_s
|
|
|
|
.constantize
|
|
|
|
.try(:lookup, { id: o_id })
|
|
|
|
.try(:access?, user, 'read')
|
|
|
|
rescue NameError
|
|
|
|
false
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
2015-06-30 23:38:16 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
cleanup old entries
|
|
|
|
|
|
|
|
RecentView.cleanup
|
|
|
|
|
2016-08-17 11:24:51 +00:00
|
|
|
optional you can put the max oldest entries as argument
|
2015-06-30 23:38:16 +00:00
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
RecentView.cleanup(3.month)
|
2015-06-30 23:38:16 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
def self.cleanup(diff = 3.months)
|
2015-06-30 23:38:16 +00:00
|
|
|
RecentView.where('created_at < ?', Time.zone.now - diff).delete_all
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|