From 50ab8068aafc6d3c8e50755185f70ab0036c96bd Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 22 Nov 2023 15:51:11 +0100 Subject: [PATCH] refactor: use server network-info route --- src/models/streaming_server.rs | 108 ++++++++++++++++----- src/runtime/msg/internal.rs | 8 +- src/types/streaming_server/mod.rs | 6 ++ src/types/streaming_server/network_info.rs | 7 ++ src/types/streaming_server/response.rs | 19 ++++ src/types/streaming_server/settings.rs | 33 +------ 6 files changed, 120 insertions(+), 61 deletions(-) create mode 100644 src/types/streaming_server/network_info.rs create mode 100644 src/types/streaming_server/response.rs diff --git a/src/models/streaming_server.rs b/src/models/streaming_server.rs index 15fed876e..ce3bbfebc 100644 --- a/src/models/streaming_server.rs +++ b/src/models/streaming_server.rs @@ -8,7 +8,9 @@ use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvError, EnvFutureExt, use crate::types::addon::ResourcePath; use crate::types::api::SuccessResponse; use crate::types::profile::{AuthKey, Profile}; -use crate::types::streaming_server::{Settings, Statistics}; +use crate::types::streaming_server::{ + GetHTTPSResponse, NetworkInfo, Settings, SettingsResponse, Statistics, +}; use enclose::enclose; use futures::{FutureExt, TryFutureExt}; use http::request::Request; @@ -33,14 +35,6 @@ pub struct StatisticsRequest { pub file_idx: u16, } -#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct GetHTTPSResponse { - pub ip_address: String, - pub domain: String, - pub port: u16, -} - #[derive(Clone, Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Selected { @@ -56,6 +50,7 @@ pub struct StreamingServer { pub base_url: Option, pub remote_url: Option, pub playback_devices: Loadable, EnvError>, + pub network_info: Loadable, pub torrent: Option<(String, Loadable)>, pub statistics: Option>, } @@ -65,6 +60,7 @@ impl StreamingServer { let effects = Effects::many(vec![ get_settings::(&profile.settings.streaming_server_url), get_playback_devices::(&profile.settings.streaming_server_url), + get_network_info::(&profile.settings.streaming_server_url), ]); ( Self { @@ -76,6 +72,7 @@ impl StreamingServer { base_url: None, remote_url: None, playback_devices: Loadable::Loading, + network_info: Loadable::Loading, torrent: None, statistics: None, }, @@ -89,14 +86,17 @@ impl UpdateWithCtx for StreamingServer { match msg { Msg::Action(Action::StreamingServer(ActionStreamingServer::Reload)) => { let settings_effects = eq_update(&mut self.settings, Loadable::Loading); + let network_info_effects = eq_update(&mut self.network_info, Loadable::Loading); let base_url_effects = eq_update(&mut self.base_url, None); let remote_url_effects = eq_update(&mut self.remote_url, None); Effects::many(vec![ get_settings::(&self.selected.transport_url), get_playback_devices::(&self.selected.transport_url), + get_network_info::(&self.selected.transport_url), ]) .unchanged() .join(settings_effects) + .join(network_info_effects) .join(base_url_effects) .join(remote_url_effects) } @@ -215,6 +215,7 @@ impl UpdateWithCtx for StreamingServer { statistics: None, }; self.settings = Loadable::Loading; + self.network_info = Loadable::Loading; self.base_url = None; self.remote_url = None; self.torrent = None; @@ -222,6 +223,7 @@ impl UpdateWithCtx for StreamingServer { Effects::many(vec![ get_settings::(&self.selected.transport_url), get_playback_devices::(&self.selected.transport_url), + get_network_info::(&self.selected.transport_url), ]) } Msg::Internal(Internal::StreamingServerSettingsResult(url, result)) @@ -229,14 +231,16 @@ impl UpdateWithCtx for StreamingServer { { match result { Ok(settings) => { - let settings_effects = - eq_update(&mut self.settings, Loadable::Ready(settings.to_owned())); + let settings_effects = eq_update( + &mut self.settings, + Loadable::Ready(settings.values.to_owned()), + ); let base_url_effects = eq_update(&mut self.base_url, Some(settings.base_url.to_owned())); let remote_url_effects = update_remote_url::( &mut self.remote_url, &self.selected, - settings, + &settings.values, ctx, ); settings_effects @@ -248,12 +252,15 @@ impl UpdateWithCtx for StreamingServer { let remote_url_effects = eq_update(&mut self.remote_url, None); let playback_devices_effects = eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned())); + let network_info_effects = + eq_update(&mut self.network_info, Loadable::Err(error.to_owned())); let settings_effects = eq_update(&mut self.settings, Loadable::Err(error.to_owned())); let torrent_effects = eq_update(&mut self.torrent, None); base_url_effects .join(remote_url_effects) .join(playback_devices_effects) + .join(network_info_effects) .join(settings_effects) .join(torrent_effects) } @@ -272,12 +279,42 @@ impl UpdateWithCtx for StreamingServer { let remote_url_effects = eq_update(&mut self.remote_url, None); let playback_devices_effects = eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned())); + let network_info_effects = + eq_update(&mut self.network_info, Loadable::Err(error.to_owned())); let settings_effects = eq_update(&mut self.settings, Loadable::Err(error.to_owned())); let torrent_effects = eq_update(&mut self.torrent, None); base_url_effects .join(remote_url_effects) .join(playback_devices_effects) + .join(network_info_effects) + .join(settings_effects) + .join(torrent_effects) + } + } + } + Msg::Internal(Internal::StreamingServerNetworkInfoResult(url, result)) + if self.selected.transport_url == *url && self.network_info.is_loading() => + { + match result { + Ok(network_info) => eq_update( + &mut self.network_info, + Loadable::Ready(network_info.to_owned()), + ), + Err(error) => { + let base_url_effects = eq_update(&mut self.base_url, None); + let remote_url_effects = eq_update(&mut self.remote_url, None); + let playback_devices_effects = + eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned())); + let network_info_effects = + eq_update(&mut self.network_info, Loadable::Err(error.to_owned())); + let settings_effects = + eq_update(&mut self.settings, Loadable::Err(error.to_owned())); + let torrent_effects = eq_update(&mut self.torrent, None); + base_url_effects + .join(remote_url_effects) + .join(playback_devices_effects) + .join(network_info_effects) .join(settings_effects) .join(torrent_effects) } @@ -291,11 +328,17 @@ impl UpdateWithCtx for StreamingServer { Err(error) => { let base_url_effects = eq_update(&mut self.base_url, None); let remote_url_effects = eq_update(&mut self.remote_url, None); + let playback_devices_effects = + eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned())); + let network_info_effects = + eq_update(&mut self.network_info, Loadable::Err(error.to_owned())); let settings_effects = eq_update(&mut self.settings, Loadable::Err(error.to_owned())); let torrent_effects = eq_update(&mut self.torrent, None); base_url_effects .join(remote_url_effects) + .join(playback_devices_effects) + .join(network_info_effects) .join(settings_effects) .join(torrent_effects) } @@ -364,7 +407,7 @@ fn get_settings(url: &Url) -> Effect { .body(()) .expect("request builder failed"); EffectFuture::Concurrent( - E::fetch::<_, Settings>(request) + E::fetch::<_, SettingsResponse>(request) .map(enclose!((url) move |result| { Msg::Internal(Internal::StreamingServerSettingsResult( url, result, @@ -391,6 +434,22 @@ fn get_playback_devices(url: &Url) -> Effect { .into() } +fn get_network_info(url: &Url) -> Effect { + let endpoint = url.join("network-info").expect("url builder failed"); + let request = Request::get(endpoint.as_str()) + .body(()) + .expect("request builder failed"); + EffectFuture::Concurrent( + E::fetch::<_, NetworkInfo>(request) + .map_ok(|resp| resp) + .map(enclose!((url) move |result| + Msg::Internal(Internal::StreamingServerNetworkInfoResult(url, result)) + )) + .boxed_env(), + ) + .into() +} + fn set_settings(url: &Url, settings: &Settings) -> Effect { #[derive(Serialize)] #[serde(rename_all = "camelCase")] @@ -405,14 +464,14 @@ fn set_settings(url: &Url, settings: &Settings) -> Effect { bt_min_peers_for_stable: u64, } let body = Body { - remote_https: settings.values.remote_https.to_owned(), - cache_size: settings.values.cache_size.to_owned(), - bt_max_connections: settings.values.bt_max_connections.to_owned(), - bt_handshake_timeout: settings.values.bt_handshake_timeout.to_owned(), - bt_request_timeout: settings.values.bt_request_timeout.to_owned(), - bt_download_speed_soft_limit: settings.values.bt_download_speed_soft_limit.to_owned(), - bt_download_speed_hard_limit: settings.values.bt_download_speed_hard_limit.to_owned(), - bt_min_peers_for_stable: settings.values.bt_min_peers_for_stable.to_owned(), + remote_https: settings.remote_https.to_owned(), + cache_size: settings.cache_size.to_owned(), + bt_max_connections: settings.bt_max_connections.to_owned(), + bt_handshake_timeout: settings.bt_handshake_timeout.to_owned(), + bt_request_timeout: settings.bt_request_timeout.to_owned(), + bt_download_speed_soft_limit: settings.bt_download_speed_soft_limit.to_owned(), + bt_download_speed_hard_limit: settings.bt_download_speed_hard_limit.to_owned(), + bt_min_peers_for_stable: settings.bt_min_peers_for_stable.to_owned(), }; let endpoint = url.join("settings").expect("url builder failed"); let request = Request::post(endpoint.as_str()) @@ -636,11 +695,8 @@ fn update_remote_url( settings: &Settings, ctx: &Ctx, ) -> Effects { - match ( - settings.values.remote_https.as_ref(), - ctx.profile.auth_key(), - ) { - (Some(ip_address), Some(auth_key)) if ip_address.is_empty() => Effects::one( + match (settings.remote_https.as_ref(), ctx.profile.auth_key()) { + (Some(ip_address), Some(auth_key)) if !ip_address.is_empty() => Effects::one( get_https_endpoint::(&selected.transport_url, auth_key, ip_address), ) .unchanged(), diff --git a/src/runtime/msg/internal.rs b/src/runtime/msg/internal.rs index 3aee08251..63fb0b172 100644 --- a/src/runtime/msg/internal.rs +++ b/src/runtime/msg/internal.rs @@ -3,7 +3,7 @@ use url::Url; use crate::models::ctx::CtxError; use crate::models::link::LinkError; use crate::models::local_search::Searchable; -use crate::models::streaming_server::{GetHTTPSResponse, PlaybackDevice, StatisticsRequest}; +use crate::models::streaming_server::{PlaybackDevice, StatisticsRequest}; use crate::runtime::EnvError; use crate::types::addon::{Descriptor, Manifest, ResourceRequest, ResourceResponse}; use crate::types::api::{ @@ -13,7 +13,7 @@ use crate::types::api::{ use crate::types::library::{LibraryBucket, LibraryItem, LibraryItemId}; use crate::types::profile::{Auth, AuthKey, Profile, User}; use crate::types::resource::Stream; -use crate::types::streaming_server::{Settings as StreamingServerSettings, Statistics}; +use crate::types::streaming_server::{GetHTTPSResponse, NetworkInfo, SettingsResponse, Statistics}; pub type CtxStorageResponse = ( Option, @@ -79,11 +79,13 @@ pub enum Internal { /// Result for loading link data. LinkDataResult(String, Result), /// Result for loading streaming server settings. - StreamingServerSettingsResult(Url, Result), + StreamingServerSettingsResult(Url, Result), /// Result for loading streaming server base url. StreamingServerBaseURLResult(Url, Result), // Result for loading streaming server playback devices. StreamingServerPlaybackDevicesResult(Url, Result, EnvError>), + // Result for network info. + StreamingServerNetworkInfoResult(Url, Result), /// Result for updating streaming server settings. StreamingServerUpdateSettingsResult(Url, Result<(), EnvError>), /// Result for creating a torrent. diff --git a/src/types/streaming_server/mod.rs b/src/types/streaming_server/mod.rs index 55af229eb..f66e059a7 100644 --- a/src/types/streaming_server/mod.rs +++ b/src/types/streaming_server/mod.rs @@ -1,3 +1,9 @@ +mod network_info; +pub use network_info::*; + +mod response; +pub use response::*; + mod settings; pub use settings::*; diff --git a/src/types/streaming_server/network_info.rs b/src/types/streaming_server/network_info.rs new file mode 100644 index 000000000..7cebdbf0e --- /dev/null +++ b/src/types/streaming_server/network_info.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct NetworkInfo { + pub available_interfaces: Vec, +} diff --git a/src/types/streaming_server/response.rs b/src/types/streaming_server/response.rs new file mode 100644 index 000000000..ffb80d84c --- /dev/null +++ b/src/types/streaming_server/response.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; +use url::Url; + +use crate::types::streaming_server::Settings; + +#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct SettingsResponse { + pub base_url: Url, + pub values: Settings, +} + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct GetHTTPSResponse { + pub ip_address: String, + pub domain: String, + pub port: u16, +} diff --git a/src/types/streaming_server/settings.rs b/src/types/streaming_server/settings.rs index 6e370062c..8356bf641 100644 --- a/src/types/streaming_server/settings.rs +++ b/src/types/streaming_server/settings.rs @@ -1,31 +1,8 @@ use serde::{Deserialize, Serialize}; -use url::Url; - -#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum SettingsOptionSelectionValue { - String(String), - Number(u64), - Float(f64), -} #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct SettingsOptionSelection { - pub name: String, - pub val: Option, -} - -#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct SettingsOption { - pub id: String, - pub selections: Option>, -} - -#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct SettingsValues { +pub struct Settings { pub app_path: String, pub cache_root: String, pub server_version: String, @@ -38,11 +15,3 @@ pub struct SettingsValues { pub bt_download_speed_hard_limit: f64, pub bt_min_peers_for_stable: u64, } - -#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct Settings { - pub base_url: Url, - pub values: SettingsValues, - pub options: Vec, -}