mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-29 00:12:12 +00:00
refactor(site): drop site_id_from_key, split the key at its caller
site_id_from_key split the prefix out and then walked the registry to confirm the prefix was a registered site id — a round trip over a value cache_key itself produced from that registry: the unknown and no-colon branches were unreachable for any key the bot makes. The single caller (the link-cache hit path) splits the prefix directly; the registry-echo test and the misskey assertion of it go with it.
This commit is contained in:
@@ -209,7 +209,6 @@ mod tests {
|
||||
cache_key("https://misskey.io/notes/aotihl10lqrs015s"),
|
||||
Some("misskey:aotihl10lqrs015s".to_string())
|
||||
);
|
||||
assert_eq!(crate::site::site_id_from_key("misskey:abc"), "misskey");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -231,19 +231,6 @@ pub fn cache_key(url: &str) -> Option<String> {
|
||||
SITES.iter().find_map(|site| site.cache_key(url))
|
||||
}
|
||||
|
||||
/// The site id carried by a cache key (`"twitter:123"` → `"twitter"`).
|
||||
/// Unknown prefixes fall back to `"unknown"`. The bot uses this on the
|
||||
/// link-cache hit path, where no [`Fetched`] is available — the same value
|
||||
/// a fresh fetch would read from [`Fetched::site_id`].
|
||||
pub fn site_id_from_key(key: &str) -> &'static str {
|
||||
let prefix = key.split(':').next().unwrap_or("");
|
||||
SITES
|
||||
.iter()
|
||||
.map(|site| site.id())
|
||||
.find(|id| *id == prefix)
|
||||
.unwrap_or("unknown")
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FetchError {
|
||||
#[error("http error: {0}")]
|
||||
@@ -591,16 +578,6 @@ mod tests {
|
||||
assert_eq!(cache_key("https://example.com/not-a-post"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn site_id_from_key_parses_prefix() {
|
||||
assert_eq!(site_id_from_key("twitter:123"), "twitter");
|
||||
assert_eq!(site_id_from_key("pixiv:123"), "pixiv");
|
||||
assert_eq!(site_id_from_key("bsky:handle.example/3lorem"), "bsky");
|
||||
assert_eq!(site_id_from_key("bilibili:123"), "bilibili");
|
||||
assert_eq!(site_id_from_key("unknown:1"), "unknown");
|
||||
assert_eq!(site_id_from_key("no-colon"), "unknown");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_lists_all_sites_in_dispatch_order() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -554,8 +554,10 @@ async fn url_media_inner(
|
||||
log::debug!("link cache hit for {key}");
|
||||
let chat_data = ctx.chat_store.get(chat_id).await;
|
||||
// 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);
|
||||
// the value a fresh fetch would read from Fetched::site_id. The key
|
||||
// came out of cache_key, so its prefix is a registered id by
|
||||
// construction — splitting it off is the whole lookup.
|
||||
let site = key.split(':').next().unwrap_or("");
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user