diff --git a/lib/pages/boot/start_up.rb b/lib/pages/boot/start_up.rb index 0c7605a..4d8d923 100644 --- a/lib/pages/boot/start_up.rb +++ b/lib/pages/boot/start_up.rb @@ -75,10 +75,25 @@ module W3DHubLauncher def step_launcher_update @initialization_container.append do tagline "Checking for updates..." - caption "Launcher version OK", margin_left: LARGE_PADDING end - after(1000) do + Worker::Api.fetch_url("https://api.github.com/repos/cyberarm/w3d_hub_linux_launcher/releases/latest") do |result| + if result.okay? + hash = JSON.parse(result.data) + + @initialization_container.append do + if hash["name"] && Gem::Version.new(hash["name"].sub("v", "")) > Gem::Version.new(W3DHubLauncher::VERSION) + caption "Launcher update available #{hash["version"]} (#{W3DHubLauncher::VERSION} running)", margin_left: LARGE_PADDING + else + caption "Launcher version OK", margin_left: LARGE_PADDING + end + end + else + @initialization_container.append do + caption "Fetch latest launcher version FAILED", margin_left: LARGE_PADDING + end + end + next_step end end @@ -89,7 +104,7 @@ module W3DHubLauncher end counter = 0 - every(1000) do + every(1000) do |timer| if !MemCache[:servers].nil? || counter >= 5 @initialization_container.append do if MemCache[:servers] @@ -98,6 +113,7 @@ module W3DHubLauncher caption "Failed to retrieve server listing.", color: 0xff_ff8800, margin_left: LARGE_PADDING end + timer.cancel next_step end end @@ -107,13 +123,53 @@ module W3DHubLauncher end def step_refresh_account_data - @initialization_container.append do - tagline "Updating account information..." - caption "Refresh session OK", margin_left: LARGE_PADDING - caption "Profile data OK", margin_left: LARGE_PADDING + account = MemCache[:settings]&.account + + # user isn't signed in, skip step + if account&.refresh_token.empty? + next_step + + return end - after(1000) do + # Expired or expiring session refresh immediately + if account.access_token_expiration_time < Time.now || (account.access_token_expiration_time - Time.now) < 60 * 60 * 6 # expiring in 6 hours + @initialization_container.append do + tagline "Updating account information..." + end + + Worker::Api.refresh_user_login(account.refresh_token) do |result| + if result.okay? + hash = JSON.parse(result.data) + + MemCache[:settings].account = W3DHubLauncher::Worker::Api::Settings::Account.new(hash) + + Worker::Api.update_settings(MemCache[:settings]) do |settings_result| + if settings_result.okay? + @initialization_container.append do + caption "Refresh session OK", margin_left: LARGE_PADDING + end + + next_step + else + @initialization_container.append do + caption "Refresh session FAILED", margin_left: LARGE_PADDING + end + MemCache[:account_session_expired] = true + + next_step + end + end + else + @initialization_container.append do + caption "Refresh session FAILED", margin_left: LARGE_PADDING + end + MemCache[:account_session_expired] = true + + next_step + end + end + else next_step end end diff --git a/lib/worker/api.rb b/lib/worker/api.rb index 9193729..4a865df 100644 --- a/lib/worker/api.rb +++ b/lib/worker/api.rb @@ -4,7 +4,8 @@ module W3DHubLauncher class Worker class Api # downloads requested resource, returns raw string - def self.fetch_url + def self.fetch_url(url, method = :get, headers = nil, body = nil, &block) + Worker::Request.new(:fetch_url, { url: url, method: method, headers: headers, body: body }, &block) end # downloads requested resource, periodically reporting progress until completion, returning path for file on disk diff --git a/lib/worker/w3dhub_api.rb b/lib/worker/w3dhub_api.rb index 81ceece..ed6f538 100644 --- a/lib/worker/w3dhub_api.rb +++ b/lib/worker/w3dhub_api.rb @@ -181,17 +181,67 @@ module W3DHubLauncher single_source = true end - unless single_source - puts "FIXME: merge applications source" - # We've gotten data back from both backends, merge them. - # pp [primary_result, alternate_result] + if single_source + @applications = JSON.parse(result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || [] - # FIXME: Merge primary and alternate results + result + else # We've gotten data back from both backends, merge them. + primary = JSON.parse(primary_result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || [] + alternate = JSON.parse(alternate_result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || [] + + primary.each do |app| + application = alternate.find { |a| a.id == app.id } + unless application + alternate << app + + # App doesn't exist in alternate list + # comparing channels isn't useful + next + end + + app.channels.each do |chan| + channel = application.channels.find { |c| c.id == chan.id } + + unless channel + application.channels << chan + + # App doesn't have channel in alternate list + # comparing channels isn't useful + next + end + + # If channel versions and access levels match then all is well + if chan.version == channel.version && + chan.user_level == channel.user_level + + # All is well! + next + end + + # If the access levels don't match then overwrite alternate's channel with primary's channel + if chan.user_level != channel.user_level + # Replace alternate's channel with primary's channel + application.channels[application.channels.index(channel)] = chan + + # Replaced, continue. + next + end + + # If versions don't match then pick whichever one is higher + if Gem::Version.new(chan.current_version) > Gem::Version.new(channel.current_version) + # Replace alternate's channel with primary's channel + application.channels[application.channels.index(channel)] = chan + else + # Do nothing, alternate backend version is greater. + end + end + end + + @applications = alternate # alternate is merged with primary + result.data = { applications: alternate }.to_json + + result end - - @applications = JSON.parse(result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || [] - - result end def fetch_news(category)