| 1 | use std::collections::HashMap; |
| 2 | use std::io::Write as _; |
| 3 | use std::path::PathBuf; |
| 4 | use std::rc::Rc; |
| 5 | |
| 6 | use chatsvc::{ |
| 7 | Attendance, Occurrence, Recap, RecordingDetails, TranscriptFormat, TranscriptTurn, |
| 8 | days_until_expiry, |
| 9 | }; |
| 10 | use chrono::{DateTime, Utc}; |
| 11 | use gpui_kit::component::WindowExt as _; |
| 12 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 13 | use gpui_kit::component::dialog::DialogFooter; |
| 14 | use gpui_kit::component::input::{InputEvent, InputState}; |
| 15 | use gpui_kit::*; |
| 16 | |
| 17 | use super::ConversationView; |
| 18 | use crate::app_state::{AppState, Selection}; |
| 19 | use crate::channel_files::SharedContent; |
| 20 | use crate::demo_recap; |
| 21 | use crate::downloads::{self, DownloadState}; |
| 22 | use crate::i18n::tr; |
| 23 | use crate::notice::short_error; |
| 24 | use crate::recap::{DELETE_QUESTION, Load, save_name}; |
| 25 | use crate::rows::{MeetingInfo, MeetingSlot}; |
| 26 | use crate::runtime; |
| 27 | use crate::views::channel_tabs::{ChannelPane, render_chat_bar}; |
| 28 | use crate::views::recap_tab::{RecapCommand, RecapDownload, RecapRun, RecapView, render_recap}; |
| 29 | use crate::views::shared_tab::{SharedCommand, SharedRun}; |
| 30 | |
| 31 | const SEARCH_PLACEHOLDER: &str = "Search transcript"; |
| 32 | const RECORDING_EXTENSION: &str = "mp4"; |
| 33 | const EXPIRY_OCCURRENCES: usize = 3; |
| 34 | const EXPIRY_CACHE_PREFIX: &str = "recording_expiry/"; |
| 35 | |
| 36 | pub(super) fn search_input( |
| 37 | window: &mut Window, |
| 38 | cx: &mut Context<ConversationView>, |
| 39 | ) -> Entity<InputState> { |
| 40 | cx.new(|cx| InputState::new(window, cx).placeholder(SEARCH_PLACEHOLDER)) |
| 41 | } |
| 42 | |
| 43 | pub(super) struct RecapState { |
| 44 | listing: Load<Vec<Occurrence>>, |
| 45 | selected: usize, |
| 46 | details: Load<RecordingDetails>, |
| 47 | transcript: Load<Vec<TranscriptTurn>>, |
| 48 | attendance: Load<Attendance>, |
| 49 | query: String, |
| 50 | downloads: HashMap<RecapDownload, DownloadState>, |
| 51 | generation: u64, |
| 52 | expiry_days: HashMap<String, i64>, |
| 53 | } |
| 54 | |
| 55 | impl Default for RecapState { |
| 56 | fn default() -> Self { |
| 57 | RecapState { |
| 58 | listing: Load::Loading, |
| 59 | selected: 0, |
| 60 | details: Load::Loading, |
| 61 | transcript: Load::Loading, |
| 62 | attendance: Load::Loading, |
| 63 | query: String::new(), |
| 64 | downloads: HashMap::new(), |
| 65 | generation: 0, |
| 66 | expiry_days: HashMap::new(), |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | enum SaveEvent { |
| 72 | Progress(u8), |
| 73 | Finished(Result<PathBuf, String>), |
| 74 | } |
| 75 | |
| 76 | impl ConversationView { |
| 77 | fn is_meeting_chat(&self, cx: &App) -> bool { |
| 78 | let Some(Selection::Chat(chat_id)) = |
| 79 | self.current.as_ref().map(|current| ¤t.selection) |
| 80 | else { |
| 81 | return false; |
| 82 | }; |
| 83 | self.app |
| 84 | .read(cx) |
| 85 | .sidebar |
| 86 | .chats |
| 87 | .iter() |
| 88 | .any(|chat| &chat.id == chat_id && chat.kind.eq_ignore_ascii_case("meeting")) |
| 89 | } |
| 90 | |
| 91 | fn occurrences(&self) -> &[Occurrence] { |
| 92 | self.recap.listing.ready().map_or(&[], Vec::as_slice) |
| 93 | } |
| 94 | |
| 95 | fn selected_occurrence(&self) -> Option<Occurrence> { |
| 96 | self.occurrences().get(self.recap.selected).cloned() |
| 97 | } |
| 98 | |
| 99 | pub(super) fn recap_active(&self) -> bool { |
| 100 | self.pane == ChannelPane::Recap && !self.occurrences().is_empty() |
| 101 | } |
| 102 | |
| 103 | pub(super) fn on_recap_search_event( |
| 104 | &mut self, |
| 105 | input: &Entity<InputState>, |
| 106 | event: &InputEvent, |
| 107 | _: &mut Window, |
| 108 | cx: &mut Context<Self>, |
| 109 | ) { |
| 110 | if matches!(event, InputEvent::Change) { |
| 111 | self.recap.query = input.read(cx).value().to_string(); |
| 112 | cx.notify(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub(super) fn reset_recap(&mut self, cx: &mut Context<Self>) { |
| 117 | self.recap = RecapState { |
| 118 | generation: self.recap.generation + 1, |
| 119 | ..RecapState::default() |
| 120 | }; |
| 121 | if !self.is_meeting_chat(cx) { |
| 122 | return; |
| 123 | } |
| 124 | let Some(conversation_id) = self.conversation_id() else { |
| 125 | return; |
| 126 | }; |
| 127 | let state = self.app.read(cx); |
| 128 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 129 | if demo { |
| 130 | let now = Utc::now(); |
| 131 | let occurrences = demo_recap::occurrences(&conversation_id, now); |
| 132 | self.recap.expiry_days = occurrences |
| 133 | .iter() |
| 134 | .filter_map(|occurrence| { |
| 135 | let expires_at = demo_recap::details(occurrence, now).expires_at?; |
| 136 | Some(( |
| 137 | occurrence.message_id.clone(), |
| 138 | days_until_expiry(expires_at, now), |
| 139 | )) |
| 140 | }) |
| 141 | .collect(); |
| 142 | self.recap.listing = Load::Ready(occurrences); |
| 143 | return; |
| 144 | } |
| 145 | let Some(engine) = engine else { |
| 146 | return; |
| 147 | }; |
| 148 | self.recap.expiry_days = expiry_days(&cached_expiry(state, &conversation_id), Utc::now()); |
| 149 | let generation = self.recap.generation; |
| 150 | let receiver = runtime::spawn(async move { |
| 151 | Recap::new(engine.remote().session()) |
| 152 | .occurrences(&conversation_id) |
| 153 | .await |
| 154 | }); |
| 155 | cx.spawn(async move |this, cx| { |
| 156 | let outcome = match receiver.await { |
| 157 | Ok(Ok(occurrences)) => Ok(occurrences), |
| 158 | Ok(Err(error)) => Err(short_error(&error)), |
| 159 | Err(error) => Err(short_error(&error)), |
| 160 | }; |
| 161 | this.update(cx, |this, cx| { |
| 162 | if generation != this.recap.generation { |
| 163 | return; |
| 164 | } |
| 165 | this.recap.listing = match outcome { |
| 166 | Ok(occurrences) => { |
| 167 | this.load_expiry(&occurrences, cx); |
| 168 | Load::Ready(occurrences) |
| 169 | } |
| 170 | Err(reason) => Load::Failed(reason), |
| 171 | }; |
| 172 | if this.pane == ChannelPane::Recap { |
| 173 | this.select_occurrence(0, cx); |
| 174 | } |
| 175 | cx.notify(); |
| 176 | }) |
| 177 | .ok(); |
| 178 | }) |
| 179 | .detach(); |
| 180 | } |
| 181 | |
| 182 | /// The chat's recording lines and recap cards say when a recording expires; the value comes |
| 183 | /// from the same SharePoint list item the Recap tab reads. |
| 184 | fn load_expiry(&mut self, occurrences: &[Occurrence], cx: &mut Context<Self>) { |
| 185 | let (Some(engine), Some(conversation_id)) = |
| 186 | (self.app.read(cx).engine.clone(), self.conversation_id()) |
| 187 | else { |
| 188 | return; |
| 189 | }; |
| 190 | let newest: Vec<Occurrence> = occurrences |
| 191 | .iter() |
| 192 | .take(EXPIRY_OCCURRENCES) |
| 193 | .cloned() |
| 194 | .collect(); |
| 195 | if newest.is_empty() { |
| 196 | return; |
| 197 | } |
| 198 | let receiver = runtime::spawn(async move { |
| 199 | let recap = Recap::new(engine.remote().session()); |
| 200 | let mut found = Vec::new(); |
| 201 | for occurrence in newest { |
| 202 | if let Ok(details) = recap.details(&occurrence).await { |
| 203 | found.push((occurrence.message_id, details.expires_at)); |
| 204 | } |
| 205 | } |
| 206 | found |
| 207 | }); |
| 208 | cx.spawn(async move |this, cx| { |
| 209 | let Ok(found) = receiver.await else { |
| 210 | return; |
| 211 | }; |
| 212 | this.update(cx, |this, cx| { |
| 213 | if this.conversation_id().as_deref() != Some(conversation_id.as_str()) { |
| 214 | return; |
| 215 | } |
| 216 | let state = this.app.read(cx); |
| 217 | let mut cached = cached_expiry(state, &conversation_id); |
| 218 | cached.extend( |
| 219 | found |
| 220 | .into_iter() |
| 221 | .filter_map(|(message_id, expires_at)| Some((message_id, expires_at?))), |
| 222 | ); |
| 223 | if let Ok(json) = serde_json::to_string(&cached) { |
| 224 | let key = format!("{EXPIRY_CACHE_PREFIX}{conversation_id}"); |
| 225 | state.store.set_meta(&key, &json).ok(); |
| 226 | } |
| 227 | let expiry = expiry_days(&cached, Utc::now()); |
| 228 | if expiry != this.recap.expiry_days { |
| 229 | this.recap.expiry_days = expiry; |
| 230 | this.rebuild(false, cx); |
| 231 | } |
| 232 | }) |
| 233 | .ok(); |
| 234 | }) |
| 235 | .detach(); |
| 236 | } |
| 237 | |
| 238 | pub(super) fn meeting_info(&self, app: &AppState) -> MeetingInfo { |
| 239 | let Some(current) = self.current.as_ref() else { |
| 240 | return MeetingInfo::default(); |
| 241 | }; |
| 242 | let conversation_id = current.selection.conversation_id(); |
| 243 | MeetingInfo { |
| 244 | title: Some(current.title.clone()), |
| 245 | slots: app |
| 246 | .calendar_events |
| 247 | .iter() |
| 248 | .filter(|event| event.thread_id.as_deref() == Some(conversation_id)) |
| 249 | .map(|event| MeetingSlot { |
| 250 | start: event.start, |
| 251 | end: event.end, |
| 252 | }) |
| 253 | .collect(), |
| 254 | expiry_days: self.recap.expiry_days.clone(), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /// Teams opens a meeting chat recording in the Recap tab at its occurrence and plays any |
| 259 | /// other recording (channel meetings) in the browser. |
| 260 | pub(super) fn open_recording( |
| 261 | &mut self, |
| 262 | message_id: &str, |
| 263 | url: Option<&str>, |
| 264 | cx: &mut Context<Self>, |
| 265 | ) { |
| 266 | let occurrence = self |
| 267 | .occurrences() |
| 268 | .iter() |
| 269 | .position(|occurrence| occurrence.message_id == message_id); |
| 270 | match (occurrence, url) { |
| 271 | (Some(index), _) if self.is_meeting_chat(cx) => { |
| 272 | self.select_pane(ChannelPane::Recap, cx); |
| 273 | if self.recap.selected != index { |
| 274 | self.select_occurrence(index, cx); |
| 275 | } |
| 276 | } |
| 277 | (_, Some(url)) => app_base::open_link::open_url(cx, url), |
| 278 | (_, None) => {} |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | pub(super) fn open_recap_pane(&mut self, cx: &mut Context<Self>) { |
| 283 | self.refresh_shared_items(cx); |
| 284 | if matches!(self.recap.details, Load::Loading) { |
| 285 | self.select_occurrence(0, cx); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn select_occurrence(&mut self, index: usize, cx: &mut Context<Self>) { |
| 290 | let Some(occurrence) = self.occurrences().get(index).cloned() else { |
| 291 | return; |
| 292 | }; |
| 293 | self.recap.selected = index; |
| 294 | self.recap.details = Load::Loading; |
| 295 | self.recap.transcript = Load::Loading; |
| 296 | self.recap.attendance = Load::Loading; |
| 297 | self.recap.downloads.clear(); |
| 298 | self.recap.generation += 1; |
| 299 | let generation = self.recap.generation; |
| 300 | cx.notify(); |
| 301 | let state = self.app.read(cx); |
| 302 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 303 | if demo { |
| 304 | let now = Utc::now(); |
| 305 | self.recap.details = Load::Ready(demo_recap::details(&occurrence, now)); |
| 306 | self.recap.transcript = Load::Ready(demo_recap::transcript()); |
| 307 | self.recap.attendance = Load::Ready(demo_recap::attendance(now)); |
| 308 | return; |
| 309 | } |
| 310 | let (Some(engine), Some(conversation_id)) = (engine, self.conversation_id()) else { |
| 311 | return; |
| 312 | }; |
| 313 | let attendance_engine = engine.clone(); |
| 314 | let loaded = runtime::spawn(async move { |
| 315 | let recap = Recap::new(engine.remote().session()); |
| 316 | let details = recap.details(&occurrence).await?; |
| 317 | let transcript = recap.transcript(&details).await.ok(); |
| 318 | Ok::<_, chatsvc::TranscriptError>((details, transcript)) |
| 319 | }); |
| 320 | let attendance = runtime::spawn(async move { |
| 321 | Recap::new(attendance_engine.remote().session()) |
| 322 | .attendance(&conversation_id) |
| 323 | .await |
| 324 | }); |
| 325 | cx.spawn(async move |this, cx| { |
| 326 | let loaded = match loaded.await { |
| 327 | Ok(Ok(loaded)) => Ok(loaded), |
| 328 | Ok(Err(error)) => Err(short_error(&error)), |
| 329 | Err(error) => Err(short_error(&error)), |
| 330 | }; |
| 331 | this.update(cx, |this, cx| { |
| 332 | if generation != this.recap.generation { |
| 333 | return; |
| 334 | } |
| 335 | match loaded { |
| 336 | Ok((details, transcript)) => { |
| 337 | this.recap.details = Load::Ready(details); |
| 338 | this.recap.transcript = Load::Ready(transcript.unwrap_or_default()); |
| 339 | } |
| 340 | Err(reason) => { |
| 341 | this.recap.details = Load::Failed(reason.clone()); |
| 342 | this.recap.transcript = Load::Failed(reason); |
| 343 | } |
| 344 | } |
| 345 | cx.notify(); |
| 346 | }) |
| 347 | .ok(); |
| 348 | }) |
| 349 | .detach(); |
| 350 | cx.spawn(async move |this, cx| { |
| 351 | let outcome = match attendance.await { |
| 352 | Ok(Ok(attendance)) => Ok(attendance), |
| 353 | Ok(Err(error)) => Err(short_error(&error)), |
| 354 | Err(error) => Err(short_error(&error)), |
| 355 | }; |
| 356 | this.update(cx, |this, cx| { |
| 357 | if generation != this.recap.generation { |
| 358 | return; |
| 359 | } |
| 360 | this.recap.attendance = match outcome { |
| 361 | Ok(attendance) => Load::Ready(attendance), |
| 362 | Err(reason) => Load::Failed(reason), |
| 363 | }; |
| 364 | cx.notify(); |
| 365 | }) |
| 366 | .ok(); |
| 367 | }) |
| 368 | .detach(); |
| 369 | } |
| 370 | |
| 371 | pub(super) fn render_chat_tab_bar(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 372 | if !self.in_chat() { |
| 373 | return None; |
| 374 | } |
| 375 | let with_recap = !self.occurrences().is_empty() && self.is_meeting_chat(cx); |
| 376 | let view = cx.weak_entity(); |
| 377 | let select = Rc::new(move |pane, cx: &mut App| { |
| 378 | view.update(cx, |this, cx| this.select_pane(pane, cx)).ok(); |
| 379 | }); |
| 380 | Some(render_chat_bar(self.pane, with_recap, select)) |
| 381 | } |
| 382 | |
| 383 | pub(super) fn render_recap(&self, cx: &mut Context<Self>) -> AnyElement { |
| 384 | let view = cx.weak_entity(); |
| 385 | let run: RecapRun = Rc::new(move |command, window, cx| { |
| 386 | view.update(cx, |this, cx| this.run_recap(command, window, cx)) |
| 387 | .ok(); |
| 388 | }); |
| 389 | let file_indices: Rc<Vec<usize>> = Rc::new( |
| 390 | self.shared |
| 391 | .items |
| 392 | .iter() |
| 393 | .enumerate() |
| 394 | .filter(|(_, item)| matches!(item.content, SharedContent::File(_))) |
| 395 | .map(|(index, _)| index) |
| 396 | .collect(), |
| 397 | ); |
| 398 | let file_states = self.item_download_states(); |
| 399 | let files = file_indices |
| 400 | .iter() |
| 401 | .map(|index| self.shared.items[*index].clone()) |
| 402 | .collect::<Vec<_>>(); |
| 403 | let file_labels = file_indices |
| 404 | .iter() |
| 405 | .map(|index| { |
| 406 | crate::views::shared_tab::download_label( |
| 407 | file_states.get(*index).and_then(Option::as_ref), |
| 408 | ) |
| 409 | }) |
| 410 | .collect(); |
| 411 | let file_run: SharedRun = { |
| 412 | let view = cx.weak_entity(); |
| 413 | Rc::new(move |command, window, cx| { |
| 414 | let command = match command { |
| 415 | SharedCommand::OpenItem(index) => SharedCommand::OpenItem(file_indices[index]), |
| 416 | SharedCommand::JumpToPost(index) => { |
| 417 | SharedCommand::JumpToPost(file_indices[index]) |
| 418 | } |
| 419 | other => other, |
| 420 | }; |
| 421 | view.update(cx, |this, cx| { |
| 422 | if matches!(command, SharedCommand::JumpToPost(_)) { |
| 423 | this.pane = ChannelPane::Posts; |
| 424 | } |
| 425 | this.run_shared(command, window, cx) |
| 426 | }) |
| 427 | .ok(); |
| 428 | }) |
| 429 | }; |
| 430 | let can_delete = self.can_delete_recording(cx); |
| 431 | render_recap( |
| 432 | RecapView { |
| 433 | occurrences: self.occurrences(), |
| 434 | selected: self.recap.selected, |
| 435 | details: &self.recap.details, |
| 436 | transcript: &self.recap.transcript, |
| 437 | attendance: &self.recap.attendance, |
| 438 | search: &self.recap_search, |
| 439 | query: &self.recap.query, |
| 440 | can_delete, |
| 441 | recording_state: self.recap.downloads.get(&RecapDownload::Recording), |
| 442 | vtt_state: self |
| 443 | .recap |
| 444 | .downloads |
| 445 | .get(&RecapDownload::Transcript(TranscriptFormat::Vtt)), |
| 446 | docx_state: self |
| 447 | .recap |
| 448 | .downloads |
| 449 | .get(&RecapDownload::Transcript(TranscriptFormat::Docx)), |
| 450 | files: &files, |
| 451 | file_labels, |
| 452 | file_run, |
| 453 | now: Utc::now(), |
| 454 | }, |
| 455 | &run, |
| 456 | ) |
| 457 | } |
| 458 | |
| 459 | fn can_delete_recording(&self, cx: &App) -> bool { |
| 460 | let my_user_id = self |
| 461 | .app |
| 462 | .read(cx) |
| 463 | .directory |
| 464 | .me |
| 465 | .as_ref() |
| 466 | .map(|me| me.user_id.as_str()); |
| 467 | match (self.selected_occurrence(), my_user_id) { |
| 468 | (Some(occurrence), Some(my_user_id)) => { |
| 469 | occurrence.organizer_id.as_deref() == Some(my_user_id) |
| 470 | } |
| 471 | _ => false, |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | fn run_recap(&mut self, command: RecapCommand, window: &mut Window, cx: &mut Context<Self>) { |
| 476 | match command { |
| 477 | RecapCommand::Occurrence(index) => self.select_occurrence(index, cx), |
| 478 | RecapCommand::Retry => self.select_occurrence(self.recap.selected, cx), |
| 479 | RecapCommand::Play => { |
| 480 | if let Some(occurrence) = self.selected_occurrence() { |
| 481 | app_base::open_link::open_url(cx, &occurrence.recording_url); |
| 482 | } |
| 483 | } |
| 484 | RecapCommand::Download(download) => self.pick_save_target(download, cx), |
| 485 | RecapCommand::AskDelete => self.confirm_delete(window, cx), |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | fn confirm_delete(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 490 | let view = cx.weak_entity(); |
| 491 | window.open_alert_dialog(cx, move |alert, _, _| { |
| 492 | let view = view.clone(); |
| 493 | let cancel = Button::new("recap-delete-cancel") |
| 494 | .label(tr("common.cancel")) |
| 495 | .on_click(|_, window, cx| window.close_dialog(cx)); |
| 496 | let delete = Button::new("recap-delete-confirm") |
| 497 | .label(tr("common.delete")) |
| 498 | .danger() |
| 499 | .on_click(move |_, window, cx| { |
| 500 | view.update(cx, |this, cx| this.delete_recording(cx)).ok(); |
| 501 | window.close_dialog(cx); |
| 502 | }); |
| 503 | alert |
| 504 | .title(DELETE_QUESTION) |
| 505 | .footer(DialogFooter::new().child(cancel).child(delete)) |
| 506 | .on_ok(|_, _, _| false) |
| 507 | }); |
| 508 | } |
| 509 | |
| 510 | fn delete_recording(&mut self, cx: &mut Context<Self>) { |
| 511 | let (Some(occurrence), Some(details)) = ( |
| 512 | self.selected_occurrence(), |
| 513 | self.recap.details.ready().cloned(), |
| 514 | ) else { |
| 515 | return; |
| 516 | }; |
| 517 | let state = self.app.read(cx); |
| 518 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 519 | if demo { |
| 520 | self.finish_delete(&occurrence, Ok(()), cx); |
| 521 | return; |
| 522 | } |
| 523 | let Some(engine) = engine else { |
| 524 | return; |
| 525 | }; |
| 526 | let receiver = runtime::spawn(async move { |
| 527 | Recap::new(engine.remote().session()) |
| 528 | .delete_recording(&details) |
| 529 | .await |
| 530 | }); |
| 531 | cx.spawn(async move |this, cx| { |
| 532 | let outcome = match receiver.await { |
| 533 | Ok(Ok(())) => Ok(()), |
| 534 | Ok(Err(error)) => Err(short_error(&error)), |
| 535 | Err(error) => Err(short_error(&error)), |
| 536 | }; |
| 537 | this.update(cx, |this, cx| this.finish_delete(&occurrence, outcome, cx)) |
| 538 | .ok(); |
| 539 | }) |
| 540 | .detach(); |
| 541 | } |
| 542 | |
| 543 | fn finish_delete( |
| 544 | &mut self, |
| 545 | occurrence: &Occurrence, |
| 546 | outcome: Result<(), String>, |
| 547 | cx: &mut Context<Self>, |
| 548 | ) { |
| 549 | if let Err(reason) = outcome { |
| 550 | self.notice = Some(format!("Recording not deleted: {reason}")); |
| 551 | cx.notify(); |
| 552 | return; |
| 553 | } |
| 554 | let remaining: Vec<Occurrence> = self |
| 555 | .occurrences() |
| 556 | .iter() |
| 557 | .filter(|other| other.message_id != occurrence.message_id) |
| 558 | .cloned() |
| 559 | .collect(); |
| 560 | let empty = remaining.is_empty(); |
| 561 | self.recap.listing = Load::Ready(remaining); |
| 562 | if empty { |
| 563 | self.pane = ChannelPane::Posts; |
| 564 | cx.notify(); |
| 565 | } else { |
| 566 | self.select_occurrence(0, cx); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | fn pick_save_target(&mut self, download: RecapDownload, cx: &mut Context<Self>) { |
| 571 | let Some(occurrence) = self.selected_occurrence() else { |
| 572 | return; |
| 573 | }; |
| 574 | if matches!( |
| 575 | self.recap.downloads.get(&download), |
| 576 | Some(DownloadState::Saving(_)) |
| 577 | ) { |
| 578 | return; |
| 579 | } |
| 580 | let extension = match download { |
| 581 | RecapDownload::Recording => RECORDING_EXTENSION, |
| 582 | RecapDownload::Transcript(format) => format.query(), |
| 583 | }; |
| 584 | let name = save_name(&occurrence, extension); |
| 585 | let receiver = cx.prompt_for_new_path(&downloads::downloads_directory(), Some(&name)); |
| 586 | cx.spawn(async move |this, cx| { |
| 587 | if let Ok(Ok(Some(target))) = receiver.await { |
| 588 | this.update(cx, |this, cx| { |
| 589 | this.save_to(download, occurrence, target, cx) |
| 590 | }) |
| 591 | .ok(); |
| 592 | } |
| 593 | }) |
| 594 | .detach(); |
| 595 | } |
| 596 | |
| 597 | fn save_to( |
| 598 | &mut self, |
| 599 | download: RecapDownload, |
| 600 | occurrence: Occurrence, |
| 601 | target: PathBuf, |
| 602 | cx: &mut Context<Self>, |
| 603 | ) { |
| 604 | self.recap |
| 605 | .downloads |
| 606 | .insert(download, DownloadState::Saving(0)); |
| 607 | cx.notify(); |
| 608 | let generation = self.recap.generation; |
| 609 | let state = self.app.read(cx); |
| 610 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 611 | let details = self.recap.details.ready().cloned(); |
| 612 | let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::<SaveEvent>(); |
| 613 | if demo { |
| 614 | let outcome = std::fs::write(&target, demo_recap::DEMO_FILE_BYTES) |
| 615 | .map(|_| target) |
| 616 | .map_err(|error| short_error(&error)); |
| 617 | self.recap |
| 618 | .downloads |
| 619 | .insert(download, finished_state(outcome)); |
| 620 | return; |
| 621 | } |
| 622 | let (Some(engine), Some(details)) = (engine, details) else { |
| 623 | self.recap |
| 624 | .downloads |
| 625 | .insert(download, DownloadState::Failed("not connected".to_owned())); |
| 626 | return; |
| 627 | }; |
| 628 | drop(runtime::spawn(async move { |
| 629 | let outcome = |
| 630 | save_download(&engine, download, &occurrence, &details, target, &sender).await; |
| 631 | let _ = sender.send(SaveEvent::Finished(outcome)); |
| 632 | })); |
| 633 | cx.spawn(async move |this, cx| { |
| 634 | while let Some(event) = receiver.recv().await { |
| 635 | let updated = this.update(cx, |this, cx| { |
| 636 | if generation == this.recap.generation { |
| 637 | match event { |
| 638 | SaveEvent::Progress(percent) => { |
| 639 | this.recap |
| 640 | .downloads |
| 641 | .insert(download, DownloadState::Saving(percent)); |
| 642 | } |
| 643 | SaveEvent::Finished(outcome) => { |
| 644 | this.recap |
| 645 | .downloads |
| 646 | .insert(download, finished_state(outcome)); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | cx.notify(); |
| 651 | }); |
| 652 | if updated.is_err() { |
| 653 | return; |
| 654 | } |
| 655 | } |
| 656 | }) |
| 657 | .detach(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | fn finished_state(outcome: Result<PathBuf, String>) -> DownloadState { |
| 662 | match outcome { |
| 663 | Ok(path) => DownloadState::Saved(path), |
| 664 | Err(reason) => DownloadState::Failed(reason), |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | async fn save_download( |
| 669 | engine: &crate::backend::Engine, |
| 670 | download: RecapDownload, |
| 671 | occurrence: &Occurrence, |
| 672 | details: &RecordingDetails, |
| 673 | target: PathBuf, |
| 674 | events: &tokio::sync::mpsc::UnboundedSender<SaveEvent>, |
| 675 | ) -> Result<PathBuf, String> { |
| 676 | match download { |
| 677 | RecapDownload::Transcript(format) => { |
| 678 | let bytes = Recap::new(engine.remote().session()) |
| 679 | .transcript_file(details, format) |
| 680 | .await |
| 681 | .map_err(|error| short_error(&error))?; |
| 682 | std::fs::write(&target, bytes).map_err(|error| short_error(&error))?; |
| 683 | } |
| 684 | RecapDownload::Recording => { |
| 685 | let mut file = std::fs::File::create(&target).map_err(|error| short_error(&error))?; |
| 686 | let progress = |percent| { |
| 687 | let _ = events.send(SaveEvent::Progress(percent)); |
| 688 | }; |
| 689 | let result = engine |
| 690 | .download_file( |
| 691 | &occurrence.recording_url, |
| 692 | |bytes: &[u8]| file.write_all(bytes), |
| 693 | progress, |
| 694 | ) |
| 695 | .await; |
| 696 | if let Err(error) = result { |
| 697 | drop(file); |
| 698 | let _ = std::fs::remove_file(&target); |
| 699 | return Err(short_error(&error)); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | Ok(target) |
| 704 | } |
| 705 | |
| 706 | /// Expiry from the last open, so the recording lines have their final text in the first frame. |
| 707 | fn cached_expiry(state: &AppState, conversation_id: &str) -> HashMap<String, DateTime<Utc>> { |
| 708 | state |
| 709 | .store |
| 710 | .meta(&format!("{EXPIRY_CACHE_PREFIX}{conversation_id}")) |
| 711 | .ok() |
| 712 | .flatten() |
| 713 | .and_then(|json| serde_json::from_str(&json).ok()) |
| 714 | .unwrap_or_default() |
| 715 | } |
| 716 | |
| 717 | fn expiry_days( |
| 718 | expiry: &HashMap<String, DateTime<Utc>>, |
| 719 | now: DateTime<Utc>, |
| 720 | ) -> HashMap<String, i64> { |
| 721 | expiry |
| 722 | .iter() |
| 723 | .map(|(message_id, expires_at)| (message_id.clone(), days_until_expiry(*expires_at, now))) |
| 724 | .collect() |
| 725 | } |