online_notification.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. # Copyright (C) 2012-2024 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. affected_user_ids = []
  129. where(created_at: ...max_age)
  130. .tap { |relation| affected_user_ids |= relation.distinct.pluck(:user_id) }
  131. .delete_all
  132. where(seen: true)
  133. .where('(user_id = updated_by_id AND updated_at < :max_own_seen) OR (user_id != updated_by_id AND updated_at < :max_auto_seen)',
  134. max_own_seen:, max_auto_seen:)
  135. .tap { |relation| affected_user_ids |= relation.distinct.pluck(:user_id) }
  136. .delete_all
  137. cleanup_notify_agents(affected_user_ids)
  138. true
  139. end
  140. def self.cleanup_notify_agents(user_ids)
  141. return if user_ids.blank?
  142. User
  143. .with_permissions('ticket.agent')
  144. .where(id: user_ids)
  145. .each do |user|
  146. Sessions.send_to(
  147. user.id,
  148. {
  149. event: 'OnlineNotification::changed',
  150. data: {}
  151. }
  152. )
  153. end
  154. end
  155. =begin
  156. check if online notification should be shown in general as already seen with current state
  157. ticket = Ticket.find(1)
  158. seen = OnlineNotification.seen_state?(ticket, user_id_check)
  159. returns
  160. result = true # or false
  161. =end
  162. def self.seen_state?(ticket, user_id_check = nil)
  163. state = Ticket::State.lookup(id: ticket.state_id)
  164. state_type = Ticket::StateType.lookup(id: state.state_type_id)
  165. # always to set unseen for ticket owner and users which did not the update
  166. return false if seen_state_not_merged_owner?(state_type, ticket, user_id_check)
  167. # set all to seen if pending action state is a closed or merged state
  168. if state_type.name == 'pending action' && state.next_state_id
  169. state = Ticket::State.lookup(id: state.next_state_id)
  170. state_type = Ticket::StateType.lookup(id: state.state_type_id)
  171. end
  172. # set all to seen if new state is pending reminder state
  173. if state_type.name == 'pending reminder'
  174. return seen_state_pending_reminder?(ticket, user_id_check)
  175. end
  176. # set all to seen if new state is a closed or merged state
  177. return true if %w[closed merged].include? state_type.name
  178. false
  179. end
  180. def self.seen_state_pending_reminder?(ticket, user_id_check)
  181. return true if !user_id_check
  182. return false if ticket.owner_id == 1
  183. return false if ticket.updated_by_id != ticket.owner_id && user_id_check == ticket.owner_id
  184. true
  185. end
  186. private_class_method :seen_state_pending_reminder?
  187. def self.seen_state_not_merged_owner?(state_type, ticket, user_id_check)
  188. return false if state_type.name == 'merged'
  189. return false if !user_id_check
  190. user_id_check == ticket.owner_id && user_id_check != ticket.updated_by_id
  191. end
  192. private_class_method :seen_state_not_merged_owner?
  193. private
  194. def notify_clients_after_change
  195. Sessions.send_to(
  196. user_id,
  197. {
  198. event: 'OnlineNotification::changed',
  199. data: {}
  200. }
  201. )
  202. end
  203. end