From 75dd52f60c65314c4b79df8bc764c33f9cc9571e Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sun, 19 Nov 2023 11:52:49 -0800 Subject: [PATCH 1/6] surround_sound_enabled setting added --- src/constants.rs | 2 +- src/runtime/env.rs | 75 +++++++++++++++++++++- src/types/profile/settings.rs | 2 + src/unit_tests/serde/default_tokens_ext.rs | 4 +- src/unit_tests/serde/settings.rs | 9 ++- 5 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 83dc2f3fc..5e6ac0970 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -31,7 +31,7 @@ pub const NOTIFICATION_ITEMS_COUNT: usize = 100; pub const WATCHED_THRESHOLD_COEF: f64 = 0.7; pub const CREDITS_THRESHOLD_COEF: f64 = 0.9; /// The latest migration scheme version -pub const SCHEMA_VERSION: u32 = 9; +pub const SCHEMA_VERSION: u32 = 10; pub const IMDB_LINK_CATEGORY: &str = "imdb"; pub const GENRES_LINK_CATEGORY: &str = "Genres"; pub const CINEMETA_TOP_CATALOG_ID: &str = "top"; diff --git a/src/runtime/env.rs b/src/runtime/env.rs index 8947f5989..c7f1a0463 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -232,6 +232,12 @@ pub trait Env { .await?; schema_version = 9; } + if schema_version == 9 { + migrate_storage_schema_to_v10::() + .map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error))) + .await?; + schema_version = 10; + } if schema_version != SCHEMA_VERSION { panic!( "Storage schema version must be upgraded from {} to {}", @@ -491,6 +497,29 @@ fn migrate_storage_schema_to_v9() -> TryEnvFuture<()> { .boxed_env() } +fn migrate_storage_schema_to_v10() -> TryEnvFuture<()> { + E::get_storage::(PROFILE_STORAGE_KEY) + .and_then(|mut profile| { + match profile + .as_mut() + .and_then(|profile| profile.as_object_mut()) + .and_then(|profile| profile.get_mut("settings")) + .and_then(|settings| settings.as_object_mut()) + { + Some(settings) => { + settings.insert( + "surroundSoundEnabled".to_owned(), + serde_json::Value::Bool(false), + ); + E::set_storage(PROFILE_STORAGE_KEY, Some(&profile)) + } + _ => E::set_storage::<()>(PROFILE_STORAGE_KEY, None), + } + }) + .and_then(|_| E::set_storage(SCHEMA_VERSION_STORAGE_KEY, Some(&10))) + .boxed_env() +} + #[cfg(test)] mod test { use serde_json::{json, Value}; @@ -501,8 +530,9 @@ mod test { }, runtime::{ env::{ - migrate_storage_schema_to_v6, migrate_storage_schema_to_v7, - migrate_storage_schema_to_v8, migrate_storage_schema_to_v9, + migrate_storage_schema_to_v10, migrate_storage_schema_to_v6, + migrate_storage_schema_to_v7, migrate_storage_schema_to_v8, + migrate_storage_schema_to_v9, }, Env, }, @@ -865,4 +895,45 @@ mod test { ); } } + + #[tokio::test] + async fn test_migration_from_9_to_10() { + { + let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv"); + let profile_before = json!({ + "settings": {} + }); + + let migrated_profile = json!({ + "settings": { + "surroundSoundEnabled": false, + } + }); + + // setup storage for migration + set_profile_and_schema_version(&profile_before, 9); + + // migrate storage + migrate_storage_schema_to_v10::() + .await + .expect("Should migrate"); + + let storage = STORAGE.read().expect("Should lock"); + + assert_eq!( + &10.to_string(), + storage + .get(SCHEMA_VERSION_STORAGE_KEY) + .expect("Should have the schema set"), + "Scheme version should now be updated" + ); + assert_eq!( + &migrated_profile.to_string(), + storage + .get(PROFILE_STORAGE_KEY) + .expect("Should have the profile set"), + "Profile should match" + ); + } + } } diff --git a/src/types/profile/settings.rs b/src/types/profile/settings.rs index dc4e03003..5048af70b 100644 --- a/src/types/profile/settings.rs +++ b/src/types/profile/settings.rs @@ -35,6 +35,7 @@ pub struct Settings { pub seek_short_time_duration: u32, /// Whether we should pause the playback when the application get's minimized pub pause_on_minimize: bool, + pub surround_sound_enabled: bool, pub streaming_server_warning_dismissed: Option>, } @@ -72,6 +73,7 @@ impl Default for Settings { seek_time_duration: 10000, seek_short_time_duration: 3000, pause_on_minimize: false, + surround_sound_enabled: false, streaming_server_warning_dismissed: None, } } diff --git a/src/unit_tests/serde/default_tokens_ext.rs b/src/unit_tests/serde/default_tokens_ext.rs index 52b35569a..4d97580ea 100644 --- a/src/unit_tests/serde/default_tokens_ext.rs +++ b/src/unit_tests/serde/default_tokens_ext.rs @@ -371,7 +371,7 @@ impl DefaultTokens for Settings { vec![ Token::Struct { name: "Settings", - len: 25, + len: 26, }, Token::Str("interfaceLanguage"), Token::Str("eng"), @@ -424,6 +424,8 @@ impl DefaultTokens for Settings { Token::U32(3000), Token::Str("pauseOnMinimize"), Token::Bool(false), + Token::Str("surroundSoundEnabled"), + Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, Token::StructEnd, diff --git a/src/unit_tests/serde/settings.rs b/src/unit_tests/serde/settings.rs index e02c31cdf..fac9ad3c0 100644 --- a/src/unit_tests/serde/settings.rs +++ b/src/unit_tests/serde/settings.rs @@ -31,6 +31,7 @@ fn settings() { seek_time_duration: 10, seek_short_time_duration: 3, pause_on_minimize: true, + surround_sound_enabled: false, streaming_server_warning_dismissed: Some( Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap(), ), @@ -38,7 +39,7 @@ fn settings() { &[ Token::Struct { name: "Settings", - len: 25, + len: 26, }, Token::Str("interfaceLanguage"), Token::Str("interface_language"), @@ -94,6 +95,8 @@ fn settings() { Token::U32(3), Token::Str("pauseOnMinimize"), Token::Bool(true), + Token::Str("surroundSoundEnabled"), + Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::Some, Token::Str("2021-01-01T00:00:00Z"), @@ -109,7 +112,7 @@ fn settings_de() { &[ Token::Struct { name: "Settings", - len: 20, + len: 21, }, Token::Str("interfaceLanguage"), Token::Str("eng"), @@ -158,6 +161,8 @@ fn settings_de() { Token::U32(3000), Token::Str("pauseOnMinimize"), Token::Bool(false), + Token::Str("surroundSoundEnabled"), + Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, Token::StructEnd, From 5d5f9444d6003b5462bc05cee4109aba13128117 Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sat, 25 Nov 2023 06:41:36 -0800 Subject: [PATCH 2/6] migration fixed after pull --- src/constants.rs | 2 +- src/runtime/env.rs | 12 +++++++++--- src/runtime/msg/action.rs | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index ed3c49a0a..777398b92 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -32,7 +32,7 @@ pub const NOTIFICATION_ITEMS_COUNT: usize = 100; pub const WATCHED_THRESHOLD_COEF: f64 = 0.7; pub const CREDITS_THRESHOLD_COEF: f64 = 0.9; /// The latest migration scheme version -pub const SCHEMA_VERSION: u32 = 10; +pub const SCHEMA_VERSION: u32 = 11; pub const IMDB_LINK_CATEGORY: &str = "imdb"; pub const GENRES_LINK_CATEGORY: &str = "Genres"; pub const CINEMETA_TOP_CATALOG_ID: &str = "top"; diff --git a/src/runtime/env.rs b/src/runtime/env.rs index 012d6e72f..2d5535a4f 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -238,6 +238,12 @@ pub trait Env { .await?; schema_version = 10; } + if schema_version == 10 { + migrate_storage_schema_to_v11::() + .map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error))) + .await?; + schema_version = 11; + } if schema_version != SCHEMA_VERSION { panic!( "Storage schema version must be upgraded from {} to {}", @@ -536,9 +542,9 @@ mod test { }, runtime::{ env::{ - migrate_storage_schema_to_v10, migrate_storage_schema_to_v6, - migrate_storage_schema_to_v7, migrate_storage_schema_to_v8, - migrate_storage_schema_to_v9, + migrate_storage_schema_to_v10, migrate_storage_schema_to_v11, + migrate_storage_schema_to_v6, migrate_storage_schema_to_v7, + migrate_storage_schema_to_v8, migrate_storage_schema_to_v9, }, Env, }, diff --git a/src/runtime/msg/action.rs b/src/runtime/msg/action.rs index 3087dad53..96de5c345 100644 --- a/src/runtime/msg/action.rs +++ b/src/runtime/msg/action.rs @@ -21,7 +21,7 @@ use crate::{ types::{ addon::Descriptor, api::AuthRequest, - library::{LibraryItemId, LibraryItem}, + library::{LibraryItem, LibraryItemId}, profile::Settings as ProfileSettings, resource::{MetaItemId, MetaItemPreview, Video}, }, From 59e647c3b4d0dec232c7a4724dcfd6e7a820b811 Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sat, 25 Nov 2023 06:44:31 -0800 Subject: [PATCH 3/6] test_migration_from_10_to_11 --- src/runtime/env.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runtime/env.rs b/src/runtime/env.rs index 2d5535a4f..83500bd36 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -528,7 +528,7 @@ fn migrate_storage_schema_to_v11() -> TryEnvFuture<()> { _ => E::set_storage::<()>(PROFILE_STORAGE_KEY, None), } }) - .and_then(|_| E::set_storage(SCHEMA_VERSION_STORAGE_KEY, Some(&10))) + .and_then(|_| E::set_storage(SCHEMA_VERSION_STORAGE_KEY, Some(&11))) .boxed_env() } @@ -947,17 +947,17 @@ mod test { }); // setup storage for migration - set_profile_and_schema_version(&profile_before, 9); + set_profile_and_schema_version(&profile_before, 10); // migrate storage - migrate_storage_schema_to_v10::() + migrate_storage_schema_to_v11::() .await .expect("Should migrate"); let storage = STORAGE.read().expect("Should lock"); assert_eq!( - &10.to_string(), + &11.to_string(), storage .get(SCHEMA_VERSION_STORAGE_KEY) .expect("Should have the schema set"), From bef68af6b0db125358a23096a5b92f4eb3f8ad54 Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sat, 25 Nov 2023 06:47:24 -0800 Subject: [PATCH 4/6] fix clippy --- src/runtime/msg/action.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/msg/action.rs b/src/runtime/msg/action.rs index 96de5c345..228500343 100644 --- a/src/runtime/msg/action.rs +++ b/src/runtime/msg/action.rs @@ -21,7 +21,7 @@ use crate::{ types::{ addon::Descriptor, api::AuthRequest, - library::{LibraryItem, LibraryItemId}, + library::LibraryItemId, profile::Settings as ProfileSettings, resource::{MetaItemId, MetaItemPreview, Video}, }, From b9c1df41cc5e1a923c23c90af378ac1c6655ba8f Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sat, 25 Nov 2023 08:26:03 -0800 Subject: [PATCH 5/6] remove `enabled` suffix --- src/runtime/env.rs | 4 ++-- src/types/profile/settings.rs | 4 ++-- src/unit_tests/serde/default_tokens_ext.rs | 2 +- src/unit_tests/serde/settings.rs | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/runtime/env.rs b/src/runtime/env.rs index 83500bd36..df1484c52 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -520,7 +520,7 @@ fn migrate_storage_schema_to_v11() -> TryEnvFuture<()> { { Some(settings) => { settings.insert( - "surroundSoundEnabled".to_owned(), + "surroundSound".to_owned(), serde_json::Value::Bool(false), ); E::set_storage(PROFILE_STORAGE_KEY, Some(&profile)) @@ -942,7 +942,7 @@ mod test { let migrated_profile = json!({ "settings": { - "surroundSoundEnabled": false, + "surroundSound": false, } }); diff --git a/src/types/profile/settings.rs b/src/types/profile/settings.rs index 5048af70b..87ec5f539 100644 --- a/src/types/profile/settings.rs +++ b/src/types/profile/settings.rs @@ -35,7 +35,7 @@ pub struct Settings { pub seek_short_time_duration: u32, /// Whether we should pause the playback when the application get's minimized pub pause_on_minimize: bool, - pub surround_sound_enabled: bool, + pub surround_sound: bool, pub streaming_server_warning_dismissed: Option>, } @@ -73,7 +73,7 @@ impl Default for Settings { seek_time_duration: 10000, seek_short_time_duration: 3000, pause_on_minimize: false, - surround_sound_enabled: false, + surround_sound: false, streaming_server_warning_dismissed: None, } } diff --git a/src/unit_tests/serde/default_tokens_ext.rs b/src/unit_tests/serde/default_tokens_ext.rs index 4d97580ea..6b77bb1f5 100644 --- a/src/unit_tests/serde/default_tokens_ext.rs +++ b/src/unit_tests/serde/default_tokens_ext.rs @@ -424,7 +424,7 @@ impl DefaultTokens for Settings { Token::U32(3000), Token::Str("pauseOnMinimize"), Token::Bool(false), - Token::Str("surroundSoundEnabled"), + Token::Str("surroundSound"), Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, diff --git a/src/unit_tests/serde/settings.rs b/src/unit_tests/serde/settings.rs index fac9ad3c0..cca86c9ee 100644 --- a/src/unit_tests/serde/settings.rs +++ b/src/unit_tests/serde/settings.rs @@ -31,7 +31,7 @@ fn settings() { seek_time_duration: 10, seek_short_time_duration: 3, pause_on_minimize: true, - surround_sound_enabled: false, + surround_sound: false, streaming_server_warning_dismissed: Some( Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap(), ), @@ -95,7 +95,7 @@ fn settings() { Token::U32(3), Token::Str("pauseOnMinimize"), Token::Bool(true), - Token::Str("surroundSoundEnabled"), + Token::Str("surroundSound"), Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::Some, @@ -161,7 +161,7 @@ fn settings_de() { Token::U32(3000), Token::Str("pauseOnMinimize"), Token::Bool(false), - Token::Str("surroundSoundEnabled"), + Token::Str("surroundSound"), Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, From c4e9cbea29d43df89b5200de476222811e926e7e Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Sat, 25 Nov 2023 08:58:34 -0800 Subject: [PATCH 6/6] fmt --- src/runtime/env.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/runtime/env.rs b/src/runtime/env.rs index df1484c52..8f89b6b9f 100644 --- a/src/runtime/env.rs +++ b/src/runtime/env.rs @@ -519,10 +519,7 @@ fn migrate_storage_schema_to_v11() -> TryEnvFuture<()> { .and_then(|settings| settings.as_object_mut()) { Some(settings) => { - settings.insert( - "surroundSound".to_owned(), - serde_json::Value::Bool(false), - ); + settings.insert("surroundSound".to_owned(), serde_json::Value::Bool(false)); E::set_storage(PROFILE_STORAGE_KEY, Some(&profile)) } _ => E::set_storage::<()>(PROFILE_STORAGE_KEY, None),