online_notification.rb 5.6 KB

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