ticket_overview_list.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
  3. def self.reset(user_id)
  4. Rails.cache.write("TicketOverviewPull::#{user_id}", { needed: true })
  5. end
  6. def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 7) # rubocop:disable Lint/MissingSuper
  7. @user = user
  8. @client = client
  9. @client_id = client_id
  10. @ttl = ttl
  11. @asset_lookup = asset_lookup
  12. @last_index_lists = nil
  13. @last_overview = {}
  14. @last_overview_change = nil
  15. @last_ticket_change = nil
  16. @last_full_fetch = nil
  17. end
  18. def self.overview_history_append(overview, user_id)
  19. key = "TicketOverviewHistory::#{user_id}"
  20. history = Rails.cache.read(key) || []
  21. history.prepend overview
  22. history.uniq!
  23. if history.count > 4
  24. history.pop
  25. end
  26. Rails.cache.write(key, history)
  27. end
  28. def self.overview_history_get(user_id)
  29. Rails.cache.read("TicketOverviewHistory::#{user_id}")
  30. end
  31. def load
  32. # get whole collection
  33. index_and_lists = nil
  34. local_overview_changed = overview_changed?
  35. if !@last_index_lists || !@last_full_fetch || @last_full_fetch < (Time.zone.now.to_i - 60) || local_overview_changed
  36. # check if min one ticket has changed
  37. return if !ticket_changed?(true) && !local_overview_changed
  38. index_and_lists = Ticket::Overviews.index(@user)
  39. @last_full_fetch = Time.zone.now.to_i
  40. else
  41. # check if min one ticket has changed
  42. return if !ticket_changed? && !local_overview_changed
  43. index_and_lists_local = Ticket::Overviews.index(@user, Sessions::Backend::TicketOverviewList.overview_history_get(@user.id))
  44. # compare index_and_lists_local to index_and_lists_local
  45. # return if no changes
  46. index_and_lists = []
  47. @last_index_lists.each do |last_index|
  48. found_in_particular_index = false
  49. index_and_lists_local.each do |local_index|
  50. next if local_index[:overview][:id] != last_index[:overview][:id]
  51. index_and_lists.push local_index
  52. found_in_particular_index = true
  53. break
  54. end
  55. next if found_in_particular_index == true
  56. index_and_lists.push last_index
  57. end
  58. end
  59. # no data exists
  60. return if index_and_lists.blank?
  61. # no change exists
  62. return if @last_index_lists == index_and_lists
  63. # remember last state
  64. @last_index_lists = index_and_lists
  65. index_and_lists
  66. end
  67. def local_to_run?
  68. return false if !@time_now
  69. return true if pull_overview?
  70. false
  71. end
  72. def pull_overview?
  73. result = Rails.cache.read("TicketOverviewPull::#{@user.id}")
  74. Rails.cache.delete("TicketOverviewPull::#{@user.id}") if result
  75. return true if result
  76. false
  77. end
  78. def push
  79. return if !to_run? && !local_to_run?
  80. @time_now = Time.zone.now.to_i
  81. # load current data
  82. index_and_lists = load
  83. return if !index_and_lists
  84. # push overview index
  85. indexes = []
  86. index_and_lists.each do |index|
  87. overview = Overview.lookup(id: index[:overview][:id])
  88. next if !overview
  89. meta = {
  90. id: overview.id,
  91. name: overview.name,
  92. prio: overview.prio,
  93. link: overview.link,
  94. count: index[:count],
  95. }
  96. indexes.push meta
  97. end
  98. if @client
  99. @client.log "push overview_index for user #{@user.id}"
  100. @client.send(
  101. event: 'ticket_overview_index',
  102. data: indexes,
  103. )
  104. end
  105. @time_now = Time.zone.now.to_i
  106. # push overviews
  107. results = []
  108. assets = AssetsSet.new
  109. index_and_lists.each do |data|
  110. # do not deliver unchanged lists
  111. next if @last_overview[data[:overview][:id]] == [data[:tickets], data[:overview]]
  112. @last_overview[data[:overview][:id]] = [data[:tickets], data[:overview]]
  113. overview = Overview.lookup(id: data[:overview][:id])
  114. next if !overview
  115. if asset_needed?(overview)
  116. assets = asset_push(overview, assets)
  117. end
  118. data[:tickets].each do |ticket_meta|
  119. next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
  120. ticket = Ticket.lookup(id: ticket_meta[:id])
  121. next if !ticket
  122. assets = asset_push(ticket, assets)
  123. end
  124. data[:assets] = assets.to_h
  125. if @client
  126. @client.log "push overview_list #{overview.link} for user #{@user.id}"
  127. # send update to browser
  128. @client.send(
  129. event: 'ticket_overview_list',
  130. data: data,
  131. )
  132. else
  133. result = {
  134. event: 'ticket_overview_list',
  135. data: data,
  136. }
  137. results.push result
  138. end
  139. assets.flush
  140. end
  141. return results if !@client
  142. nil
  143. end
  144. def overview_changed?
  145. # check if min one overview has changed
  146. last_overview_change = Overview.latest_change
  147. return false if last_overview_change == @last_overview_change
  148. @last_overview_change = last_overview_change
  149. true
  150. end
  151. def ticket_changed?(reset = false)
  152. # check if min one ticket has changed
  153. last_ticket_change = Ticket.latest_change
  154. return false if last_ticket_change == @last_ticket_change
  155. @last_ticket_change = last_ticket_change if reset
  156. true
  157. end
  158. end