From 2ff213e22f01989ff4f0079e9322e67e23f25c98 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Thu, 24 Sep 2026 04:13:14 +0800 Subject: [PATCH] refactor(inline): delete InlineKind, convert straight to CachedMediaKind MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit InlineKind mirrored CachedMediaKind variant for variant (Photo/Video/Gif) and existed only to feed url_result, with two conversions kept in step: Media → InlineKind at the fetch call and CachedMediaKind → InlineKind at the cache call (plus the enum and the mapper). url_result takes CachedMediaKind now — the type the cache path already carries and the fetch path maps to with the same three-arm match — and the degraded-video skip collapses to a guard before the call. One enum fewer between a media kind and its Telegram result. --- crates/xmedia-bot/src/handlers/inline.rs | 50 ++++++++---------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/crates/xmedia-bot/src/handlers/inline.rs b/crates/xmedia-bot/src/handlers/inline.rs index 52f8586..c5bbbe8 100644 --- a/crates/xmedia-bot/src/handlers/inline.rs +++ b/crates/xmedia-bot/src/handlers/inline.rs @@ -219,7 +219,11 @@ async fn answer_inline_query( .unwrap_or(url); results.push(url_result( i.to_string(), - inline_kind(media), + match media { + Media::Illustration { .. } => CachedMediaKind::Photo, + Media::Video { .. } => CachedMediaKind::Video, + Media::Animated { .. } => CachedMediaKind::Animation, + }, url, thumbnail, fetched.title.clone(), @@ -252,26 +256,10 @@ fn inline_caption(cached: &CachedPost, quote_chars: usize) -> String { .into_owned() } -/// Which inline result kind a payload maps to. -fn inline_kind(media: &Media) -> InlineKind { - match media { - Media::Illustration { .. } => InlineKind::Photo, - Media::Video { .. } => InlineKind::Video, - Media::Animated { .. } => InlineKind::Gif, - } -} - -#[derive(Clone, Copy)] -enum InlineKind { - Photo, - Video, - Gif, -} - /// One inline result pointing Telegram at a URL it fetches itself. fn url_result( id: String, - kind: InlineKind, + kind: CachedMediaKind, url: url::Url, thumbnail: url::Url, title: String, @@ -279,12 +267,12 @@ fn url_result( ) -> InlineQueryResult { let parse_mode = ParseMode::Html; match kind { - InlineKind::Photo => InlineQueryResult::Photo( + CachedMediaKind::Photo => InlineQueryResult::Photo( InlineQueryResultPhoto::new(id, url, thumbnail) .caption(caption) .parse_mode(parse_mode), ), - InlineKind::Video => InlineQueryResult::Video( + CachedMediaKind::Video => InlineQueryResult::Video( InlineQueryResultVideo::new( id, url, @@ -295,7 +283,7 @@ fn url_result( .caption(caption) .parse_mode(parse_mode), ), - InlineKind::Gif => InlineQueryResult::Mpeg4Gif( + CachedMediaKind::Animation => InlineQueryResult::Mpeg4Gif( InlineQueryResultMpeg4Gif::new(id, url, thumbnail) .caption(caption) .parse_mode(parse_mode), @@ -360,20 +348,16 @@ fn cached_inline_results(cached: &CachedPost, caption: &str) -> Vec InlineKind::Photo, - CachedMediaKind::Video => { - log::debug!("inline: skipping a cached video with no thumbnail {i}"); - return None; - } - CachedMediaKind::Animation => InlineKind::Gif, - }; + // A degraded entry has no poster, and Telegram would try to render + // a video URL as its own thumbnail — skip it. A photo or gif is an + // image, so its own URL serves as the thumbnail. + if matches!(media.kind, CachedMediaKind::Video) { + log::debug!("inline: skipping a cached video with no thumbnail {i}"); + return None; + } Some(url_result( id, - kind, + media.kind, url.clone(), url, cached.title.clone(),