2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2013-09-28 00:07:11 +00:00
class ActivityStream < ApplicationModel
self . table_name = 'activity_streams'
2015-04-27 13:42:53 +00:00
belongs_to :activity_stream_type , class_name : 'TypeLookup'
belongs_to :activity_stream_object , class_name : 'ObjectLookup'
2013-09-28 00:07:11 +00:00
= begin
add a new activity entry for an object
ActivityStream . add (
2016-02-22 19:58:23 +00:00
type : 'update' ,
object : 'Ticket' ,
2016-08-12 16:39:09 +00:00
permission : 'admin.user' ,
2016-02-22 19:58:23 +00:00
o_id : ticket . id ,
created_by_id : 1 ,
created_at : '2013-06-04 10:00:00' ,
2013-09-28 00:07:11 +00:00
)
= end
def self . add ( data )
# lookups
if data [ :type ]
2016-02-22 19:58:23 +00:00
type_id = TypeLookup . by_name ( data [ :type ] )
2013-09-28 00:07:11 +00:00
end
if data [ :object ]
2016-02-22 19:58:23 +00:00
object_id = ObjectLookup . by_name ( data [ :object ] )
2013-09-28 00:07:11 +00:00
end
2016-08-12 16:39:09 +00:00
permission_id = nil
if data [ :permission ]
permission = Permission . lookup ( name : data [ :permission ] )
if ! permission
raise " No such Permission #{ data [ :permission ] } "
2013-09-28 00:07:11 +00:00
end
2016-08-12 16:39:09 +00:00
permission_id = permission . id
2013-09-28 00:07:11 +00:00
end
2014-06-26 22:42:27 +00:00
# check newest entry - is needed
2013-09-28 00:07:11 +00:00
result = ActivityStream . where (
2015-04-27 13:42:53 +00:00
o_id : data [ :o_id ] ,
2014-12-31 14:07:56 +00:00
#:activity_stream_type_id => type_id,
2016-08-12 16:39:09 +00:00
permission_id : permission_id ,
2015-04-27 13:42:53 +00:00
activity_stream_object_id : object_id ,
created_by_id : data [ :created_by_id ]
2014-06-26 22:42:27 +00:00
) . order ( 'created_at DESC, id DESC' ) . first
2013-09-28 00:07:11 +00:00
2014-05-30 07:57:35 +00:00
# resturn if old entry is really fresh
2016-04-13 07:30:44 +00:00
if result
activity_record_delay = if ENV [ 'ZAMMAD_ACTIVITY_RECORD_DELAY' ]
ENV [ 'ZAMMAD_ACTIVITY_RECORD_DELAY' ] . to_i . seconds
else
90 . seconds
end
return result if result . created_at . to_i > = ( data [ :created_at ] . to_i - activity_record_delay )
end
2014-06-01 08:31:04 +00:00
2013-09-28 00:07:11 +00:00
# create history
record = {
2015-04-27 13:42:53 +00:00
o_id : data [ :o_id ] ,
activity_stream_type_id : type_id ,
activity_stream_object_id : object_id ,
2016-08-12 16:39:09 +00:00
permission_id : permission_id ,
2015-04-27 13:42:53 +00:00
group_id : data [ :group_id ] ,
created_at : data [ :created_at ] ,
created_by_id : data [ :created_by_id ]
2013-09-28 00:07:11 +00:00
}
2013-09-29 21:37:49 +00:00
2013-09-28 00:07:11 +00:00
ActivityStream . create ( record )
end
= begin
remove whole activity entries of an object
2016-02-22 19:58:23 +00:00
ActivityStream . remove ( 'Ticket' , 123 )
2013-09-28 00:07:11 +00:00
= end
2016-02-22 19:58:23 +00:00
def self . remove ( object_name , o_id )
object_id = ObjectLookup . by_name ( object_name )
2013-09-28 00:07:11 +00:00
ActivityStream . where (
2015-04-27 13:42:53 +00:00
activity_stream_object_id : object_id ,
o_id : o_id ,
2013-09-28 00:07:11 +00:00
) . destroy_all
end
= begin
return all activity entries of an user
2016-08-12 16:39:09 +00:00
activity_stream = ActivityStream . list ( user , limit )
2013-09-28 00:07:11 +00:00
= end
2015-04-27 14:53:29 +00:00
def self . list ( user , limit )
2013-09-29 21:37:49 +00:00
# do not return an activity stream for custoers
2016-08-12 16:39:09 +00:00
return [ ] if ! user . permissions? ( 'ticket.agent' ) && ! user . permissions? ( 'admin' )
permission_ids = [ ]
user . roles . each { | role |
permission_ids = permission_ids . concat ( role . permission_ids )
}
group_ids = user . group_ids
2013-09-29 21:37:49 +00:00
2016-01-15 17:22:57 +00:00
stream = if group_ids . empty?
2016-08-12 16:39:09 +00:00
ActivityStream . where ( '(permission_id IN (?) AND group_id is NULL)' , permission_ids )
. order ( 'created_at DESC, id DESC' )
. limit ( limit )
2016-01-15 17:22:57 +00:00
else
2016-08-12 16:39:09 +00:00
ActivityStream . where ( '(permission_id IN (?) AND group_id is NULL) OR (permission_id IN (?) AND group_id IN (?)) OR (permission_id is NULL AND group_id IN (?))' , permission_ids , permission_ids , group_ids , group_ids )
. order ( 'created_at DESC, id DESC' )
. limit ( limit )
2016-01-15 17:22:57 +00:00
end
2013-09-28 00:07:11 +00:00
list = [ ]
stream . each do | item |
2014-12-31 14:07:56 +00:00
data = item . attributes
data [ 'object' ] = ObjectLookup . by_id ( data [ 'activity_stream_object_id' ] )
data [ 'type' ] = TypeLookup . by_id ( data [ 'activity_stream_type_id' ] )
2013-09-28 00:07:11 +00:00
data . delete ( 'activity_stream_object_id' )
data . delete ( 'activity_stream_type_id' )
list . push data
end
list
end
2015-06-30 22:25:05 +00:00
= begin
cleanup old stream messages
ActivityStream . cleanup
2016-08-17 11:24:51 +00:00
optional you can put the max oldest stream entries as argument
2015-06-30 22:25:05 +00:00
2015-06-30 23:31:21 +00:00
ActivityStream . cleanup ( 3 . months )
2015-06-30 22:25:05 +00:00
= end
2015-06-30 23:31:21 +00:00
def self . cleanup ( diff = 3 . months )
2015-06-30 22:25:05 +00:00
ActivityStream . where ( 'created_at < ?' , Time . zone . now - diff ) . delete_all
true
end
2015-04-27 14:15:29 +00:00
end