diff --git a/crates/openplotva-app/src/lib.rs b/crates/openplotva-app/src/lib.rs index a1ff487..12e9fbf 100644 --- a/crates/openplotva-app/src/lib.rs +++ b/crates/openplotva-app/src/lib.rs @@ -4447,10 +4447,15 @@ async fn start_runtime_workers( telegram.clone(), ) .with_reaction_ux(Arc::clone(&generation_reactions)) - .with_frames(video_jobs::TelegramVideoFrames::new( - telegram.clone(), - Arc::clone(&dialog_tool_media), - )), + .with_frames( + video_jobs::TelegramVideoFrames::new( + telegram.clone(), + Arc::clone(&dialog_tool_media), + ) + .with_files(PostgresTelegramFileStore::new( + service_clients.postgres.clone(), + )), + ), }) as Arc }); let vip_image_queue = Arc::clone(&task_queue_for_updates); diff --git a/crates/openplotva-app/src/video_jobs.rs b/crates/openplotva-app/src/video_jobs.rs index 01278f1..db2facb 100644 --- a/crates/openplotva-app/src/video_jobs.rs +++ b/crates/openplotva-app/src/video_jobs.rs @@ -239,6 +239,7 @@ const VIDEO_FRAME_MAX_SIDE: u32 = 1280; pub struct TelegramVideoFrames { data_urls: crate::vision::TelegramClientVisionDataUrlProvider, describer: Arc, + files: Option, } impl TelegramVideoFrames { @@ -251,9 +252,35 @@ impl TelegramVideoFrames { data_urls: crate::vision::TelegramClientVisionDataUrlProvider::new(telegram) .with_max_side(VIDEO_FRAME_MAX_SIDE), describer, + files: None, } } + /// Find the file id of a quoted picture, which the dialog knows only by its unique id. + #[must_use] + pub fn with_files(mut self, files: openplotva_storage::PostgresTelegramFileStore) -> Self { + self.files = Some(files); + self + } + + async fn file_id(&self, frame: &VideoFrameRef) -> Result { + let file_id = frame.file_id.trim(); + if !file_id.is_empty() { + return Ok(file_id.to_owned()); + } + let Some(files) = &self.files else { + return Err("the picture has no file id".to_owned()); + }; + files + .list_files_by_unique_ids(&[frame.file_unique_id.trim().to_owned()]) + .await + .map_err(|error| format!("look up the picture: {error}"))? + .into_iter() + .map(|record| record.latest_file_id.trim().to_owned()) + .find(|file_id| !file_id.is_empty()) + .ok_or_else(|| "the picture has no known file id".to_owned()) + } + async fn load( &self, params: &VideoGenJobParams, @@ -261,15 +288,16 @@ impl TelegramVideoFrames { ) -> Result { use crate::vision::TelegramVisionDataUrlProvider as _; + let file_id = self.file_id(frame).await?; let data_url = self .data_urls - .telegram_file_data_url(&frame.file_id, "photo", None) + .telegram_file_data_url(&file_id, "photo", None) .await .map_err(|error| format!("download the picture: {error}"))?; let picture = openplotva_core::ChatAttachment { kind: "image".to_owned(), source: "message".to_owned(), - file_id: frame.file_id.clone(), + file_id, file_unique_id: frame.file_unique_id.clone(), ..openplotva_core::ChatAttachment::default() };