online_notification.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class OnlineNotification < ApplicationModel
  3. belongs_to :type_lookup, :class_name => 'TypeLookup'
  4. belongs_to :object_lookup, :class_name => 'ObjectLookup'
  5. after_create :notify_clients_after_change
  6. after_update :notify_clients_after_change
  7. after_destroy :notify_clients_after_change
  8. =begin
  9. add a new online notification for this user
  10. OnlineNotification.add(
  11. :type => 'Assigned to you',
  12. :object => 'Ticket',
  13. :o_id => ticket.id,
  14. :seen => false,
  15. :created_by_id => 1,
  16. :user_id => 2,
  17. )
  18. =end
  19. def self.add(data)
  20. # lookups
  21. if data[:type]
  22. type_id = TypeLookup.by_name( data[:type] )
  23. end
  24. if data[:object]
  25. object_id = ObjectLookup.by_name( data[:object] )
  26. end
  27. record = {
  28. :o_id => data[:o_id],
  29. :object_lookup_id => object_id,
  30. :type_lookup_id => type_id,
  31. :seen => data[:seen],
  32. :user_id => data[:user_id],
  33. :created_by_id => data[:created_by_id]
  34. }
  35. OnlineNotification.create(record)
  36. end
  37. =begin
  38. add a new online notification for this user
  39. OnlineNotification.add(
  40. :type => 'Assigned to you',
  41. :object => 'Ticket',
  42. :o_id => ticket.id,
  43. :seen => 1,
  44. :created_by_id => 1,
  45. :user_id => 2,
  46. )
  47. =end
  48. def self.seen(data)
  49. notification = OnlineNotification.find(data[:id])
  50. notification.seen = true
  51. notification.save
  52. end
  53. =begin
  54. remove whole online notifications of an object
  55. OnlineNotification.remove( 'Ticket', 123 )
  56. =end
  57. def self.remove( object_name, o_id )
  58. object_id = ObjectLookup.by_name( object_name )
  59. OnlineNotification.where(
  60. :object_lookup_id => object_id,
  61. :o_id => o_id,
  62. ).destroy_all
  63. end
  64. =begin
  65. return all online notifications of an user
  66. notifications = OnlineNotification.list( user )
  67. =end
  68. def self.list(user,limit)
  69. notifications = OnlineNotification.where(:user_id => user.id).
  70. order( 'created_at DESC, id DESC' ).
  71. limit( limit )
  72. list = []
  73. notifications.each do |item|
  74. data = item.attributes
  75. data['object'] = ObjectLookup.by_id( data['object_lookup_id'] )
  76. data['type'] = TypeLookup.by_id( data['type_lookup_id'] )
  77. data.delete('object_lookup_id')
  78. data.delete('type_lookup_id')
  79. list.push data
  80. end
  81. list
  82. end
  83. =begin
  84. return all online notifications of an user with assets
  85. OnlineNotification.list_full( user )
  86. returns:
  87. list = {
  88. :stream => notifications,
  89. :assets => assets
  90. }
  91. =end
  92. def self.list_full(user,limit)
  93. notifications = OnlineNotification.list(user, limit)
  94. assets = ApplicationModel.assets_of_object_list(notifications)
  95. return {
  96. :stream => notifications,
  97. :assets => assets
  98. }
  99. end
  100. def notify_clients_after_change
  101. Sessions.send_to(
  102. self.user_id,
  103. {
  104. :event => 'OnlineNotification::changed',
  105. :data => {}
  106. }
  107. )
  108. end
  109. end