online_notification.rb 7.4 KB

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