2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
class ActivityStream < ApplicationModel
|
2018-03-20 12:16:17 +00:00
|
|
|
include ActivityStream::Assets
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
self.table_name = 'activity_streams'
|
2018-04-12 14:57:37 +00:00
|
|
|
|
|
|
|
# rubocop:disable Rails/InverseOf
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'activity_stream_object_id', optional: true
|
|
|
|
belongs_to :type, class_name: 'TypeLookup', foreign_key: 'activity_stream_type_id', optional: true
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:enable Rails/InverseOf
|
|
|
|
|
|
|
|
# the noop is needed since Layout/EmptyLines detects
|
|
|
|
# the block commend below wrongly as the measurement of
|
|
|
|
# the wanted indentation of the rubocop re-enabling above
|
|
|
|
def noop; end
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
permission_id = permission.id
|
2013-09-28 00:07:11 +00:00
|
|
|
end
|
|
|
|
|
2018-12-18 07:33:54 +00:00
|
|
|
# check if object for online notification exists
|
|
|
|
exists_by_object_and_id?(data[:object], data[:o_id])
|
|
|
|
|
2014-06-26 22:42:27 +00:00
|
|
|
# check newest entry - is needed
|
2013-09-28 00:07:11 +00:00
|
|
|
result = ActivityStream.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
2021-08-13 06:03:57 +00:00
|
|
|
# :activity_stream_type_id => type_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
permission_id: permission_id,
|
2015-04-27 13:42:53 +00:00
|
|
|
activity_stream_object_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_by_id: data[:created_by_id]
|
2019-04-07 15:23:03 +00:00
|
|
|
).order(created_at: :desc).first
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# return if old entry is really fresh
|
2016-04-13 07:30:44 +00:00
|
|
|
if result
|
2016-12-08 14:06:54 +00:00
|
|
|
activity_record_delay = 90.seconds
|
2021-07-16 13:38:01 +00:00
|
|
|
return result if result.created_at.to_i >= (data[:created_at].to_i - activity_record_delay)
|
2016-04-13 07:30:44 +00:00
|
|
|
end
|
2014-06-01 08:31:04 +00:00
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
# create history
|
|
|
|
record = {
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
|
|
|
activity_stream_type_id: type_id,
|
2015-04-27 13:42:53 +00:00
|
|
|
activity_stream_object_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
permission_id: permission_id,
|
|
|
|
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,
|
2018-12-19 17:31:51 +00:00
|
|
|
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)
|
2021-07-23 13:07:16 +00:00
|
|
|
ActivityStreamPolicy::Scope.new(user, self).resolve
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.limit(limit)
|
2013-09-28 00:07:11 +00:00
|
|
|
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
|