refactor(profile): separate subscription refresh and scheduling policy

Separate download and scheduling policy from the subscription use case while retaining the existing write path.
This commit is contained in:
Tunglies
2026-12-01 22:07:38 +08:00
parent 25750ae001
commit 33ce830c01
11 changed files with 407 additions and 306 deletions
+24 -32
View File
@@ -1,4 +1,7 @@
use super::{PrfItem, PrfOption, PrfSelected, patch_opt_field, prfitem::build_profile};
use super::{
PrfItem, PrfOption, PrfSelected, patch_opt_field,
prfitem::{RemoteProfileRefresh, build_profile},
};
use crate::core::notify::{Refresh, announce};
use crate::utils::retry::{RetryError, RetryPolicy, retry, retry_with_state};
use crate::{
@@ -188,37 +191,26 @@ fn validate_profile_file(file: &str) -> Result<()> {
}
/// Updates fields returned by a remote profile refresh.
async fn update_item(profiles: &mut IProfiles, uid: &str, item: &mut PrfItem) -> Result<()> {
if profiles.items.is_none() {
profiles.items = Some(vec![]);
}
async fn apply_remote_refresh(profiles: &mut IProfiles, uid: &str, refresh: RemoteProfileRefresh) -> Result<()> {
let _ = profiles.get_item(uid)?;
if let Some(items) = profiles.items.as_mut() {
for each in items.iter_mut() {
if each.uid.as_deref() == Some(uid) {
each.extra = item.extra;
each.updated = item.updated;
each.home = item.home.to_owned();
each.option = PrfOption::merge(each.option.as_ref(), item.option.as_ref());
if let Some(file_data) = item.file_data.take() {
let file = each.file.take();
let file =
file.unwrap_or_else(|| item.file.take().unwrap_or_else(|| format!("{}.yaml", uid).into()));
each.file = Some(file.clone());
let path = dirs::app_profiles_dir()?.join(file.as_str());
fs::write(&path, file_data.as_bytes())
.await
.with_context(|| format!("failed to write to file \"{file}\""))?;
}
break;
}
}
if let Some(each) = profiles
.items
.iter_mut()
.flatten()
.find(|item| item.uid.as_deref() == Some(uid))
{
each.extra = refresh.extra;
each.updated = Some(refresh.updated);
each.home = refresh.home;
each.option = PrfOption::merge(each.option.as_ref(), Some(&refresh.option));
let file = each
.file
.get_or_insert_with(|| format!("{}.yaml", help::get_uid("R")).into());
let path = dirs::app_profiles_dir()?.join(file.as_str());
fs::write(&path, refresh.body.as_bytes())
.await
.with_context(|| format!("failed to write to file \"{file}\""))?;
}
save_profiles(profiles).await
@@ -320,9 +312,9 @@ pub(crate) async fn profiles_save_file_safe() -> Result<()> {
with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await
}
pub(crate) async fn profiles_update_item_safe(index: &str, item: &mut PrfItem) -> Result<()> {
pub(crate) async fn apply_remote_refresh_safe(index: &str, refresh: RemoteProfileRefresh) -> Result<()> {
with_profiles_edit(|mut profiles| async move {
update_item(&mut profiles, index, item).await?;
apply_remote_refresh(&mut profiles, index, refresh).await?;
Ok(profiles)
})
.await