2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 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
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :ticket, foreign_key: 'o_id', optional: true
|
|
|
|
belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'recent_view_object_id', optional: true
|
2020-09-08 15:06:23 +00:00
|
|
|
belongs_to :created_by, class_name: 'User'
|
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
|
|
|
|
2020-09-14 12:34:36 +00:00
|
|
|
association_attributes_ignored :created_by
|
|
|
|
|
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)
|
|
|
|
|
2018-12-19 17:31:51 +00:00
|
|
|
RecentView.create!(o_id: o_id,
|
2018-12-18 07:33:54 +00:00
|
|
|
recent_view_object_id: ObjectLookup.by_name(object),
|
2018-12-19 17:31:51 +00:00
|
|
|
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)
|
2019-07-04 11:16:55 +00:00
|
|
|
.order(Arel.sql('MAX(created_at) DESC, MAX(id) DESC'))
|
2018-09-11 05:47:30 +00:00
|
|
|
.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),
|
2020-09-30 09:07:01 +00:00
|
|
|
Ticket::State.by_category(:viewable_agent_new).pluck(:id)) # rubocop:disable Rails/PluckInWhere
|
2018-09-11 05:47:30 +00:00
|
|
|
.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',
|
2018-12-19 17:31:51 +00:00
|
|
|
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)
|
2020-03-19 09:39:51 +00:00
|
|
|
record = object.to_s
|
|
|
|
.safe_constantize
|
|
|
|
.try(:lookup, { id: o_id })
|
|
|
|
|
|
|
|
Pundit.policy(user, record).try(:show?)
|
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
|