trabajo-afectivo/lib/sessions/backend/ticket_overview_list.rb

144 lines
3.4 KiB
Ruby
Raw Normal View History

2016-03-03 08:56:13 +00:00
class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
def self.reset(user_id)
key = "TicketOverviewPull::#{user_id}"
Cache.write(key, { needed: true })
end
2016-03-03 08:56:13 +00:00
def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8)
@user = user
@client = client
@client_id = client_id
@ttl = ttl
2016-03-03 08:56:13 +00:00
@asset_lookup = asset_lookup
@last_change = nil
@last_overview = {}
@last_overview_change = nil
@last_ticket_change = nil
@time_now = Time.zone.now.to_i
2014-07-13 18:52:32 +00:00
end
def load
2014-07-13 18:52:32 +00:00
# get whole collection
index_and_lists = Ticket::Overviews.index(@user)
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
return if @last_change == index_and_lists
2014-07-13 18:52:32 +00:00
# remember last state
@last_change = index_and_lists
2014-07-13 18:52:32 +00:00
index_and_lists
end
2014-07-13 18:52:32 +00:00
def client_key
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
2014-07-13 18:52:32 +00:00
end
def work_needed?
key = "TicketOverviewPull::#{@user.id}"
if Cache.get(key)
Cache.delete(key)
return true
end
return false if Sessions::CacheIn.get(client_key)
true
end
2014-07-13 18:52:32 +00:00
def push
return if !work_needed?
2014-07-13 18:52:32 +00:00
# reset check interval
2016-03-01 14:26:46 +00:00
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
2014-07-13 18:52:32 +00:00
# check if min one ticket or overview has changed
last_overview_change = Overview.latest_change
last_ticket_change = Ticket.latest_change
return if last_ticket_change == @last_ticket_change && last_overview_change == @last_overview_change
@last_overview_change = last_overview_change
@last_ticket_change = last_ticket_change
# load current data
index_and_lists = load
return if !index_and_lists
# push overview index
indexes = []
index_and_lists.each do |index|
overview = Overview.lookup(id: index[:overview][:id])
next if !overview
meta = {
2019-01-29 07:32:43 +00:00
name: overview.name,
prio: overview.prio,
link: overview.link,
count: index[:count],
}
indexes.push meta
end
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,
)
end
2014-07-13 18:52:32 +00:00
@time_now = Time.zone.now.to_i
2014-07-13 18:52:32 +00:00
# push overviews
results = []
index_and_lists.each do |data|
# do not deliver unchanged lists
next if @last_overview[data[:overview][:id]] == data
@last_overview[data[:overview][:id]] = data
2014-07-13 18:52:32 +00:00
assets = {}
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)
assets = asset_push(overview, assets)
2016-03-03 08:56:13 +00:00
end
data[:tickets].each do |ticket_meta|
next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
ticket = Ticket.lookup(id: ticket_meta[:id])
next if !ticket
2019-01-29 07:32:43 +00:00
assets = asset_push(ticket, assets)
end
data[:assets] = assets
2014-07-13 18:52:32 +00:00
if !@client
result = {
event: 'ticket_overview_list',
2019-01-29 07:32:43 +00:00
data: data,
}
results.push result
else
@client.log "push overview_list #{overview.link} for user #{@user.id}"
# send update to browser
2015-04-27 15:21:17 +00:00
@client.send(
event: 'ticket_overview_list',
2019-01-29 07:32:43 +00:00
data: data,
2015-04-27 15:21:17 +00:00
)
end
end
return results if !@client
nil
end
end