refactor(subscription): carry refresh metadata through the download

This commit is contained in:
Tunglies
2026-12-04 01:48:32 +08:00
parent 59762520cc
commit 28aefde36f
2 changed files with 23 additions and 38 deletions
+1 -2
View File
@@ -308,13 +308,12 @@ fn check_refresh_source(current: &PrfItem, url: &str, file: Option<&String>) ->
pub(crate) async fn save_remote_refresh(
uid: &str,
url: &str,
url: String,
file: Option<String>,
downloaded: DownloadedSubscription,
options: Option<PrfOption>,
) -> Result<()> {
let uid = uid.to_owned();
let url = url.to_owned();
// Dropping the caller must not separate an issued disk write from its Draft publication.
tokio::spawn(async move {
let _profile_write = PROFILE_WRITE_LOCK.lock().await;
+22 -36
View File
@@ -17,50 +17,42 @@ use clash_verge_logging::{Type, logging};
use tristr::alias::String;
struct RefreshSource {
name: Option<String>,
url: String,
file: Option<String>,
option: Option<PrfOption>,
}
async fn should_update_profile(uid: &str, ignore_auto_update: bool) -> Result<Option<RefreshSource>> {
async fn refresh_source(uid: &str, ignore_auto_update: bool) -> Result<Option<RefreshSource>> {
let profiles = Config::profiles().await;
let _guard = profile_storage::read().await?;
let profiles = profiles.latest_arc();
let item = profiles.get_item(uid)?;
let is_remote = item.itype.as_ref().is_some_and(|s| s == "remote");
if !is_remote {
if item.itype.as_deref() != Some("remote") {
logging!(info, Type::Config, "[订阅更新] 不是远程订阅,跳过更新");
Ok(None)
} else if item.url.is_none() {
return Ok(None);
}
let Some(url) = &item.url else {
logging!(warn, Type::Config, "[订阅更新] 缺少URL,无法更新");
bail!("failed to get the profile item url");
} else if !ignore_auto_update && !item.option.as_ref().and_then(|o| o.allow_auto_update).unwrap_or(true) {
};
if !ignore_auto_update && !item.option.as_ref().and_then(|o| o.allow_auto_update).unwrap_or(true) {
logging!(info, Type::Config, "[订阅更新] 禁止自动更新,跳过更新");
Ok(None)
} else {
logging!(
info,
Type::Config,
"[订阅更新] 远程订阅,URL: {}",
mask_url(
item.url
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Profile URL is None"))?
)
);
Ok(Some(RefreshSource {
url: item.url.clone().ok_or_else(|| anyhow::anyhow!("Profile URL is None"))?,
file: item.file.clone(),
option: item.option.clone(),
}))
return Ok(None);
}
logging!(info, Type::Config, "[订阅更新] 远程订阅,URL: {}", mask_url(url));
Ok(Some(RefreshSource {
name: item.name.clone(),
url: url.clone(),
file: item.file.clone(),
option: item.option.clone(),
}))
}
#[tracing::instrument(skip_all, level = "info", fields(uid = %uid, strategy = tracing::field::Empty, configured_elapsed = tracing::field::Empty, clash_elapsed = tracing::field::Empty, system_elapsed = tracing::field::Empty))]
async fn perform_profile_update(
uid: &str,
source: &RefreshSource,
source: RefreshSource,
overrides: Option<&PrfOption>,
is_manual_trigger: bool,
) -> Result<()> {
@@ -68,13 +60,7 @@ async fn perform_profile_update(
let options = PrfOption::merge(source.option.as_ref(), overrides);
let download_options = options.as_ref();
let configured_proxy = subscription_proxy(download_options);
let profiles = Config::profiles().await;
let profiles_arc = profiles.latest_arc();
let profile_name = profiles_arc
.get_item(uid)
.ok()
.and_then(|item| item.name.clone())
.unwrap_or_else(|| String::from("UnKnown Profile"));
let profile_name = source.name.as_deref().unwrap_or("UnKnown Profile");
#[derive(Clone, Copy)]
enum Strategy {
@@ -130,9 +116,9 @@ async fn perform_profile_update(
let last_err = match result {
Ok((strategy, downloaded)) => {
// A persistence error must not replay a successful download with another transport.
save_remote_refresh(uid, url, source.file.clone(), downloaded, options).await?;
save_remote_refresh(uid, source.url, source.file, downloaded, options).await?;
if !matches!(strategy, Strategy::Configured) {
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name);
}
return Ok(());
}
@@ -151,11 +137,11 @@ async fn perform_profile_update(
#[tracing::instrument(skip_all, level = "info", fields(uid = %uid, manual = is_manual_trigger))]
pub async fn update_profile(uid: &str, option: Option<&PrfOption>, is_manual_trigger: bool) -> Result<()> {
let subscription = should_update_profile(uid, is_manual_trigger).await?;
let subscription = refresh_source(uid, is_manual_trigger).await?;
let profile_persisted = match subscription {
Some(source) => {
perform_profile_update(uid, &source, option, is_manual_trigger).await?;
perform_profile_update(uid, source, option, is_manual_trigger).await?;
true
}
None => false,