2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2016-03-03 08:56:13 +00:00
|
|
|
class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
|
2016-03-03 01:51:24 +00:00
|
|
|
|
|
|
|
def self.reset(user_id)
|
2019-07-25 06:55:56 +00:00
|
|
|
Cache.write("TicketOverviewPull::#{user_id}", { needed: true })
|
2016-03-03 01:51:24 +00:00
|
|
|
end
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 7) # rubocop:disable Lint/MissingSuper
|
2016-03-03 01:51:24 +00:00
|
|
|
@user = user
|
|
|
|
@client = client
|
|
|
|
@client_id = client_id
|
|
|
|
@ttl = ttl
|
2016-03-03 08:56:13 +00:00
|
|
|
@asset_lookup = asset_lookup
|
2019-11-28 06:59:22 +00:00
|
|
|
@last_index_lists = nil
|
2016-03-03 01:51:24 +00:00
|
|
|
@last_overview = {}
|
|
|
|
@last_overview_change = nil
|
|
|
|
@last_ticket_change = nil
|
2019-11-28 06:59:22 +00:00
|
|
|
@last_full_fetch = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.overview_history_append(overview, user_id)
|
|
|
|
key = "TicketOverviewHistory::#{user_id}"
|
2021-05-31 13:05:54 +00:00
|
|
|
history = Cache.read(key) || []
|
2019-11-28 06:59:22 +00:00
|
|
|
|
|
|
|
history.prepend overview
|
|
|
|
history.uniq!
|
|
|
|
if history.count > 4
|
|
|
|
history.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
Cache.write(key, history)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.overview_history_get(user_id)
|
2021-05-31 13:05:54 +00:00
|
|
|
Cache.read("TicketOverviewHistory::#{user_id}")
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# get whole collection
|
2019-11-28 06:59:22 +00:00
|
|
|
index_and_lists = nil
|
|
|
|
local_overview_changed = overview_changed?
|
|
|
|
if !@last_index_lists || !@last_full_fetch || @last_full_fetch < (Time.zone.now.to_i - 60) || local_overview_changed
|
|
|
|
|
|
|
|
# check if min one ticket has changed
|
|
|
|
return if !ticket_changed?(true) && !local_overview_changed
|
|
|
|
|
|
|
|
index_and_lists = Ticket::Overviews.index(@user)
|
|
|
|
@last_full_fetch = Time.zone.now.to_i
|
|
|
|
else
|
|
|
|
|
|
|
|
# check if min one ticket has changed
|
|
|
|
return if !ticket_changed? && !local_overview_changed
|
|
|
|
|
|
|
|
index_and_lists_local = Ticket::Overviews.index(@user, Sessions::Backend::TicketOverviewList.overview_history_get(@user.id))
|
|
|
|
|
|
|
|
# compare index_and_lists_local to index_and_lists_local
|
|
|
|
# return if no changes
|
|
|
|
|
|
|
|
index_and_lists = []
|
|
|
|
@last_index_lists.each do |last_index|
|
|
|
|
found_in_particular_index = false
|
|
|
|
index_and_lists_local.each do |local_index|
|
|
|
|
next if local_index[:overview][:id] != last_index[:overview][:id]
|
|
|
|
|
|
|
|
index_and_lists.push local_index
|
|
|
|
found_in_particular_index = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
next if found_in_particular_index == true
|
|
|
|
|
|
|
|
index_and_lists.push last_index
|
|
|
|
end
|
|
|
|
end
|
2015-05-10 19:54:57 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# no data exists
|
2017-10-16 13:45:04 +00:00
|
|
|
return if index_and_lists.blank?
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# no change exists
|
2019-11-28 06:59:22 +00:00
|
|
|
return if @last_index_lists == index_and_lists
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# remember last state
|
2019-11-28 06:59:22 +00:00
|
|
|
@last_index_lists = index_and_lists
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
index_and_lists
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2019-07-25 06:55:56 +00:00
|
|
|
def local_to_run?
|
|
|
|
return false if !@time_now
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2019-11-28 06:59:22 +00:00
|
|
|
return true if pull_overview?
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_overview?
|
2021-05-31 13:05:54 +00:00
|
|
|
result = Cache.read("TicketOverviewPull::#{@user.id}")
|
2019-07-25 06:55:56 +00:00
|
|
|
Cache.delete("TicketOverviewPull::#{@user.id}") if result
|
|
|
|
return true if result
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-07-25 06:55:56 +00:00
|
|
|
false
|
2016-03-03 01:51:24 +00:00
|
|
|
end
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
def push
|
2019-07-25 06:55:56 +00:00
|
|
|
return if !to_run? && !local_to_run?
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2019-07-25 06:55:56 +00:00
|
|
|
@time_now = Time.zone.now.to_i
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
# load current data
|
2016-03-03 01:51:24 +00:00
|
|
|
index_and_lists = load
|
|
|
|
return if !index_and_lists
|
|
|
|
|
|
|
|
# push overview index
|
|
|
|
indexes = []
|
2017-10-01 12:25:52 +00:00
|
|
|
index_and_lists.each do |index|
|
2016-03-03 01:51:24 +00:00
|
|
|
overview = Overview.lookup(id: index[:overview][:id])
|
2019-05-02 09:08:25 +00:00
|
|
|
next if !overview
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
meta = {
|
2019-01-29 07:32:43 +00:00
|
|
|
name: overview.name,
|
|
|
|
prio: overview.prio,
|
|
|
|
link: overview.link,
|
2016-03-03 01:51:24 +00:00
|
|
|
count: index[:count],
|
|
|
|
}
|
|
|
|
indexes.push meta
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-03-03 01:51:24 +00:00
|
|
|
if @client
|
|
|
|
@client.log "push overview_index for user #{@user.id}"
|
|
|
|
@client.send(
|
|
|
|
event: 'ticket_overview_index',
|
2019-01-29 07:32:43 +00:00
|
|
|
data: indexes,
|
2016-03-03 01:51:24 +00:00
|
|
|
)
|
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2018-08-06 09:43:40 +00:00
|
|
|
@time_now = Time.zone.now.to_i
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# push overviews
|
2014-07-14 05:26:22 +00:00
|
|
|
results = []
|
2019-11-07 11:09:46 +00:00
|
|
|
assets = AssetsSet.new
|
2017-10-01 12:25:52 +00:00
|
|
|
index_and_lists.each do |data|
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
# do not deliver unchanged lists
|
2019-10-02 13:57:08 +00:00
|
|
|
next if @last_overview[data[:overview][:id]] == [data[:tickets], data[:overview]]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-10-02 13:57:08 +00:00
|
|
|
@last_overview[data[:overview][:id]] = [data[:tickets], data[:overview]]
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2016-09-27 22:05:46 +00:00
|
|
|
overview = Overview.lookup(id: data[:overview][:id])
|
2019-04-05 16:13:37 +00:00
|
|
|
next if !overview
|
|
|
|
|
2016-03-03 08:56:13 +00:00
|
|
|
if asset_needed?(overview)
|
2017-10-18 00:09:45 +00:00
|
|
|
assets = asset_push(overview, assets)
|
2016-03-03 08:56:13 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
data[:tickets].each do |ticket_meta|
|
2017-10-18 00:09:45 +00:00
|
|
|
next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
ticket = Ticket.lookup(id: ticket_meta[:id])
|
2019-01-29 07:22:35 +00:00
|
|
|
next if !ticket
|
2019-01-29 07:32:43 +00:00
|
|
|
|
2017-10-18 00:09:45 +00:00
|
|
|
assets = asset_push(ticket, assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-11-07 11:09:46 +00:00
|
|
|
|
|
|
|
data[:assets] = assets.to_h
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2020-11-05 16:31:00 +00:00
|
|
|
if @client
|
2016-03-03 01:51:24 +00:00
|
|
|
@client.log "push overview_list #{overview.link} for user #{@user.id}"
|
2014-07-14 05:26:22 +00:00
|
|
|
|
|
|
|
# send update to browser
|
2015-04-27 15:21:17 +00:00
|
|
|
@client.send(
|
2016-03-03 01:51:24 +00:00
|
|
|
event: 'ticket_overview_list',
|
2019-01-29 07:32:43 +00:00
|
|
|
data: data,
|
2015-04-27 15:21:17 +00:00
|
|
|
)
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
result = {
|
|
|
|
event: 'ticket_overview_list',
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
results.push result
|
2014-07-14 05:26:22 +00:00
|
|
|
end
|
2019-11-07 11:09:46 +00:00
|
|
|
|
|
|
|
assets.flush
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-07-14 05:26:22 +00:00
|
|
|
return results if !@client
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2014-07-14 05:26:22 +00:00
|
|
|
nil
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2019-11-28 06:59:22 +00:00
|
|
|
def overview_changed?
|
|
|
|
|
|
|
|
# check if min one overview has changed
|
|
|
|
last_overview_change = Overview.latest_change
|
|
|
|
return false if last_overview_change == @last_overview_change
|
|
|
|
|
|
|
|
@last_overview_change = last_overview_change
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def ticket_changed?(reset = false)
|
|
|
|
|
|
|
|
# check if min one ticket has changed
|
|
|
|
last_ticket_change = Ticket.latest_change
|
|
|
|
return false if last_ticket_change == @last_ticket_change
|
|
|
|
|
|
|
|
@last_ticket_change = last_ticket_change if reset
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|