online_notification.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class OnlineNotification < ApplicationModel
  3. include OnlineNotification::Assets
  4. include OnlineNotification::TriggersSubscriptions
  5. belongs_to :user, optional: true
  6. # rubocop:disable Rails/InverseOf
  7. belongs_to :object, class_name: 'ObjectLookup', foreign_key: 'object_lookup_id', optional: true
  8. belongs_to :type, class_name: 'TypeLookup', foreign_key: 'type_lookup_id', optional: true
  9. # rubocop:enable Rails/InverseOf
  10. after_create :notify_clients_after_change
  11. after_update :notify_clients_after_change
  12. after_destroy :notify_clients_after_change
  13. belongs_to :created_by, class_name: 'User'
  14. belongs_to :updated_by, class_name: 'User'
  15. =begin
  16. add a new online notification for this user
  17. OnlineNotification.add(
  18. type: 'Assigned to you',
  19. object: 'Ticket',
  20. o_id: ticket.id,
  21. seen: false,
  22. user_id: 2,
  23. created_by_id: 1,
  24. updated_by_id: 1,
  25. created_at: Time.zone.now,
  26. updated_at: Time.zone.now,
  27. )
  28. =end
  29. def self.add(data)
  30. # lookups
  31. if data[:type]
  32. type_id = TypeLookup.by_name(data[:type])
  33. end
  34. if data[:object]
  35. object_id = ObjectLookup.by_name(data[:object])
  36. end
  37. # check if object for online notification exists
  38. exists_by_object_and_id?(data[:object], data[:o_id])
  39. record = {
  40. o_id: data[:o_id],
  41. object_lookup_id: object_id,
  42. type_lookup_id: type_id,
  43. seen: data[:seen],
  44. user_id: data[:user_id],
  45. created_by_id: data[:created_by_id],
  46. updated_by_id: data[:updated_by_id] || data[:created_by_id],
  47. created_at: data[:created_at] || Time.zone.now,
  48. updated_at: data[:updated_at] || Time.zone.now,
  49. }
  50. OnlineNotification.create!(record)
  51. end
  52. =begin
  53. remove whole online notifications of an object
  54. OnlineNotification.remove('Ticket', 123)
  55. =end
  56. def self.remove(object_name, o_id)
  57. object_id = ObjectLookup.by_name(object_name)
  58. OnlineNotification.where(
  59. object_lookup_id: object_id,
  60. o_id: o_id,
  61. ).destroy_all
  62. end
  63. =begin
  64. remove whole online notifications of an object by type
  65. OnlineNotification.remove_by_type('Ticket', 123, type, user)
  66. =end
  67. def self.remove_by_type(object_name, o_id, type_name, user)
  68. object_id = ObjectLookup.by_name(object_name)
  69. type_id = TypeLookup.by_name(type_name)
  70. OnlineNotification.where(
  71. object_lookup_id: object_id,
  72. type_lookup_id: type_id,
  73. o_id: o_id,
  74. user_id: user.id,
  75. ).destroy_all
  76. end
  77. =begin
  78. return all online notifications of an user
  79. notifications = OnlineNotification.list(user, limit)
  80. =end
  81. def self.list(user, limit)
  82. OnlineNotification.where(user_id: user.id)
  83. .order(created_at: :desc)
  84. .limit(limit)
  85. end
  86. =begin
  87. return all online notifications of an object
  88. notifications = OnlineNotification.list_by_object('Ticket', 123)
  89. =end
  90. def self.list_by_object(object_name, o_id)
  91. object_id = ObjectLookup.by_name(object_name)
  92. OnlineNotification.where(
  93. object_lookup_id: object_id,
  94. o_id: o_id,
  95. )
  96. .order(created_at: :desc)
  97. .limit(10_000)
  98. end
  99. =begin
  100. mark online notification as seen by object
  101. OnlineNotification.seen_by_object('Ticket', 123, user_id)
  102. =end
  103. def self.seen_by_object(object_name, o_id)
  104. object_id = ObjectLookup.by_name(object_name)
  105. notifications = OnlineNotification.where(
  106. object_lookup_id: object_id,
  107. o_id: o_id,
  108. seen: false,
  109. )
  110. notifications.each do |notification|
  111. notification.seen = true
  112. notification.save
  113. end
  114. true
  115. end
  116. def notify_clients_after_change
  117. Sessions.send_to(
  118. user_id,
  119. {
  120. event: 'OnlineNotification::changed',
  121. data: {}
  122. }
  123. )
  124. end
  125. =begin
  126. check if all notifications are seen for dedicated object
  127. OnlineNotification.all_seen?('Ticket', 123)
  128. returns:
  129. true # false
  130. =end
  131. def self.all_seen?(object_name, o_id)
  132. notifications = OnlineNotification.list_by_object(object_name, o_id)
  133. notifications.each do |onine_notification|
  134. return false if !onine_notification['seen']
  135. end
  136. true
  137. end
  138. =begin
  139. cleanup old online notifications
  140. OnlineNotification.cleanup
  141. with dedicated times
  142. max_age = Time.zone.now - 9.months
  143. max_own_seen = Time.zone.now - 10.minutes
  144. max_auto_seen = Time.zone.now - 8.hours
  145. OnlineNotification.cleanup(max_age, max_own_seen, max_auto_seen)
  146. =end
  147. def self.cleanup(max_age = 9.months.ago, max_own_seen = 10.minutes.ago, max_auto_seen = 8.hours.ago)
  148. OnlineNotification.where('created_at < ?', max_age).delete_all
  149. OnlineNotification.where('seen = ? AND updated_at < ?', true, max_own_seen).each do |notification|
  150. # delete own "seen" notifications after 1 hour
  151. next if notification.user_id == notification.updated_by_id && notification.updated_at > max_own_seen
  152. # delete notifications which are set to "seen" by somebody else after 8 hours
  153. next if notification.user_id != notification.updated_by_id && notification.updated_at > max_auto_seen
  154. notification.delete
  155. end
  156. # notify all agents
  157. User.with_permissions('ticket.agent').each do |user|
  158. Sessions.send_to(
  159. user.id,
  160. {
  161. event: 'OnlineNotification::changed',
  162. data: {}
  163. }
  164. )
  165. sleep 2 # slow down client requests
  166. end
  167. true
  168. end
  169. end