2014-08-26 07:54:12 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class OnlineNotification < ApplicationModel
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :type_lookup, class_name: 'TypeLookup'
|
|
|
|
belongs_to :object_lookup, class_name: 'ObjectLookup'
|
2015-06-30 22:25:05 +00:00
|
|
|
belongs_to :user
|
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,
|
|
|
|
created_by_id: 1,
|
|
|
|
user_id: 2,
|
2014-08-26 07:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:type]
|
|
|
|
type_id = TypeLookup.by_name( data[:type] )
|
|
|
|
end
|
|
|
|
if data[:object]
|
|
|
|
object_id = ObjectLookup.by_name( data[:object] )
|
|
|
|
end
|
|
|
|
|
|
|
|
record = {
|
2015-04-27 13:42:53 +00:00
|
|
|
o_id: data[:o_id],
|
|
|
|
object_lookup_id: object_id,
|
|
|
|
type_lookup_id: type_id,
|
|
|
|
seen: data[:seen],
|
|
|
|
user_id: data[:user_id],
|
|
|
|
created_by_id: data[:created_by_id]
|
2014-08-26 07:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OnlineNotification.create(record)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-01-12 22:13:50 +00:00
|
|
|
mark online notification as seen
|
2014-08-26 07:54:12 +00:00
|
|
|
|
2015-01-12 22:13:50 +00:00
|
|
|
OnlineNotification.seen(
|
2015-09-03 09:14:09 +00:00
|
|
|
id: 2,
|
2014-08-26 07:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.seen(data)
|
2015-04-30 17:14:51 +00:00
|
|
|
notification = OnlineNotification.find(data[:id])
|
2015-05-01 11:53:35 +00:00
|
|
|
notification.seen = true
|
|
|
|
notification.save
|
2014-08-26 07:54:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove whole online notifications of an object
|
|
|
|
|
|
|
|
OnlineNotification.remove( 'Ticket', 123 )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove( object_name, o_id )
|
|
|
|
object_id = ObjectLookup.by_name( object_name )
|
|
|
|
OnlineNotification.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_id,
|
|
|
|
o_id: o_id,
|
2014-08-26 07:54:12 +00:00
|
|
|
).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
return all online notifications of an user
|
|
|
|
|
2015-01-12 22:13:50 +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)
|
2014-08-26 07:54:12 +00:00
|
|
|
|
2015-04-30 17:51:31 +00:00
|
|
|
notifications = OnlineNotification.where(user_id: user.id)
|
2015-05-01 12:27:57 +00:00
|
|
|
.order( 'created_at DESC, id DESC' )
|
|
|
|
.limit( limit )
|
2014-08-26 07:54:12 +00:00
|
|
|
list = []
|
|
|
|
notifications.each do |item|
|
2015-09-03 09:14:09 +00:00
|
|
|
data = item.attributes
|
|
|
|
data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
|
|
|
|
data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
|
2014-08-26 07:54:12 +00:00
|
|
|
data.delete('object_lookup_id')
|
|
|
|
data.delete('type_lookup_id')
|
|
|
|
list.push data
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-04-01 11:14:46 +00:00
|
|
|
return all online notifications of an object
|
|
|
|
|
2015-04-01 11:47:10 +00:00
|
|
|
notifications = OnlineNotification.list_by_object( 'Ticket', 123 )
|
|
|
|
|
2015-04-01 11:14:46 +00:00
|
|
|
=end
|
|
|
|
|
2015-04-01 14:33:24 +00:00
|
|
|
def self.list_by_object( object_name, o_id)
|
2015-04-01 11:14:46 +00:00
|
|
|
object_id = ObjectLookup.by_name( object_name )
|
2015-04-01 14:33:24 +00:00
|
|
|
notifications = OnlineNotification.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
object_lookup_id: object_id,
|
|
|
|
o_id: o_id,
|
2015-04-30 17:51:31 +00:00
|
|
|
)
|
2015-05-05 13:23:45 +00:00
|
|
|
.order( 'created_at DESC, id DESC' ) # rubocop:disable Style/MultilineOperationIndentation
|
|
|
|
.limit( 10_000 ) # rubocop:disable Style/MultilineOperationIndentation
|
2015-09-03 09:14:09 +00:00
|
|
|
notifications
|
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
|
|
|
|
|
2015-09-03 09:14:09 +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)
|
2015-04-30 17:14:51 +00:00
|
|
|
object_id = ObjectLookup.by_name( object_name )
|
|
|
|
notifications = OnlineNotification.where(
|
|
|
|
object_lookup_id: object_id,
|
|
|
|
o_id: o_id,
|
|
|
|
seen: false,
|
|
|
|
)
|
|
|
|
notifications.each do |notification|
|
|
|
|
notification.seen = true
|
|
|
|
notification.save
|
|
|
|
end
|
|
|
|
true
|
2015-04-01 11:47:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-08-26 07:54:12 +00:00
|
|
|
return all online notifications of an user with assets
|
|
|
|
|
|
|
|
OnlineNotification.list_full( user )
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
list = {
|
2015-09-03 09:14:09 +00:00
|
|
|
stream: notifications,
|
|
|
|
assets: assets,
|
2014-08-26 07:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 14:53:29 +00:00
|
|
|
def self.list_full(user, limit)
|
2014-08-26 07:54:12 +00:00
|
|
|
|
|
|
|
notifications = OnlineNotification.list(user, limit)
|
|
|
|
assets = ApplicationModel.assets_of_object_list(notifications)
|
2015-04-30 17:20:27 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
stream: notifications,
|
|
|
|
assets: assets
|
2014-08-26 07:54:12 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
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',
|
|
|
|
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
|
|
|
|
|
|
|
|
cleanup old online notifications
|
|
|
|
|
|
|
|
OnlineNotification.cleanup
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.cleanup
|
2015-09-03 09:16:07 +00:00
|
|
|
OnlineNotification.where('created_at < ?', Time.zone.now - 6.months).delete_all
|
2015-09-03 06:47:58 +00:00
|
|
|
OnlineNotification.where('seen = ? AND created_at < ?', true, Time.zone.now - 4.hours).delete_all
|
2015-11-30 13:29:23 +00:00
|
|
|
OnlineNotification.where('seen = ? AND updated_at < ?', true, Time.zone.now - 1.hour).delete_all
|
2015-06-30 22:25:05 +00:00
|
|
|
|
|
|
|
# notify all agents
|
2015-07-25 14:36:16 +00:00
|
|
|
User.of_role('Agent').each {|user|
|
2015-06-30 22:25:05 +00:00
|
|
|
Sessions.send_to(
|
|
|
|
user.id,
|
|
|
|
{
|
|
|
|
event: 'OnlineNotification::changed',
|
|
|
|
data: {}
|
|
|
|
}
|
|
|
|
)
|
2015-06-30 22:33:45 +00:00
|
|
|
sleep 2 # slow down client requests
|
2015-06-30 22:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|