mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-11-05 01:24:06 +00:00
Implemented Worker::Api#fetch_url, check for launcher updates from github releases, refresh user session, merge application lists from both backends when user is signed in
This commit is contained in:
@@ -75,10 +75,25 @@ module W3DHubLauncher
|
|||||||
def step_launcher_update
|
def step_launcher_update
|
||||||
@initialization_container.append do
|
@initialization_container.append do
|
||||||
tagline "Checking for updates..."
|
tagline "Checking for updates..."
|
||||||
caption "Launcher version <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
|
||||||
end
|
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 <c=0f0>#{hash["version"]}</c> (#{W3DHubLauncher::VERSION} running)", margin_left: LARGE_PADDING
|
||||||
|
else
|
||||||
|
caption "Launcher version <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
@initialization_container.append do
|
||||||
|
caption "Fetch latest launcher version <c=f80>FAILED</c>", margin_left: LARGE_PADDING
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
next_step
|
next_step
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -89,7 +104,7 @@ module W3DHubLauncher
|
|||||||
end
|
end
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
every(1000) do
|
every(1000) do |timer|
|
||||||
if !MemCache[:servers].nil? || counter >= 5
|
if !MemCache[:servers].nil? || counter >= 5
|
||||||
@initialization_container.append do
|
@initialization_container.append do
|
||||||
if MemCache[:servers]
|
if MemCache[:servers]
|
||||||
@@ -98,6 +113,7 @@ module W3DHubLauncher
|
|||||||
caption "Failed to retrieve server listing.", color: 0xff_ff8800, margin_left: LARGE_PADDING
|
caption "Failed to retrieve server listing.", color: 0xff_ff8800, margin_left: LARGE_PADDING
|
||||||
end
|
end
|
||||||
|
|
||||||
|
timer.cancel
|
||||||
next_step
|
next_step
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -107,13 +123,53 @@ module W3DHubLauncher
|
|||||||
end
|
end
|
||||||
|
|
||||||
def step_refresh_account_data
|
def step_refresh_account_data
|
||||||
@initialization_container.append do
|
account = MemCache[:settings]&.account
|
||||||
tagline "Updating account information..."
|
|
||||||
caption "Refresh session <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
# user isn't signed in, skip step
|
||||||
caption "Profile data <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
if account&.refresh_token.empty?
|
||||||
|
next_step
|
||||||
|
|
||||||
|
return
|
||||||
end
|
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 <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
||||||
|
end
|
||||||
|
|
||||||
|
next_step
|
||||||
|
else
|
||||||
|
@initialization_container.append do
|
||||||
|
caption "Refresh session <c=f80>FAILED</c>", margin_left: LARGE_PADDING
|
||||||
|
end
|
||||||
|
MemCache[:account_session_expired] = true
|
||||||
|
|
||||||
|
next_step
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
@initialization_container.append do
|
||||||
|
caption "Refresh session <c=f80>FAILED</c>", margin_left: LARGE_PADDING
|
||||||
|
end
|
||||||
|
MemCache[:account_session_expired] = true
|
||||||
|
|
||||||
|
next_step
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
next_step
|
next_step
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ module W3DHubLauncher
|
|||||||
class Worker
|
class Worker
|
||||||
class Api
|
class Api
|
||||||
# downloads requested resource, returns raw string
|
# 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
|
end
|
||||||
|
|
||||||
# downloads requested resource, periodically reporting progress until completion, returning path for file on disk
|
# downloads requested resource, periodically reporting progress until completion, returning path for file on disk
|
||||||
|
|||||||
@@ -181,17 +181,67 @@ module W3DHubLauncher
|
|||||||
single_source = true
|
single_source = true
|
||||||
end
|
end
|
||||||
|
|
||||||
unless single_source
|
if single_source
|
||||||
puts "FIXME: merge applications source"
|
@applications = JSON.parse(result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || []
|
||||||
# We've gotten data back from both backends, merge them.
|
|
||||||
# pp [primary_result, alternate_result]
|
|
||||||
|
|
||||||
# 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
|
end
|
||||||
|
|
||||||
@applications = JSON.parse(result.data)["applications"]&.map { |app| W3DHubLauncher::Worker::Api::Application.new(app) } || []
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_news(category)
|
def fetch_news(category)
|
||||||
|
|||||||
Reference in New Issue
Block a user