ticket_overview_list.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Copyright (C) 2012-2022 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. name: overview.name,
  91. prio: overview.prio,
  92. link: overview.link,
  93. count: index[:count],
  94. }
  95. indexes.push meta
  96. end
  97. if @client
  98. @client.log "push overview_index for user #{@user.id}"
  99. @client.send(
  100. event: 'ticket_overview_index',
  101. data: indexes,
  102. )
  103. end
  104. @time_now = Time.zone.now.to_i
  105. # push overviews
  106. results = []
  107. assets = AssetsSet.new
  108. index_and_lists.each do |data|
  109. # do not deliver unchanged lists
  110. next if @last_overview[data[:overview][:id]] == [data[:tickets], data[:overview]]
  111. @last_overview[data[:overview][:id]] = [data[:tickets], data[:overview]]
  112. overview = Overview.lookup(id: data[:overview][:id])
  113. next if !overview
  114. if asset_needed?(overview)
  115. assets = asset_push(overview, assets)
  116. end
  117. data[:tickets].each do |ticket_meta|
  118. next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
  119. ticket = Ticket.lookup(id: ticket_meta[:id])
  120. next if !ticket
  121. assets = asset_push(ticket, assets)
  122. end
  123. data[:assets] = assets.to_h
  124. if @client
  125. @client.log "push overview_list #{overview.link} for user #{@user.id}"
  126. # send update to browser
  127. @client.send(
  128. event: 'ticket_overview_list',
  129. data: data,
  130. )
  131. else
  132. result = {
  133. event: 'ticket_overview_list',
  134. data: data,
  135. }
  136. results.push result
  137. end
  138. assets.flush
  139. end
  140. return results if !@client
  141. nil
  142. end
  143. def overview_changed?
  144. # check if min one overview has changed
  145. last_overview_change = Overview.latest_change
  146. return false if last_overview_change == @last_overview_change
  147. @last_overview_change = last_overview_change
  148. true
  149. end
  150. def ticket_changed?(reset = false)
  151. # check if min one ticket has changed
  152. last_ticket_change = Ticket.latest_change
  153. return false if last_ticket_change == @last_ticket_change
  154. @last_ticket_change = last_ticket_change if reset
  155. true
  156. end
  157. end