refactor(inline): delete InlineKind, convert straight to CachedMediaKind

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.
This commit is contained in:
2026-09-24 04:37:53 +08:00
parent 2c45b3491d
commit 2ff213e22f
+17 -33
View File
@@ -219,7 +219,11 @@ async fn answer_inline_query(
.unwrap_or(url); .unwrap_or(url);
results.push(url_result( results.push(url_result(
i.to_string(), i.to_string(),
inline_kind(media), match media {
Media::Illustration { .. } => CachedMediaKind::Photo,
Media::Video { .. } => CachedMediaKind::Video,
Media::Animated { .. } => CachedMediaKind::Animation,
},
url, url,
thumbnail, thumbnail,
fetched.title.clone(), fetched.title.clone(),
@@ -252,26 +256,10 @@ fn inline_caption(cached: &CachedPost, quote_chars: usize) -> String {
.into_owned() .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. /// One inline result pointing Telegram at a URL it fetches itself.
fn url_result( fn url_result(
id: String, id: String,
kind: InlineKind, kind: CachedMediaKind,
url: url::Url, url: url::Url,
thumbnail: url::Url, thumbnail: url::Url,
title: String, title: String,
@@ -279,12 +267,12 @@ fn url_result(
) -> InlineQueryResult { ) -> InlineQueryResult {
let parse_mode = ParseMode::Html; let parse_mode = ParseMode::Html;
match kind { match kind {
InlineKind::Photo => InlineQueryResult::Photo( CachedMediaKind::Photo => InlineQueryResult::Photo(
InlineQueryResultPhoto::new(id, url, thumbnail) InlineQueryResultPhoto::new(id, url, thumbnail)
.caption(caption) .caption(caption)
.parse_mode(parse_mode), .parse_mode(parse_mode),
), ),
InlineKind::Video => InlineQueryResult::Video( CachedMediaKind::Video => InlineQueryResult::Video(
InlineQueryResultVideo::new( InlineQueryResultVideo::new(
id, id,
url, url,
@@ -295,7 +283,7 @@ fn url_result(
.caption(caption) .caption(caption)
.parse_mode(parse_mode), .parse_mode(parse_mode),
), ),
InlineKind::Gif => InlineQueryResult::Mpeg4Gif( CachedMediaKind::Animation => InlineQueryResult::Mpeg4Gif(
InlineQueryResultMpeg4Gif::new(id, url, thumbnail) InlineQueryResultMpeg4Gif::new(id, url, thumbnail)
.caption(caption) .caption(caption)
.parse_mode(parse_mode), .parse_mode(parse_mode),
@@ -360,20 +348,16 @@ fn cached_inline_results(cached: &CachedPost, caption: &str) -> Vec<InlineQueryR
return None; return None;
} }
let url = url::Url::parse(&media.url).ok()?; let url = url::Url::parse(&media.url).ok()?;
// A photo or gif is an image, so its own URL serves as the // A degraded entry has no poster, and Telegram would try to render
// thumbnail; a video needs a real poster, and a degraded entry has // a video URL as its own thumbnail — skip it. A photo or gif is an
// none — Telegram would try to render the video as an image. // image, so its own URL serves as the thumbnail.
let kind = match media.kind { if matches!(media.kind, CachedMediaKind::Video) {
CachedMediaKind::Photo => InlineKind::Photo, log::debug!("inline: skipping a cached video with no thumbnail {i}");
CachedMediaKind::Video => { return None;
log::debug!("inline: skipping a cached video with no thumbnail {i}"); }
return None;
}
CachedMediaKind::Animation => InlineKind::Gif,
};
Some(url_result( Some(url_result(
id, id,
kind, media.kind,
url.clone(), url.clone(),
url, url,
cached.title.clone(), cached.title.clone(),