From dd98a90a4555d97d966f4ded752ad483d2f2bc66 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Mon, 21 Sep 2026 17:27:42 +0800 Subject: [PATCH] refactor(state): add ChatData::format_for for the per-site caption format Four call sites (cache hit, fresh fetch, startup refetch, /debug preview) spelled out the same message_format.get(...).cloned().unwrap_or_default(); the accessor names the lookup and keeps the empty-format contract in one place. --- crates/xmedia-bot/src/handlers/commands.rs | 5 +---- crates/xmedia-bot/src/handlers/urls.rs | 18 +++--------------- crates/xmedia-bot/src/state.rs | 8 ++++++++ 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/crates/xmedia-bot/src/handlers/commands.rs b/crates/xmedia-bot/src/handlers/commands.rs index 3e43cc6..1b4ddd5 100644 --- a/crates/xmedia-bot/src/handlers/commands.rs +++ b/crates/xmedia-bot/src/handlers/commands.rs @@ -626,10 +626,7 @@ pub(crate) async fn execute_command( let format = CHAT_STORE .get(message.chat.id.0) .await - .message_format - .get(fetched.site_id) - .cloned() - .unwrap_or_default(); + .format_for(fetched.site_id); let caption = preview_caption( &format, &fetched.caption, diff --git a/crates/xmedia-bot/src/handlers/urls.rs b/crates/xmedia-bot/src/handlers/urls.rs index 97236a3..1179710 100644 --- a/crates/xmedia-bot/src/handlers/urls.rs +++ b/crates/xmedia-bot/src/handlers/urls.rs @@ -638,11 +638,7 @@ async fn url_media_inner( // Cache keys are prefixed with the site id ("twitter:…"), matching // the value a fresh fetch would read from Fetched::site_id. let site = x_media::site::site_id_from_key(&key); - let format = chat_data - .message_format - .get(site) - .cloned() - .unwrap_or_default(); + let format = chat_data.format_for(site); // One call for both: `caption_from_fields` returns the truncated // built-in caption itself when the chat has no format for this site. let caption = x_media::site::caption_from_fields( @@ -713,11 +709,7 @@ async fn url_media_inner( } let chat_data = ctx.chat_store.get(chat_id).await; // Per-site caption format override (empty -> built-in caption). - let format = chat_data - .message_format - .get(fetched.site_id) - .cloned() - .unwrap_or_default(); + let format = chat_data.format_for(fetched.site_id); let caption = fetched.caption_with(&format); // Raw render data for the link cache; the send fills in the // Telegram file ids and persists the entry. @@ -862,11 +854,7 @@ async fn refetch( return Ok(None); } let chat_data = ctx.chat_store.get(chat_id).await; - let format = chat_data - .message_format - .get(fetched.site_id) - .cloned() - .unwrap_or_default(); + let format = chat_data.format_for(fetched.site_id); let caption = fetched.caption_with(&format); let cache_data = fetched .render_fields() diff --git a/crates/xmedia-bot/src/state.rs b/crates/xmedia-bot/src/state.rs index d9bc1d8..fcdd09e 100644 --- a/crates/xmedia-bot/src/state.rs +++ b/crates/xmedia-bot/src/state.rs @@ -23,6 +23,14 @@ pub struct ChatData { pub message_format: HashMap, } +impl ChatData { + /// The chat's caption format for `site`, empty when it has none — the + /// built-in caption then applies (`caption_from_fields`). + pub fn format_for(&self, site: &str) -> String { + self.message_format.get(site).cloned().unwrap_or_default() + } +} + #[derive(Serialize, Deserialize, Clone, Debug, Default)] pub struct EditMessage { pub url: String,