Skip to content

Commit

Permalink
refactor: use server network-info route
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Nov 22, 2023
1 parent 5b71c1d commit 50ab806
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 61 deletions.
108 changes: 82 additions & 26 deletions src/models/streaming_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -56,6 +50,7 @@ pub struct StreamingServer {
pub base_url: Option<Url>,
pub remote_url: Option<Url>,
pub playback_devices: Loadable<Vec<PlaybackDevice>, EnvError>,
pub network_info: Loadable<NetworkInfo, EnvError>,
pub torrent: Option<(String, Loadable<ResourcePath, EnvError>)>,
pub statistics: Option<Loadable<Statistics, EnvError>>,
}
Expand All @@ -65,6 +60,7 @@ impl StreamingServer {
let effects = Effects::many(vec![
get_settings::<E>(&profile.settings.streaming_server_url),
get_playback_devices::<E>(&profile.settings.streaming_server_url),
get_network_info::<E>(&profile.settings.streaming_server_url),
]);
(
Self {
Expand All @@ -76,6 +72,7 @@ impl StreamingServer {
base_url: None,
remote_url: None,
playback_devices: Loadable::Loading,
network_info: Loadable::Loading,
torrent: None,
statistics: None,
},
Expand All @@ -89,14 +86,17 @@ impl<E: Env + 'static> UpdateWithCtx<E> 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::<E>(&self.selected.transport_url),
get_playback_devices::<E>(&self.selected.transport_url),
get_network_info::<E>(&self.selected.transport_url),
])
.unchanged()
.join(settings_effects)
.join(network_info_effects)
.join(base_url_effects)
.join(remote_url_effects)
}
Expand Down Expand Up @@ -215,28 +215,32 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
statistics: None,
};
self.settings = Loadable::Loading;
self.network_info = Loadable::Loading;
self.base_url = None;
self.remote_url = None;
self.torrent = None;
self.statistics = None;
Effects::many(vec![
get_settings::<E>(&self.selected.transport_url),
get_playback_devices::<E>(&self.selected.transport_url),
get_network_info::<E>(&self.selected.transport_url),
])
}
Msg::Internal(Internal::StreamingServerSettingsResult(url, result))
if self.selected.transport_url == *url && self.settings.is_loading() =>
{
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::<E>(
&mut self.remote_url,
&self.selected,
settings,
&settings.values,
ctx,
);
settings_effects
Expand All @@ -248,12 +252,15 @@ impl<E: Env + 'static> UpdateWithCtx<E> 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)
}
Expand All @@ -272,12 +279,42 @@ impl<E: Env + 'static> UpdateWithCtx<E> 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)
}
Expand All @@ -291,11 +328,17 @@ impl<E: Env + 'static> UpdateWithCtx<E> 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)
}
Expand Down Expand Up @@ -364,7 +407,7 @@ fn get_settings<E: Env + 'static>(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,
Expand All @@ -391,6 +434,22 @@ fn get_playback_devices<E: Env + 'static>(url: &Url) -> Effect {
.into()
}

fn get_network_info<E: Env + 'static>(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<E: Env + 'static>(url: &Url, settings: &Settings) -> Effect {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -405,14 +464,14 @@ fn set_settings<E: Env + 'static>(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())
Expand Down Expand Up @@ -636,11 +695,8 @@ fn update_remote_url<E: Env + 'static>(
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::<E>(&selected.transport_url, auth_key, ip_address),
)
.unchanged(),
Expand Down
8 changes: 5 additions & 3 deletions src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Profile>,
Expand Down Expand Up @@ -79,11 +79,13 @@ pub enum Internal {
/// Result for loading link data.
LinkDataResult(String, Result<LinkDataResponse, LinkError>),
/// Result for loading streaming server settings.
StreamingServerSettingsResult(Url, Result<StreamingServerSettings, EnvError>),
StreamingServerSettingsResult(Url, Result<SettingsResponse, EnvError>),
/// Result for loading streaming server base url.
StreamingServerBaseURLResult(Url, Result<Url, EnvError>),
// Result for loading streaming server playback devices.
StreamingServerPlaybackDevicesResult(Url, Result<Vec<PlaybackDevice>, EnvError>),
// Result for network info.
StreamingServerNetworkInfoResult(Url, Result<NetworkInfo, EnvError>),
/// Result for updating streaming server settings.
StreamingServerUpdateSettingsResult(Url, Result<(), EnvError>),
/// Result for creating a torrent.
Expand Down
6 changes: 6 additions & 0 deletions src/types/streaming_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
mod network_info;
pub use network_info::*;

mod response;
pub use response::*;

mod settings;
pub use settings::*;

Expand Down
7 changes: 7 additions & 0 deletions src/types/streaming_server/network_info.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}
19 changes: 19 additions & 0 deletions src/types/streaming_server/response.rs
Original file line number Diff line number Diff line change
@@ -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,
}
33 changes: 1 addition & 32 deletions src/types/streaming_server/settings.rs
Original file line number Diff line number Diff line change
@@ -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<SettingsOptionSelectionValue>,
}

#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SettingsOption {
pub id: String,
pub selections: Option<Vec<SettingsOptionSelection>>,
}

#[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,
Expand All @@ -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<SettingsOption>,
}

0 comments on commit 50ab806

Please sign in to comment.