-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Early 0.18.3 support. Bump versions. Switch to using Github for lemmy_api_common. * Add rough migration code. Update Lemmy BE version. * Cleanup migration code * Update README
- Loading branch information
Showing
8 changed files
with
181 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::profile::ProfileConfiguration; | ||
use crate::profile::ProfileSettings; | ||
use std::path::Path; | ||
|
||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] | ||
pub struct ProfileSettingsV1 { | ||
show_nsfw: bool, | ||
show_scores: bool, | ||
theme: String, | ||
default_sort_type: String, | ||
default_listing_type: String, | ||
interface_language: String, | ||
show_avatars: bool, | ||
send_notifications_to_email: bool, | ||
bot_account: bool, | ||
show_bot_accounts: bool, | ||
show_read_posts: bool, | ||
show_new_post_notifs: bool, | ||
discussion_languages: Vec<i32>, | ||
open_links_in_new_tab: bool, | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] | ||
pub struct ProfileConfigurationV1 { | ||
pub blocked_users: Vec<String>, | ||
pub blocked_communities: Vec<String>, | ||
pub followed_communities: Vec<String>, | ||
pub profile_settings: ProfileSettingsV1, | ||
} | ||
|
||
const OLD_PROFILE_FILENAME: &str = "profile_v1.json"; | ||
|
||
pub fn read_profile() -> Result<ProfileConfigurationV1, String> { | ||
let path = Path::new(OLD_PROFILE_FILENAME); | ||
let profile_json_result = std::fs::read_to_string(path); | ||
let profile_json = match profile_json_result { | ||
Ok(file) => file, | ||
Err(_) => return Err(format!("ERROR: Failed to open {}", OLD_PROFILE_FILENAME)), | ||
}; | ||
|
||
let profile_local_result: Result<ProfileConfigurationV1, serde_json::Error> = serde_json::from_slice(profile_json.as_bytes()); | ||
let profile_local = match profile_local_result { | ||
Ok(profile) => profile, | ||
Err(e) => return Err(format!("ERROR: Failed to parse {} JSON - {}", OLD_PROFILE_FILENAME, e)), | ||
}; | ||
|
||
return Ok(profile_local); | ||
} | ||
|
||
pub fn convert_profile(old_profile: ProfileConfigurationV1) -> ProfileConfiguration { | ||
let new_profile = ProfileConfiguration { | ||
blocked_users: old_profile.blocked_users, | ||
blocked_communities: old_profile.blocked_communities, | ||
followed_communities: old_profile.followed_communities, | ||
profile_settings: ProfileSettings { | ||
show_nsfw: old_profile.profile_settings.show_nsfw, | ||
show_scores: old_profile.profile_settings.show_scores, | ||
theme: old_profile.profile_settings.theme, | ||
default_sort_type: old_profile.profile_settings.default_sort_type, | ||
default_listing_type: old_profile.profile_settings.default_listing_type, | ||
interface_language: old_profile.profile_settings.interface_language, | ||
show_avatars: old_profile.profile_settings.show_avatars, | ||
send_notifications_to_email: old_profile.profile_settings.send_notifications_to_email, | ||
bot_account: old_profile.profile_settings.bot_account, | ||
show_bot_accounts: old_profile.profile_settings.show_bot_accounts, | ||
show_read_posts: old_profile.profile_settings.show_read_posts, | ||
show_new_post_notifs: old_profile.profile_settings.show_new_post_notifs, | ||
discussion_languages: old_profile.profile_settings.discussion_languages, | ||
open_links_in_new_tab: old_profile.profile_settings.open_links_in_new_tab, | ||
infinite_scroll_enabled: false, | ||
}, | ||
}; | ||
|
||
return new_profile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod migrate_v1_to_v2; | ||
pub mod profile_migrate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::profile; | ||
use crate::migrations; | ||
use std::path::Path; | ||
|
||
const PROFILE_FILENAME_START: &str = "profile_v"; | ||
const PROFILE_FILENAME_END: &str = ".json"; | ||
const PROFILE_CURRENT_VERSION: u16 = 2; | ||
|
||
pub fn read_latest_profile() -> Result<profile::ProfileConfiguration, String> { | ||
// Identify latest profile version | ||
let mut latest_profile_version: u16 = 0; | ||
let directory_items = std::fs::read_dir("./").unwrap(); | ||
for item in directory_items { | ||
if item.is_ok() { | ||
let dir_entry = item.unwrap(); | ||
let metadata = std::fs::metadata(dir_entry.path()); | ||
if metadata.is_ok_and(|m| m.is_file()) { | ||
let filename = String::from(dir_entry.file_name().to_str().unwrap()); | ||
if filename.starts_with(PROFILE_FILENAME_START) && filename.ends_with(PROFILE_FILENAME_END) { | ||
let end_index = filename.find(PROFILE_FILENAME_END).unwrap(); | ||
let profile_version_string = filename.get(PROFILE_FILENAME_START.char_indices().count()..end_index); | ||
let profile_version = profile_version_string.unwrap().parse::<u16>().unwrap(); | ||
|
||
if profile_version > latest_profile_version { | ||
latest_profile_version = profile_version; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Convert as necessary | ||
let mut profile_v1: Option<migrations::migrate_v1_to_v2::ProfileConfigurationV1> = None; | ||
let mut profile_v2: Option<profile::ProfileConfiguration> = None; | ||
|
||
if latest_profile_version == 1 { | ||
let read_result = migrations::migrate_v1_to_v2::read_profile(); | ||
profile_v1 = match read_result { | ||
Ok(profile) => Some(profile), | ||
Err(e) => return Err(e), | ||
}; | ||
latest_profile_version = 2; | ||
} | ||
|
||
if latest_profile_version == PROFILE_CURRENT_VERSION { | ||
if profile_v1.is_some() { | ||
profile_v2 = Some(migrations::migrate_v1_to_v2::convert_profile(profile_v1.unwrap())); | ||
} else { | ||
let filename = format!("profile_v{}.json", PROFILE_CURRENT_VERSION); | ||
let path = Path::new(filename.as_str()); | ||
let profile_json_result = std::fs::read_to_string(path); | ||
let profile_json = match profile_json_result { | ||
Ok(file) => file, | ||
Err(_) => return Err(format!("ERROR: Failed to open {}", filename)), | ||
}; | ||
|
||
let profile_local_result: Result<profile::ProfileConfiguration, serde_json::Error> = serde_json::from_slice(profile_json.as_bytes()); | ||
let profile_local = match profile_local_result { | ||
Ok(profile) => profile, | ||
Err(e) => return Err(format!("ERROR: Failed to parse {} JSON - {}", filename, e)), | ||
}; | ||
|
||
profile_v2 = Some(profile_local); | ||
} | ||
} | ||
|
||
if profile_v2.is_some() { | ||
return Ok(profile_v2.unwrap()); | ||
} else { | ||
return Err("ERROR: No saved profiles found. Use download option first!".to_string()); | ||
} | ||
} | ||
|
||
pub fn get_latest_profile_name() -> String { | ||
return format!("{}{}{}", PROFILE_FILENAME_START, PROFILE_CURRENT_VERSION, PROFILE_FILENAME_END); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters