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
|
|
|
load 'recent_view/assets.rb'
|
|
|
|
include RecentView::Assets
|
|
|
|
|
|
|
|
belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'recent_view_object_id'
|
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)
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2014-12-31 13:56:37 +00:00
|
|
|
# access check
|
2016-01-24 18:27:36 +00:00
|
|
|
return if !access(object, o_id, user)
|
2014-12-31 13:56:37 +00:00
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
# lookups
|
2016-01-24 18:27:36 +00:00
|
|
|
object_lookup_id = ObjectLookup.by_name(object)
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
# create entry
|
|
|
|
record = {
|
2015-04-27 13:42:53 +00:00
|
|
|
o_id: o_id,
|
|
|
|
recent_view_object_id: object_lookup_id.to_i,
|
|
|
|
created_by_id: user.id,
|
2013-05-21 22:41:32 +00:00
|
|
|
}
|
|
|
|
RecentView.create(record)
|
|
|
|
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'
|
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)
|
|
|
|
recent_views = if !object_name
|
|
|
|
RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
|
|
|
|
.group(:o_id, :recent_view_object_id, :created_by_id)
|
2017-04-25 14:04:59 +00:00
|
|
|
.where(created_by_id: user.id)
|
|
|
|
.limit(limit)
|
2018-03-20 12:16:17 +00:00
|
|
|
elsif object_name == 'Ticket'
|
2017-04-25 14:04:59 +00:00
|
|
|
state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id)
|
2018-03-20 12:16:17 +00:00
|
|
|
local_recent_views = RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
|
|
|
|
.group(:o_id, :recent_view_object_id, :created_by_id)
|
|
|
|
.where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
|
|
|
|
.limit(limit + 10)
|
|
|
|
clear_list = []
|
|
|
|
local_recent_views.each do |item|
|
|
|
|
ticket = Ticket.find_by(id: item.o_id)
|
|
|
|
next if !ticket
|
|
|
|
next if !state_ids.include?(ticket.state_id)
|
|
|
|
clear_list.push item
|
|
|
|
break if clear_list.count == limit
|
|
|
|
end
|
|
|
|
clear_list
|
2016-01-15 17:22:57 +00:00
|
|
|
else
|
2018-03-20 12:16:17 +00:00
|
|
|
RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
|
|
|
|
.group(:o_id, :recent_view_object_id, :created_by_id)
|
|
|
|
.where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
|
2016-01-15 17:22:57 +00:00
|
|
|
.limit(limit)
|
|
|
|
end
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
recent_views.each do |item|
|
2014-12-31 13:56:37 +00:00
|
|
|
|
|
|
|
# access check
|
2018-03-20 12:16:17 +00:00
|
|
|
next if !access(ObjectLookup.by_id(item['recent_view_object_id']), item['o_id'], user)
|
2014-12-31 13:56:37 +00:00
|
|
|
|
|
|
|
# add to result list
|
2018-03-20 12:16:17 +00:00
|
|
|
list.push item
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
list
|
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)
|
|
|
|
|
|
|
|
# check if object exists
|
|
|
|
begin
|
2016-01-24 18:27:36 +00:00
|
|
|
return if !Kernel.const_get(object)
|
|
|
|
record = Kernel.const_get(object).lookup(id: o_id)
|
2014-12-31 13:56:37 +00:00
|
|
|
return if !record
|
|
|
|
rescue
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# check permission
|
2017-06-16 20:43:09 +00:00
|
|
|
return if !record.respond_to?(:access?)
|
|
|
|
record.access?(user, 'read')
|
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
|