2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2014-08-26 07:54:12 +00:00
|
|
|
|
|
|
|
class OnlineNotification < ApplicationModel
|
2018-03-20 12:16:17 +00:00
|
|
|
include OnlineNotification::Assets
|
|
|
|
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :user, optional: true
|
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: 'object_lookup_id', optional: true
|
|
|
|
belongs_to :type, class_name: 'TypeLookup', foreign_key: 'type_lookup_id', optional: true
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:enable Rails/InverseOf
|
2014-08-26 07:54:12 +00:00
|
|
|
|
|
|
|
after_create :notify_clients_after_change
|
|
|
|
after_update :notify_clients_after_change
|
|
|
|
after_destroy :notify_clients_after_change
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
add a new online notification for this user
|
|
|
|
|
|
|
|
OnlineNotification.add(
|
2015-09-03 09:14:09 +00:00
|
|
|
type: 'Assigned to you',
|
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
seen: false,
|
|
|
|
user_id: 2,
|
2016-02-21 11:43:16 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
updated_at: Time.zone.now,
|
2014-08-26 07:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:type]
|
2016-02-20 10:12:15 +00:00
|
|
|
type_id = TypeLookup.by_name(data[:type])
|
2014-08-26 07:54:12 +00:00
|
|
|
end
|
|
|
|
if data[:object]
|
2016-02-20 10:12:15 +00:00
|
|
|
object_id = ObjectLookup.by_name(data[:object])
|
2014-08-26 07:54:12 +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-08-26 07:54:12 +00:00
|
|
|
record = {
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
type_lookup_id: type_id,
|
|
|
|
seen: data[:seen],
|
|
|
|
user_id: data[:user_id],
|
|
|
|
created_by_id: data[:created_by_id],
|
|
|
|
updated_by_id: data[:updated_by_id] || data[:created_by_id],
|
|
|
|
created_at: data[:created_at] || Time.zone.now,
|
|
|
|
updated_at: data[:updated_at] || Time.zone.now,
|
2014-08-26 07:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
OnlineNotification.create!(record)
|
2014-08-26 07:54:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove whole online notifications of an object
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
OnlineNotification.remove('Ticket', 123)
|
2014-08-26 07:54:12 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
def self.remove(object_name, o_id)
|
|
|
|
object_id = ObjectLookup.by_name(object_name)
|
2014-08-26 07:54:12 +00:00
|
|
|
OnlineNotification.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: o_id,
|
2014-08-26 07:54:12 +00:00
|
|
|
).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
remove whole online notifications of an object by type
|
|
|
|
|
2016-02-22 12:54:28 +00:00
|
|
|
OnlineNotification.remove_by_type('Ticket', 123, type, user)
|
2016-02-20 10:12:15 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
def self.remove_by_type(object_name, o_id, type_name, user)
|
2016-02-20 10:12:15 +00:00
|
|
|
object_id = ObjectLookup.by_name(object_name)
|
2018-03-20 12:16:17 +00:00
|
|
|
type_id = TypeLookup.by_name(type_name)
|
2016-02-20 10:12:15 +00:00
|
|
|
OnlineNotification.where(
|
|
|
|
object_lookup_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
type_lookup_id: type_id,
|
|
|
|
o_id: o_id,
|
|
|
|
user_id: user.id,
|
2016-02-20 10:12:15 +00:00
|
|
|
).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-08-26 07:54:12 +00:00
|
|
|
return all online notifications of an user
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
notifications = OnlineNotification.list(user, limit)
|
2014-08-26 07:54:12 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 14:53:29 +00:00
|
|
|
def self.list(user, limit)
|
2018-03-20 12:16:17 +00:00
|
|
|
OnlineNotification.where(user_id: user.id)
|
2021-07-05 15:03:29 +00:00
|
|
|
.order(created_at: :desc)
|
2018-03-20 12:16:17 +00:00
|
|
|
.limit(limit)
|
2014-08-26 07:54:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-04-01 11:14:46 +00:00
|
|
|
return all online notifications of an object
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
notifications = OnlineNotification.list_by_object('Ticket', 123)
|
2015-04-01 11:47:10 +00:00
|
|
|
|
2015-04-01 11:14:46 +00:00
|
|
|
=end
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
def self.list_by_object(object_name, o_id)
|
|
|
|
object_id = ObjectLookup.by_name(object_name)
|
2020-07-07 06:30:20 +00:00
|
|
|
OnlineNotification.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: o_id,
|
2015-04-30 17:51:31 +00:00
|
|
|
)
|
2019-04-07 15:23:03 +00:00
|
|
|
.order(created_at: :desc)
|
2016-02-20 10:12:15 +00:00
|
|
|
.limit(10_000)
|
2020-07-07 06:30:20 +00:00
|
|
|
|
2015-04-01 11:14:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-04-01 11:47:10 +00:00
|
|
|
mark online notification as seen by object
|
|
|
|
|
2016-02-20 10:12:15 +00:00
|
|
|
OnlineNotification.seen_by_object('Ticket', 123, user_id)
|
2015-04-01 11:47:10 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.seen_by_object(object_name, o_id)
|
2016-02-20 10:12:15 +00:00
|
|
|
object_id = ObjectLookup.by_name(object_name)
|
2015-04-30 17:14:51 +00:00
|
|
|
notifications = OnlineNotification.where(
|
|
|
|
object_lookup_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: o_id,
|
|
|
|
seen: false,
|
2015-04-30 17:14:51 +00:00
|
|
|
)
|
|
|
|
notifications.each do |notification|
|
|
|
|
notification.seen = true
|
|
|
|
notification.save
|
|
|
|
end
|
|
|
|
true
|
2015-04-01 11:47:10 +00:00
|
|
|
end
|
|
|
|
|
2014-08-26 07:54:12 +00:00
|
|
|
def notify_clients_after_change
|
2014-08-27 13:06:09 +00:00
|
|
|
Sessions.send_to(
|
2015-05-07 12:10:38 +00:00
|
|
|
user_id,
|
2014-08-27 13:06:09 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
event: 'OnlineNotification::changed',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {}
|
2014-08-27 13:06:09 +00:00
|
|
|
}
|
2014-08-26 07:54:12 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-06-30 22:25:05 +00:00
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
check if all notifications are seen for dedicated object
|
2016-02-22 07:58:33 +00:00
|
|
|
|
|
|
|
OnlineNotification.all_seen?('Ticket', 123)
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-03-20 12:16:17 +00:00
|
|
|
def self.all_seen?(object_name, o_id)
|
|
|
|
notifications = OnlineNotification.list_by_object(object_name, o_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
notifications.each do |onine_notification|
|
2016-02-22 07:58:33 +00:00
|
|
|
return false if !onine_notification['seen']
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-02-22 07:58:33 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if notification was created for certain user
|
|
|
|
|
|
|
|
OnlineNotification.exists?(for_user, object, o_id, type, created_by_user, seen)
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
# rubocop:disable Metrics/ParameterLists
|
2018-03-20 12:16:17 +00:00
|
|
|
def self.exists?(user, object_name, o_id, type_name, created_by_user, seen)
|
2016-02-22 07:58:33 +00:00
|
|
|
# rubocop:enable Metrics/ParameterLists
|
2018-03-20 12:16:17 +00:00
|
|
|
object_id = ObjectLookup.by_name(object_name)
|
|
|
|
type_id = TypeLookup.by_name(type_name)
|
2016-02-22 07:58:33 +00:00
|
|
|
notifications = OnlineNotification.list(user, 10)
|
2017-10-01 12:25:52 +00:00
|
|
|
notifications.each do |notification|
|
2018-03-20 12:16:17 +00:00
|
|
|
next if notification.o_id != o_id
|
|
|
|
next if notification.object_lookup_id != object_id
|
|
|
|
next if notification.type_lookup_id != type_id
|
|
|
|
next if notification.created_by_id != created_by_user.id
|
|
|
|
next if notification.seen != seen
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-02-22 07:58:33 +00:00
|
|
|
return true
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-02-22 07:58:33 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-06-30 22:25:05 +00:00
|
|
|
cleanup old online notifications
|
|
|
|
|
|
|
|
OnlineNotification.cleanup
|
|
|
|
|
2016-02-21 11:43:16 +00:00
|
|
|
with dedicated times
|
|
|
|
|
|
|
|
max_age = Time.zone.now - 9.months
|
2016-02-24 05:27:47 +00:00
|
|
|
max_own_seen = Time.zone.now - 10.minutes
|
2016-02-21 11:43:16 +00:00
|
|
|
max_auto_seen = Time.zone.now - 8.hours
|
|
|
|
|
|
|
|
OnlineNotification.cleanup(max_age, max_own_seen, max_auto_seen)
|
|
|
|
|
2015-06-30 22:25:05 +00:00
|
|
|
=end
|
|
|
|
|
2022-01-18 06:20:33 +00:00
|
|
|
def self.cleanup(max_age = 9.months.ago, max_own_seen = 10.minutes.ago, max_auto_seen = 8.hours.ago)
|
2016-02-21 11:43:16 +00:00
|
|
|
OnlineNotification.where('created_at < ?', max_age).delete_all
|
2017-10-01 12:25:52 +00:00
|
|
|
OnlineNotification.where('seen = ? AND updated_at < ?', true, max_own_seen).each do |notification|
|
2016-02-21 11:43:16 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# delete own "seen" notifications after 1 hour
|
2016-02-21 11:43:16 +00:00
|
|
|
next if notification.user_id == notification.updated_by_id && notification.updated_at > max_own_seen
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# delete notifications which are set to "seen" by somebody else after 8 hours
|
2016-02-21 11:43:16 +00:00
|
|
|
next if notification.user_id != notification.updated_by_id && notification.updated_at > max_auto_seen
|
|
|
|
|
|
|
|
notification.delete
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-06-30 22:25:05 +00:00
|
|
|
|
|
|
|
# notify all agents
|
2017-10-01 12:25:52 +00:00
|
|
|
User.with_permissions('ticket.agent').each do |user|
|
2015-06-30 22:25:05 +00:00
|
|
|
Sessions.send_to(
|
|
|
|
user.id,
|
|
|
|
{
|
|
|
|
event: 'OnlineNotification::changed',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {}
|
2015-06-30 22:25:05 +00:00
|
|
|
}
|
|
|
|
)
|
2015-06-30 22:33:45 +00:00
|
|
|
sleep 2 # slow down client requests
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-06-30 22:25:05 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|