2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 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
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :object_lookup, class_name: 'ObjectLookup'
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2014-08-25 23:29:28 +00:00
|
|
|
after_create :notify_clients
|
|
|
|
after_update :notify_clients
|
|
|
|
after_destroy :notify_clients
|
|
|
|
|
2014-08-23 23:56:45 +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
|
|
|
|
return if !access( object, o_id, user )
|
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
# lookups
|
2014-08-23 23:56:45 +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
|
|
|
|
|
|
|
|
def self.log_destroy( requested_object, requested_object_id )
|
2014-08-26 14:24:44 +00:00
|
|
|
return if requested_object == 'RecentView'
|
2015-04-30 17:51:31 +00:00
|
|
|
RecentView.where( recent_view_object_id: ObjectLookup.by_name( requested_object ) )
|
|
|
|
.where( o_id: requested_object_id )
|
|
|
|
.destroy_all
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
2014-08-23 22:29:04 +00:00
|
|
|
def self.user_log_destroy( user )
|
2015-04-27 13:42:53 +00:00
|
|
|
RecentView.where( created_by_id: user.id ).destroy_all
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
2014-09-21 19:58:07 +00:00
|
|
|
def self.list( user, limit = 10, type = nil )
|
|
|
|
if !type
|
2015-04-30 17:51:31 +00:00
|
|
|
recent_views = RecentView.where( created_by_id: user.id )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order('created_at DESC, id DESC')
|
|
|
|
.limit(limit)
|
2014-09-21 19:58:07 +00:00
|
|
|
else
|
2015-04-30 17:51:31 +00:00
|
|
|
recent_views = RecentView.select('DISTINCT(o_id), recent_view_object_id').where( created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(type) )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order('created_at DESC, id DESC')
|
|
|
|
.limit(limit)
|
2014-09-21 19:58:07 +00:00
|
|
|
end
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
list = []
|
|
|
|
recent_views.each { |item|
|
2014-12-31 13:56:37 +00:00
|
|
|
data = item.attributes
|
2014-08-24 00:16:50 +00:00
|
|
|
data['object'] = ObjectLookup.by_id( data['recent_view_object_id'] )
|
|
|
|
data.delete( 'recent_view_object_id' )
|
2014-12-31 13:56:37 +00:00
|
|
|
|
|
|
|
# access check
|
|
|
|
next if !access( data['object'], data['o_id'], user )
|
|
|
|
|
|
|
|
# add to result list
|
2013-05-21 22:41:32 +00:00
|
|
|
list.push data
|
|
|
|
}
|
2014-07-13 18:52:32 +00:00
|
|
|
list
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
2014-08-26 14:30:16 +00:00
|
|
|
def self.list_full( user, limit = 10 )
|
2015-05-07 12:10:38 +00:00
|
|
|
recent_viewed = list( user, limit )
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2014-08-26 07:40:25 +00:00
|
|
|
# get related object
|
|
|
|
assets = ApplicationModel.assets_of_object_list(recent_viewed)
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2015-04-30 17:20:27 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
stream: recent_viewed,
|
|
|
|
assets: assets,
|
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
|
|
|
|
return if !Kernel.const_get( object )
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
|
return if !record.respond_to?(:permission)
|
2015-04-27 13:42:53 +00:00
|
|
|
record.permission( current_user: user )
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|