Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const NEW_USER_DAYS: chrono::Duration = chrono::Duration::days(30);
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 = 20;
pub const SCHEMA_VERSION: u32 = 21;
pub const IMDB_LINK_CATEGORY: &str = "imdb";
pub const GENRES_LINK_CATEGORY: &str = "Genres";
pub const CINEMETA_TOP_CATALOG_ID: &str = "top";
Expand Down
76 changes: 73 additions & 3 deletions src/runtime/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ pub trait Env {
.await?;
schema_version = 20;
}
if schema_version == 20 {
migrate_storage_schema_to_v21::<Self>()
.map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error)))
.await?;
schema_version = 21;
}
if schema_version != SCHEMA_VERSION {
panic!(
"Storage schema version must be upgraded from {} to {}",
Expand Down Expand Up @@ -740,6 +746,29 @@ fn migrate_storage_schema_to_v20<E: Env>() -> TryEnvFuture<()> {
.boxed_env()
}

fn migrate_storage_schema_to_v21<E: Env>() -> TryEnvFuture<()> {
E::get_storage::<serde_json::Value>(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(
"assSubtitlesStyling".to_owned(),
serde_json::Value::Bool(true),
);
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(&21)))
.boxed_env()
}

#[cfg(test)]
mod test {
use serde_json::{json, Value};
Expand All @@ -755,9 +784,9 @@ mod test {
migrate_storage_schema_to_v14, migrate_storage_schema_to_v15,
migrate_storage_schema_to_v16, migrate_storage_schema_to_v17,
migrate_storage_schema_to_v18, migrate_storage_schema_to_v19,
migrate_storage_schema_to_v20, migrate_storage_schema_to_v6,
migrate_storage_schema_to_v7, migrate_storage_schema_to_v8,
migrate_storage_schema_to_v9,
migrate_storage_schema_to_v20, migrate_storage_schema_to_v21,
migrate_storage_schema_to_v6, migrate_storage_schema_to_v7,
migrate_storage_schema_to_v8, migrate_storage_schema_to_v9,
},
Env,
},
Expand Down Expand Up @@ -1490,4 +1519,45 @@ mod test {
);
}
}

#[tokio::test]
async fn test_migration_from_20_to_21() {
{
let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv");
let profile_before = json!({
"settings": {}
});

let migrated_profile = json!({
"settings": {
"assSubtitlesStyling": true,
}
});

// setup storage for migration
set_profile_and_schema_version(&profile_before, 20);

// migrate storage
migrate_storage_schema_to_v21::<TestEnv>()
.await
.expect("Should migrate");

let storage = STORAGE.read().expect("Should lock");

assert_eq!(
&21.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"
);
}
}
}
2 changes: 2 additions & 0 deletions src/types/profile/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Settings {
pub subtitles_background_color: String,
pub subtitles_outline_color: String,
pub subtitles_opacity: u8,
pub ass_subtitles_styling: bool,
/// Whether or not the Escape key should exists from the app when in Full screen.
pub esc_exit_fullscreen: bool,
/// The Seek time duration (in milliseconds) is when using the Arrow keys
Expand Down Expand Up @@ -81,6 +82,7 @@ impl Default for Settings {
subtitles_background_color: "#00000000".to_owned(),
subtitles_outline_color: "#000000".to_owned(),
subtitles_opacity: 100,
ass_subtitles_styling: true,
esc_exit_fullscreen: true,
seek_time_duration: 10000,
seek_short_time_duration: 3000,
Expand Down
4 changes: 3 additions & 1 deletion src/unit_tests/serde/default_tokens_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl DefaultTokens for Settings {
vec![
Token::Struct {
name: "Settings",
len: 33,
len: 34,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -431,6 +431,8 @@ impl DefaultTokens for Settings {
Token::Str("#000000"),
Token::Str("subtitlesOpacity"),
Token::U8(100),
Token::Str("assSubtitlesStyling"),
Token::Bool(true),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand Down
9 changes: 7 additions & 2 deletions src/unit_tests/serde/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn settings() {
subtitles_background_color: "subtitles_background_color".to_owned(),
subtitles_outline_color: "subtitles_outline_color".to_owned(),
subtitles_opacity: 1,
ass_subtitles_styling: true,
esc_exit_fullscreen: true,
seek_time_duration: 10,
seek_short_time_duration: 3,
Expand All @@ -46,7 +47,7 @@ fn settings() {
&[
Token::Struct {
name: "Settings",
len: 33,
len: 34,
},
Token::Str("interfaceLanguage"),
Token::Str("interface_language"),
Expand Down Expand Up @@ -105,6 +106,8 @@ fn settings() {
Token::Str("subtitles_outline_color"),
Token::Str("subtitlesOpacity"),
Token::U8(1),
Token::Str("assSubtitlesStyling"),
Token::Bool(true),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand Down Expand Up @@ -136,7 +139,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 33,
len: 34,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -187,6 +190,8 @@ fn settings_de() {
Token::Str("#000000"),
Token::Str("subtitlesOpacity"),
Token::U8(100),
Token::Str("assSubtitlesStyling"),
Token::Bool(true),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand Down