mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-06 01:22:16 +00:00
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.
This commit is contained in:
@@ -626,10 +626,7 @@ pub(crate) async fn execute_command(
|
|||||||
let format = CHAT_STORE
|
let format = CHAT_STORE
|
||||||
.get(message.chat.id.0)
|
.get(message.chat.id.0)
|
||||||
.await
|
.await
|
||||||
.message_format
|
.format_for(fetched.site_id);
|
||||||
.get(fetched.site_id)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
let caption = preview_caption(
|
let caption = preview_caption(
|
||||||
&format,
|
&format,
|
||||||
&fetched.caption,
|
&fetched.caption,
|
||||||
|
|||||||
@@ -638,11 +638,7 @@ async fn url_media_inner(
|
|||||||
// Cache keys are prefixed with the site id ("twitter:…"), matching
|
// Cache keys are prefixed with the site id ("twitter:…"), matching
|
||||||
// the value a fresh fetch would read from Fetched::site_id.
|
// the value a fresh fetch would read from Fetched::site_id.
|
||||||
let site = x_media::site::site_id_from_key(&key);
|
let site = x_media::site::site_id_from_key(&key);
|
||||||
let format = chat_data
|
let format = chat_data.format_for(site);
|
||||||
.message_format
|
|
||||||
.get(site)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
// One call for both: `caption_from_fields` returns the truncated
|
// One call for both: `caption_from_fields` returns the truncated
|
||||||
// built-in caption itself when the chat has no format for this site.
|
// built-in caption itself when the chat has no format for this site.
|
||||||
let caption = x_media::site::caption_from_fields(
|
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;
|
let chat_data = ctx.chat_store.get(chat_id).await;
|
||||||
// Per-site caption format override (empty -> built-in caption).
|
// Per-site caption format override (empty -> built-in caption).
|
||||||
let format = chat_data
|
let format = chat_data.format_for(fetched.site_id);
|
||||||
.message_format
|
|
||||||
.get(fetched.site_id)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
let caption = fetched.caption_with(&format);
|
let caption = fetched.caption_with(&format);
|
||||||
// Raw render data for the link cache; the send fills in the
|
// Raw render data for the link cache; the send fills in the
|
||||||
// Telegram file ids and persists the entry.
|
// Telegram file ids and persists the entry.
|
||||||
@@ -862,11 +854,7 @@ async fn refetch(
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
let chat_data = ctx.chat_store.get(chat_id).await;
|
let chat_data = ctx.chat_store.get(chat_id).await;
|
||||||
let format = chat_data
|
let format = chat_data.format_for(fetched.site_id);
|
||||||
.message_format
|
|
||||||
.get(fetched.site_id)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
let caption = fetched.caption_with(&format);
|
let caption = fetched.caption_with(&format);
|
||||||
let cache_data = fetched
|
let cache_data = fetched
|
||||||
.render_fields()
|
.render_fields()
|
||||||
|
|||||||
@@ -23,6 +23,14 @@ pub struct ChatData {
|
|||||||
pub message_format: HashMap<String, String>,
|
pub message_format: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)]
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||||
pub struct EditMessage {
|
pub struct EditMessage {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user