diff --git a/app/models/online_notification.rb b/app/models/online_notification.rb index 0b4112d31..1c0a25c92 100644 --- a/app/models/online_notification.rb +++ b/app/models/online_notification.rb @@ -13,12 +13,12 @@ class OnlineNotification < ApplicationModel add a new online notification for this user OnlineNotification.add( - :type => 'Assigned to you', - :object => 'Ticket', - :o_id => ticket.id, - :seen => false, - :created_by_id => 1, - :user_id => 2, + :type => 'Assigned to you', + :object => 'Ticket', + :o_id => ticket.id, + :seen => false, + :created_by_id => 1, + :user_id => 2, ) =end @@ -50,9 +50,9 @@ add a new online notification for this user mark online notification as seen OnlineNotification.seen( - :id => 2, - :user => UserObject, #optional, if passed all - #notfications for the given user are marked as seen + :id => 2, + :user => UserObject, # optional, if passed all + # notfications for the given user are marked as seen ) =end @@ -106,6 +106,34 @@ return all online notifications of an user =begin +return all online notifications of an object + + notifications = OnlineNotification.by_object( 'Ticket', 123 ) + +=end + + def self.by_object( object_name, o_id ) + object_id = ObjectLookup.by_name( object_name ) + notifications = OnlineNotification.where( + :object_lookup_id => object_id, + :o_id => o_id, + ). + order( 'created_at DESC, id DESC' ). + limit( 10_000 ) + list = [] + notifications.each do |item| + data = item.attributes + data['object'] = ObjectLookup.by_id( data['object_lookup_id'] ) + data['type'] = TypeLookup.by_id( data['type_lookup_id'] ) + data.delete('object_lookup_id') + data.delete('type_lookup_id') + list.push data + end + list + end + +=begin + return all online notifications of an user with assets OnlineNotification.list_full( user ) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 3bb47a0f7..ff67cef5d 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -213,6 +213,9 @@ returns # delete articles self.articles.destroy_all + + # destroy online notifications + OnlineNotification.remove( self.class.to_s, self.id ) end end \ No newline at end of file diff --git a/test/unit/online_notifiaction_test.rb b/test/unit/online_notifiaction_test.rb index 53d2231e7..072700b43 100644 --- a/test/unit/online_notifiaction_test.rb +++ b/test/unit/online_notifiaction_test.rb @@ -2,8 +2,8 @@ require 'test_helper' class OnlineNotificationTest < ActiveSupport::TestCase - role = Role.lookup( :name => 'Agent' ) - group = Group.lookup( :name => 'Users' ) + role = Role.lookup( :name => 'Agent' ) + group = Group.lookup( :name => 'Users' ) agent_user1 = User.create_or_update( :login => 'agent_online_notify1', :firstname => 'Bob', @@ -118,6 +118,10 @@ class OnlineNotificationTest < ActiveSupport::TestCase ticket.destroy found = Ticket.where( :id => ticket_id ).first assert( !found, "Ticket destroyed") + + # check if notifications for ticket still exist + notifications = OnlineNotification.by_object( 'Ticket', ticket_id ) + assert( notifications.empty?, "still notifications for destroyed ticket available") } end