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 a2e5a4e4b..8f89b6b9f 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 {}", @@ -503,6 +509,26 @@ fn migrate_storage_schema_to_v10() -> TryEnvFuture<()> { .boxed_env() } +fn migrate_storage_schema_to_v11() -> 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("surroundSound".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(&11))) + .boxed_env() +} + #[cfg(test)] mod test { use serde_json::{json, Value}; @@ -513,9 +539,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, }, @@ -903,4 +929,44 @@ mod test { assert_storage_shema_version(10); } } + + async fn test_migration_from_10_to_11() { + { + let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv"); + let profile_before = json!({ + "settings": {} + }); + + let migrated_profile = json!({ + "settings": { + "surroundSound": false, + } + }); + + // setup storage for migration + set_profile_and_schema_version(&profile_before, 10); + + // migrate storage + migrate_storage_schema_to_v11::() + .await + .expect("Should migrate"); + + let storage = STORAGE.read().expect("Should lock"); + + assert_eq!( + &11.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/runtime/msg/action.rs b/src/runtime/msg/action.rs index 3087dad53..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::{LibraryItemId, LibraryItem}, + library::LibraryItemId, profile::Settings as ProfileSettings, resource::{MetaItemId, MetaItemPreview, Video}, }, diff --git a/src/types/profile/settings.rs b/src/types/profile/settings.rs index dc4e03003..87ec5f539 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: 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: 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..6b77bb1f5 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("surroundSound"), + 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..cca86c9ee 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: 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("surroundSound"), + 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("surroundSound"), + Token::Bool(false), Token::Str("streamingServerWarningDismissed"), Token::None, Token::StructEnd,