refactor(profile): look up the refresh target once

This commit is contained in:
Tunglies
2026-12-03 19:38:18 +08:00
parent c53e005f25
commit c742a9a17c
3 changed files with 30 additions and 46 deletions
+15 -30
View File
@@ -193,32 +193,6 @@ fn validate_profile_file(file: &str) -> Result<()> {
Ok(())
}
/// Updates fields returned by a remote profile refresh.
async fn apply_remote_refresh(profiles: &mut IProfiles, uid: &str, refresh: RemoteProfileRefresh) -> Result<()> {
let _ = profiles.get_item(uid)?;
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
}
/// Raise intervals below `min_minutes`. `None`/`0` mean "never auto-update", left alone.
/// Returns how many changed so the caller can skip a pointless write.
pub(crate) fn raise_short_update_intervals(profiles: &mut IProfiles, min_minutes: u64) -> usize {
@@ -315,14 +289,25 @@ 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 apply_remote_refresh_safe(index: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> {
pub(crate) async fn save_remote_refresh(uid: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> {
with_profiles_edit(|mut profiles| async move {
let current = profiles.get_item(index)?;
let profile = profiles.get_item_mut(uid)?;
anyhow::ensure!(
current.itype.as_deref() == Some("remote") && current.url.as_deref() == Some(url),
profile.itype.as_deref() == Some("remote") && profile.url.as_deref() == Some(url),
"profile source changed during refresh"
);
apply_remote_refresh(&mut profiles, index, refresh).await?;
profile.extra = refresh.extra;
profile.updated = Some(refresh.updated);
profile.home = refresh.home;
profile.option = PrfOption::merge(profile.option.as_ref(), Some(&refresh.option));
let file = profile
.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?;
Ok(profiles)
})
.await
+2 -2
View File
@@ -1,5 +1,5 @@
use crate::{
config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::apply_remote_refresh_safe},
config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::save_remote_refresh},
core::{
CoreManager, handle,
notify::{NoticeStatus, Refresh, announce},
@@ -133,7 +133,7 @@ async fn perform_profile_update(
"profile source changed during refresh"
);
let refresh = prepare_remote_refresh(downloaded, merged_opt.as_ref()).await?;
apply_remote_refresh_safe(uid, url, refresh).await?;
save_remote_refresh(uid, url, refresh).await?;
if !matches!(strategy, Strategy::Configured) {
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
}