| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::sync::Arc; |
| 3 | use std::time::Instant; |
| 4 | |
| 5 | use chatsvc::TypingEvent; |
| 6 | use chrono::{DateTime, Utc}; |
| 7 | use gpui_kit::*; |
| 8 | use store::{ChatRecord, Sidebar, Store}; |
| 9 | use teams_core::{ |
| 10 | ChatApp, ChatSection, ChatSectionSettings, CoreEvent, ImageRef, PinnedMessage, PresenceStatus, |
| 11 | ScheduledDraft, |
| 12 | }; |
| 13 | |
| 14 | use crate::backend::{BackendEvent, ConnectionState, Engine, LiveState}; |
| 15 | use crate::call::{ActiveCall, CallLauncher, CallMedia, MissedCall, Rings}; |
| 16 | use crate::card_state::{CardState, TaskDialogState}; |
| 17 | use crate::data::{self, Directory}; |
| 18 | use crate::image_view_gallery::ViewerRequest; |
| 19 | use crate::local_previews::{LocalPreview, load_local_previews}; |
| 20 | use crate::message_actions::{ForwardSource, SavedSet}; |
| 21 | use crate::notice::Notice; |
| 22 | use crate::notify::LateArrivals; |
| 23 | use crate::profile_state::{ProfileCache, ProfileRequest}; |
| 24 | use crate::translation::TranslationState; |
| 25 | use crate::typing::TypingState; |
| 26 | use calling::meeting::LiveMeeting; |
| 27 | |
| 28 | const COLLAPSED_META_KEY: &str = "ui.collapsed_sections"; |
| 29 | const RECENT_MESSAGES_FOR_TYPING: usize = 5; |
| 30 | |
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 32 | pub enum Selection { |
| 33 | Chat(String), |
| 34 | Channel(String), |
| 35 | } |
| 36 | |
| 37 | impl Selection { |
| 38 | pub fn conversation_id(&self) -> &str { |
| 39 | match self { |
| 40 | Selection::Chat(id) | Selection::Channel(id) => id, |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub enum AppEvent { |
| 46 | Sidebar, |
| 47 | Selection, |
| 48 | Messages(String), |
| 49 | Images(Vec<String>), |
| 50 | Jump, |
| 51 | Status, |
| 52 | Directory, |
| 53 | Cards(String), |
| 54 | TaskDialog, |
| 55 | Typing, |
| 56 | LocalPreviews, |
| 57 | Outbox(String), |
| 58 | Scheduled, |
| 59 | Pins(String), |
| 60 | Saved, |
| 61 | CallHistory, |
| 62 | Translation, |
| 63 | Forward, |
| 64 | ImageViewer, |
| 65 | StatusMessage, |
| 66 | Settings(crate::settings_section::SettingsSection), |
| 67 | Profile, |
| 68 | Call, |
| 69 | Ring, |
| 70 | LiveMeeting, |
| 71 | MissedCall(MissedCall), |
| 72 | MeetingReminder, |
| 73 | MeetingChime, |
| 74 | CallTone(chatsvc::sounds::CallTone), |
| 75 | SoundSettings, |
| 76 | } |
| 77 | |
| 78 | #[derive(Debug, Clone, Copy, Default)] |
| 79 | pub struct Mode { |
| 80 | pub demo: bool, |
| 81 | pub read_only: bool, |
| 82 | pub demo_sync: Option<std::time::Duration>, |
| 83 | } |
| 84 | |
| 85 | pub struct AppState { |
| 86 | pub store: Arc<Store>, |
| 87 | pub engine: Option<Arc<Engine>>, |
| 88 | pub sidebar: Sidebar, |
| 89 | pub favorite_ids: Vec<String>, |
| 90 | pub selection: Option<Selection>, |
| 91 | pub late_arrivals: LateArrivals, |
| 92 | pub new_chat: bool, |
| 93 | pub connection: ConnectionState, |
| 94 | pub live: LiveState, |
| 95 | pub last_sync: Option<DateTime<Utc>>, |
| 96 | pub mode: Mode, |
| 97 | pub directory: Directory, |
| 98 | pub collapsed: HashSet<String>, |
| 99 | pub expanded_lists: HashSet<String>, |
| 100 | pub start_on_channels: bool, |
| 101 | pub pending_jump: Option<(String, String)>, |
| 102 | pub cards: CardState, |
| 103 | pub task_dialog: Option<TaskDialogState>, |
| 104 | pub bot_apps: HashMap<String, Vec<ChatApp>>, |
| 105 | pub bot_apps_pending: HashSet<String>, |
| 106 | pub notice: Option<Notice>, |
| 107 | pub connection_banner: crate::connection_banner::BannerTiming, |
| 108 | pub live_retry: Option<Arc<tokio::sync::Notify>>, |
| 109 | pub notice_count: u64, |
| 110 | pub keep_unread: Option<String>, |
| 111 | pub typing: TypingState, |
| 112 | typing_timer_running: bool, |
| 113 | pub local_previews: HashMap<String, LocalPreview>, |
| 114 | pub scheduled: Vec<ScheduledDraft>, |
| 115 | pub scheduled_polling: bool, |
| 116 | pub saved: SavedSet, |
| 117 | pub call_history: Vec<teams_core::CallLogEntry>, |
| 118 | pub calendar_events: Vec<chatsvc::CalendarEvent>, |
| 119 | pub calendar_nudge: Option<tokio::sync::mpsc::UnboundedSender<()>>, |
| 120 | pub calls_list: crate::call::CallsList, |
| 121 | pub pins: HashMap<String, Vec<PinnedMessage>>, |
| 122 | pub forward_request: Option<ForwardSource>, |
| 123 | pub image_viewer_request: Option<ViewerRequest>, |
| 124 | pub own_status: PresenceStatus, |
| 125 | pub own_email: String, |
| 126 | pub own_time_zone: Option<String>, |
| 127 | pub own_time_zone_requested: bool, |
| 128 | pub status_menu_open: bool, |
| 129 | pub status_message_request: bool, |
| 130 | pub profiles: ProfileCache, |
| 131 | pub profile_request: Option<ProfileRequest>, |
| 132 | pub call: Option<ActiveCall>, |
| 133 | pub held_call: Option<ActiveCall>, |
| 134 | pub call_end: Option<crate::call::end_screen::CallEndScreen>, |
| 135 | pub call_media: Option<Entity<CallMedia>>, |
| 136 | pub call_launcher: Option<Arc<CallLauncher>>, |
| 137 | pub call_count: u64, |
| 138 | pub call_share_sound: bool, |
| 139 | pub recording_policy: teams_core::RecordingPolicy, |
| 140 | pub account_sounds: Option<chatsvc::sound_settings::SoundSettings>, |
| 141 | pub own_tenant: Option<String>, |
| 142 | pub call_share_motion: bool, |
| 143 | pub call_share_standout: bool, |
| 144 | pub call_report_enabled: bool, |
| 145 | pub call_strip_width: f32, |
| 146 | pub call_view: crate::call::ViewSettings, |
| 147 | pub call_background: crate::call::BackgroundPick, |
| 148 | pub call_camera: calling::DeviceChoice, |
| 149 | pub call_input: calling::DeviceChoice, |
| 150 | pub call_output: calling::DeviceChoice, |
| 151 | pub call_mic: calling::MicSettings, |
| 152 | pub call_effects: calling::CameraEffects, |
| 153 | pub meet_now_pending: bool, |
| 154 | pub prejoin: Option<crate::call::PendingJoin>, |
| 155 | pub prejoin_count: u64, |
| 156 | pub join_options: crate::call::JoinOptions, |
| 157 | pub rsvp_send_response: bool, |
| 158 | pub meeting_toasts: crate::call::MeetingToasts, |
| 159 | pub demo_meetings: Vec<chatsvc::CalendarEvent>, |
| 160 | pub call_backgrounds: crate::call::BackgroundLibrary, |
| 161 | pub transfer_picker: Option<AnyView>, |
| 162 | pub transcript_panel: Option<AnyView>, |
| 163 | pub rings: Rings, |
| 164 | pub live_meetings: HashMap<String, LiveMeeting>, |
| 165 | pub live_refreshing: HashSet<String>, |
| 166 | pub translation: TranslationState, |
| 167 | pub section_changes: crate::section_actions::SectionChanges, |
| 168 | } |
| 169 | |
| 170 | pub struct AppHandle(pub Entity<AppState>); |
| 171 | |
| 172 | impl Global for AppHandle {} |
| 173 | |
| 174 | impl EventEmitter<AppEvent> for AppState {} |
| 175 | |
| 176 | pub fn selection_for(sidebar: &Sidebar, conversation_id: &str) -> Option<Selection> { |
| 177 | if sidebar.chats.iter().any(|chat| chat.id == conversation_id) { |
| 178 | return Some(Selection::Chat(conversation_id.to_owned())); |
| 179 | } |
| 180 | sidebar |
| 181 | .teams |
| 182 | .iter() |
| 183 | .flat_map(|team| team.channels.iter()) |
| 184 | .any(|channel| channel.id == conversation_id) |
| 185 | .then(|| Selection::Channel(conversation_id.to_owned())) |
| 186 | } |
| 187 | |
| 188 | pub fn chat_title(chat: &ChatRecord) -> String { |
| 189 | chat.display_title() |
| 190 | } |
| 191 | |
| 192 | pub fn composer_name(sidebar: &Sidebar, selection: &Selection) -> String { |
| 193 | let unnamed_group = match selection { |
| 194 | Selection::Chat(id) => sidebar.chats.iter().any(|chat| { |
| 195 | &chat.id == id |
| 196 | && chat.title.trim().is_empty() |
| 197 | && !chat.kind.eq_ignore_ascii_case("oneOnOne") |
| 198 | }), |
| 199 | Selection::Channel(_) => false, |
| 200 | }; |
| 201 | if unnamed_group { |
| 202 | String::new() |
| 203 | } else { |
| 204 | selection_title(sidebar, selection) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | pub fn selection_title(sidebar: &Sidebar, selection: &Selection) -> String { |
| 209 | match selection { |
| 210 | Selection::Chat(id) => sidebar |
| 211 | .chats |
| 212 | .iter() |
| 213 | .find(|chat| &chat.id == id) |
| 214 | .map(chat_title) |
| 215 | .unwrap_or_else(|| "Chat".to_owned()), |
| 216 | Selection::Channel(id) => sidebar |
| 217 | .teams |
| 218 | .iter() |
| 219 | .find_map(|team| { |
| 220 | team.channels |
| 221 | .iter() |
| 222 | .find(|channel| &channel.id == id) |
| 223 | .map(|channel| format!("{} / {}", team.team.name, channel.name)) |
| 224 | }) |
| 225 | .unwrap_or_else(|| "Channel".to_owned()), |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | impl AppState { |
| 230 | pub fn new(store: Arc<Store>, mode: Mode) -> Self { |
| 231 | let sidebar = store.sidebar().unwrap_or_default(); |
| 232 | let mut state = AppState { |
| 233 | store, |
| 234 | engine: None, |
| 235 | sidebar, |
| 236 | favorite_ids: Vec::new(), |
| 237 | selection: None, |
| 238 | late_arrivals: LateArrivals::default(), |
| 239 | new_chat: false, |
| 240 | connection: if mode.demo { |
| 241 | ConnectionState::Online |
| 242 | } else { |
| 243 | ConnectionState::Connecting |
| 244 | }, |
| 245 | live: LiveState::Off, |
| 246 | last_sync: None, |
| 247 | mode, |
| 248 | directory: Directory::default(), |
| 249 | collapsed: HashSet::new(), |
| 250 | expanded_lists: HashSet::new(), |
| 251 | start_on_channels: false, |
| 252 | pending_jump: None, |
| 253 | cards: CardState::default(), |
| 254 | task_dialog: None, |
| 255 | bot_apps: HashMap::new(), |
| 256 | bot_apps_pending: HashSet::new(), |
| 257 | notice: None, |
| 258 | connection_banner: Default::default(), |
| 259 | live_retry: None, |
| 260 | notice_count: 0, |
| 261 | keep_unread: None, |
| 262 | typing: TypingState::default(), |
| 263 | typing_timer_running: false, |
| 264 | local_previews: HashMap::new(), |
| 265 | scheduled: Vec::new(), |
| 266 | scheduled_polling: false, |
| 267 | saved: SavedSet::default(), |
| 268 | call_history: Vec::new(), |
| 269 | calendar_events: Vec::new(), |
| 270 | calendar_nudge: None, |
| 271 | calls_list: crate::call::CallsList::default(), |
| 272 | pins: HashMap::new(), |
| 273 | forward_request: None, |
| 274 | image_viewer_request: None, |
| 275 | own_status: PresenceStatus::default(), |
| 276 | own_email: String::new(), |
| 277 | own_time_zone: None, |
| 278 | own_time_zone_requested: false, |
| 279 | status_menu_open: false, |
| 280 | status_message_request: false, |
| 281 | profiles: ProfileCache::new(), |
| 282 | profile_request: None, |
| 283 | call: None, |
| 284 | call_end: None, |
| 285 | held_call: None, |
| 286 | call_media: None, |
| 287 | call_launcher: None, |
| 288 | call_count: 0, |
| 289 | call_share_sound: false, |
| 290 | recording_policy: teams_core::RecordingPolicy::default(), |
| 291 | account_sounds: None, |
| 292 | own_tenant: None, |
| 293 | call_share_motion: false, |
| 294 | call_share_standout: false, |
| 295 | call_report_enabled: true, |
| 296 | call_strip_width: crate::call::STRIP_DEFAULT_WIDTH, |
| 297 | call_view: crate::call::ViewSettings::default(), |
| 298 | call_background: crate::call::BackgroundPick::None, |
| 299 | call_camera: calling::DeviceChoice::SystemDefault, |
| 300 | call_input: calling::DeviceChoice::SystemDefault, |
| 301 | call_output: calling::DeviceChoice::SystemDefault, |
| 302 | call_mic: calling::MicSettings::default(), |
| 303 | call_effects: calling::CameraEffects::default(), |
| 304 | meet_now_pending: false, |
| 305 | prejoin: None, |
| 306 | prejoin_count: 0, |
| 307 | join_options: crate::call::JoinOptions::default(), |
| 308 | rsvp_send_response: true, |
| 309 | meeting_toasts: crate::call::MeetingToasts::default(), |
| 310 | demo_meetings: Vec::new(), |
| 311 | call_backgrounds: if mode.demo { |
| 312 | crate::call::demo_library() |
| 313 | } else { |
| 314 | crate::call::BackgroundLibrary::new(crate::call::default_cache_directory()) |
| 315 | }, |
| 316 | transfer_picker: None, |
| 317 | transcript_panel: None, |
| 318 | rings: Rings::default(), |
| 319 | live_meetings: HashMap::new(), |
| 320 | live_refreshing: HashSet::new(), |
| 321 | translation: TranslationState::default(), |
| 322 | section_changes: crate::section_actions::SectionChanges::default(), |
| 323 | }; |
| 324 | state.local_previews = load_local_previews(&state.store); |
| 325 | state.collapsed = state.load_collapsed(); |
| 326 | state.call_share_sound = crate::call::load_share_sound(&state.store); |
| 327 | state.call_share_motion = crate::call::load_flag(&state.store, crate::call::SHARE_MOTION_META_KEY); |
| 328 | state.call_share_standout = crate::call::load_flag(&state.store, crate::call::SHARE_STANDOUT_META_KEY); |
| 329 | state.call_report_enabled = crate::call::load_report_enabled(&state.store); |
| 330 | state.call_strip_width = crate::call::load_strip_width(&state.store); |
| 331 | state.call_view = crate::call::ViewSettings::load(&state.store); |
| 332 | state.call_background = crate::call::load_background(&state.store); |
| 333 | state.call_camera = crate::call::load_camera(&state.store); |
| 334 | state.call_input = crate::call::load_input(&state.store); |
| 335 | state.call_output = crate::call::load_output(&state.store); |
| 336 | state.call_mic = crate::call::load_mic(&state.store); |
| 337 | state.call_effects = crate::call::load_camera_effects(&state.store); |
| 338 | state.join_options = crate::call::load_join_options(&state.store); |
| 339 | state.call_backgrounds.customs = crate::call::load_custom_backgrounds(&state.store); |
| 340 | state.load_cached_translation_settings(); |
| 341 | if !mode.demo { |
| 342 | state.directory.load_cached_presence(&state.store); |
| 343 | state.reload_directory(); |
| 344 | } |
| 345 | state |
| 346 | } |
| 347 | |
| 348 | fn load_collapsed(&self) -> HashSet<String> { |
| 349 | self.store |
| 350 | .meta(COLLAPSED_META_KEY) |
| 351 | .ok() |
| 352 | .flatten() |
| 353 | .map(|value| { |
| 354 | value |
| 355 | .lines() |
| 356 | .map(str::to_owned) |
| 357 | .filter(|id| !id.is_empty()) |
| 358 | .collect() |
| 359 | }) |
| 360 | .unwrap_or_default() |
| 361 | } |
| 362 | |
| 363 | pub fn toggle_collapsed(&mut self, section_id: &str, cx: &mut Context<Self>) { |
| 364 | if data::is_pending_folder(section_id) { |
| 365 | return; |
| 366 | } |
| 367 | let now_collapsed = self.collapsed.insert(section_id.to_owned()); |
| 368 | if !now_collapsed { |
| 369 | self.collapsed.remove(section_id); |
| 370 | } |
| 371 | self.save_collapsed(); |
| 372 | self.sync_folder_expanded(section_id, !now_collapsed); |
| 373 | cx.emit(AppEvent::Sidebar); |
| 374 | cx.notify(); |
| 375 | } |
| 376 | |
| 377 | pub fn older_chats(&self) -> (bool, bool) { |
| 378 | self.engine.as_ref().map_or((false, false), |engine| { |
| 379 | (engine.has_older_chats(), engine.is_loading_older_chats()) |
| 380 | }) |
| 381 | } |
| 382 | |
| 383 | pub fn request_older_chats(&self) { |
| 384 | let Some(engine) = self.engine.clone() else { |
| 385 | return; |
| 386 | }; |
| 387 | if !engine.has_older_chats() || engine.is_loading_older_chats() { |
| 388 | return; |
| 389 | } |
| 390 | drop(crate::runtime::spawn(async move { |
| 391 | if let Err(error) = engine.load_older_chats().await { |
| 392 | crate::diagnostics::record(format!("sync: older chats not loaded: {error}")); |
| 393 | } |
| 394 | })); |
| 395 | } |
| 396 | |
| 397 | pub fn toggle_list_expanded(&mut self, section_id: &str, cx: &mut Context<Self>) { |
| 398 | if !self.expanded_lists.remove(section_id) { |
| 399 | self.expanded_lists.insert(section_id.to_owned()); |
| 400 | } |
| 401 | cx.emit(AppEvent::Sidebar); |
| 402 | cx.notify(); |
| 403 | } |
| 404 | |
| 405 | fn save_collapsed(&self) { |
| 406 | let mut ids: Vec<&str> = self.collapsed.iter().map(String::as_str).collect(); |
| 407 | ids.sort_unstable(); |
| 408 | let _ = self.store.set_meta(COLLAPSED_META_KEY, &ids.join("\n")); |
| 409 | } |
| 410 | |
| 411 | fn sync_folder_expanded(&mut self, folder_id: &str, expanded: bool) { |
| 412 | let Some(folder) = self |
| 413 | .directory |
| 414 | .folders |
| 415 | .iter_mut() |
| 416 | .find(|folder| folder.id == folder_id && folder.expanded.is_some()) |
| 417 | else { |
| 418 | return; |
| 419 | }; |
| 420 | folder.expanded = Some(expanded); |
| 421 | let _ = self.store.set_folder_expanded(folder_id, expanded); |
| 422 | if let Some(engine) = self.engine.clone().filter(|_| !self.mode.read_only) { |
| 423 | let folder_id = folder_id.to_owned(); |
| 424 | drop(crate::runtime::spawn(async move { |
| 425 | engine.set_folder_expanded(&folder_id, expanded).await |
| 426 | })); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | fn adopt_folder_expanded(&mut self) { |
| 431 | let snapshot = self.collapsed.clone(); |
| 432 | for folder in &self.directory.folders { |
| 433 | match folder.expanded { |
| 434 | Some(true) => { |
| 435 | self.collapsed.remove(&folder.id); |
| 436 | } |
| 437 | Some(false) => { |
| 438 | self.collapsed.insert(folder.id.clone()); |
| 439 | } |
| 440 | None => {} |
| 441 | } |
| 442 | } |
| 443 | if self.collapsed != snapshot { |
| 444 | self.save_collapsed(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | pub fn set_chat_section( |
| 449 | &mut self, |
| 450 | section: ChatSection, |
| 451 | enabled: bool, |
| 452 | cx: &mut Context<Self>, |
| 453 | ) { |
| 454 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 455 | return; |
| 456 | }; |
| 457 | let previous = self.directory.section_settings; |
| 458 | let mut updated = previous; |
| 459 | updated.set(section, enabled); |
| 460 | self.apply_section_settings(updated, cx); |
| 461 | let Some(engine) = engine_or_demo else { |
| 462 | return; |
| 463 | }; |
| 464 | self.run_chat_action( |
| 465 | "Chat list setting", |
| 466 | async move { engine.set_section_enabled(section, enabled).await }, |
| 467 | move |state, cx| state.apply_section_settings(previous, cx), |
| 468 | |_, _| {}, |
| 469 | cx, |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | pub fn set_skin_tone(&mut self, tone: u8, cx: &mut Context<Self>) { |
| 474 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 475 | return; |
| 476 | }; |
| 477 | let previous = teams_core::skin_tone(&self.store); |
| 478 | let _ = teams_core::store_account_skin_tone(&self.store, tone); |
| 479 | let Some(engine) = engine_or_demo else { |
| 480 | return; |
| 481 | }; |
| 482 | self.run_chat_action( |
| 483 | "Skin tone", |
| 484 | async move { engine.set_skin_tone(tone).await }, |
| 485 | move |state, _| { |
| 486 | let _ = teams_core::store_account_skin_tone(&state.store, previous); |
| 487 | }, |
| 488 | |_, _| {}, |
| 489 | cx, |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | fn apply_section_settings(&mut self, settings: ChatSectionSettings, cx: &mut Context<Self>) { |
| 494 | self.directory.section_settings = settings; |
| 495 | let _ = teams_core::store_section_settings(&self.store, &settings); |
| 496 | cx.emit(AppEvent::Sidebar); |
| 497 | cx.notify(); |
| 498 | } |
| 499 | |
| 500 | pub fn reload_directory(&mut self) { |
| 501 | self.directory.me = data::me(&self.store); |
| 502 | self.own_email = self |
| 503 | .store |
| 504 | .meta("me_email") |
| 505 | .ok() |
| 506 | .flatten() |
| 507 | .unwrap_or_default(); |
| 508 | self.directory.folders = data::folders(&self.store, &self.favorite_ids); |
| 509 | self.reapply_section_changes(); |
| 510 | self.directory.section_settings = teams_core::stored_section_settings(&self.store); |
| 511 | self.adopt_folder_expanded(); |
| 512 | self.directory.pinned_channels = data::pinned_channels(&self.store); |
| 513 | if self.directory.pinned_channels.is_empty() { |
| 514 | let known: HashSet<&str> = self |
| 515 | .sidebar |
| 516 | .teams |
| 517 | .iter() |
| 518 | .flat_map(|team| team.channels.iter().map(|channel| channel.id.as_str())) |
| 519 | .collect(); |
| 520 | self.directory.pinned_channels = self |
| 521 | .favorite_ids |
| 522 | .iter() |
| 523 | .filter(|id| known.contains(id.as_str())) |
| 524 | .cloned() |
| 525 | .collect(); |
| 526 | } |
| 527 | self.resolve_pending_avatars(); |
| 528 | self.sync_presence(); |
| 529 | self.directory.unread_counts = match &self.engine { |
| 530 | Some(engine) => data::unread_counts(engine, &self.sidebar.chats), |
| 531 | None => Default::default(), |
| 532 | }; |
| 533 | } |
| 534 | |
| 535 | pub fn request_avatars(&mut self, user_ids: Vec<String>, cx: &mut Context<Self>) { |
| 536 | let wanted = self.directory.missing_avatars(user_ids); |
| 537 | if wanted.is_empty() { |
| 538 | return; |
| 539 | } |
| 540 | let mut changed = false; |
| 541 | let mut uncached = Vec::new(); |
| 542 | for user_id in wanted { |
| 543 | match data::cached_avatar(&self.store, &user_id) { |
| 544 | Some(cached) => { |
| 545 | self.directory.set_avatar(&user_id, cached); |
| 546 | changed = true; |
| 547 | } |
| 548 | None => uncached.push(user_id), |
| 549 | } |
| 550 | } |
| 551 | if let Some(engine) = self.engine.clone().filter(|_| !uncached.is_empty()) { |
| 552 | self.directory.mark_avatars_pending(&uncached); |
| 553 | let done = data::fetch_avatars(&engine, uncached); |
| 554 | self.on_done(done, cx, |state, cx| { |
| 555 | state.resolve_pending_avatars(); |
| 556 | cx.emit(AppEvent::Directory); |
| 557 | cx.notify(); |
| 558 | }); |
| 559 | changed = true; |
| 560 | } |
| 561 | if changed { |
| 562 | cx.emit(AppEvent::Directory); |
| 563 | cx.notify(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | pub fn request_images(&mut self, images: Vec<ImageRef>, cx: &mut Context<Self>) { |
| 568 | let Some(engine) = self.engine.clone() else { |
| 569 | return; |
| 570 | }; |
| 571 | let wanted = self |
| 572 | .directory |
| 573 | .images_to_request(images.iter().map(|image| image.url.clone())); |
| 574 | let mut changed = false; |
| 575 | for image in images |
| 576 | .into_iter() |
| 577 | .filter(|image| wanted.contains(&image.url)) |
| 578 | { |
| 579 | match engine.image_path(&image) { |
| 580 | Some(path) => { |
| 581 | self.directory.set_image(&image.url, &path); |
| 582 | changed = true; |
| 583 | } |
| 584 | None => { |
| 585 | let done = data::fetch_image(&engine, image.clone()); |
| 586 | cx.spawn(async move |this, cx| { |
| 587 | if !matches!(done.await, Ok(Ok(()))) { |
| 588 | this.update(cx, |state, cx| { |
| 589 | state.directory.mark_image_failed(&image.url); |
| 590 | cx.notify(); |
| 591 | }) |
| 592 | .ok(); |
| 593 | } |
| 594 | }) |
| 595 | .detach(); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | if changed { |
| 600 | cx.emit(AppEvent::Images(wanted)); |
| 601 | cx.notify(); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | pub fn jump_to_message( |
| 606 | &mut self, |
| 607 | selection: Selection, |
| 608 | message_id: String, |
| 609 | cx: &mut Context<Self>, |
| 610 | ) { |
| 611 | self.pending_jump = Some((selection.conversation_id().to_owned(), message_id)); |
| 612 | if self.selection.as_ref() == Some(&selection) { |
| 613 | cx.emit(AppEvent::Jump); |
| 614 | } else { |
| 615 | self.select(selection, cx); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | fn on_done( |
| 620 | &self, |
| 621 | done: data::Done, |
| 622 | cx: &mut Context<Self>, |
| 623 | then: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 624 | ) { |
| 625 | cx.spawn(async move |this, cx| { |
| 626 | let _ = done.await; |
| 627 | this.update(cx, |state, cx| then(state, cx)).ok(); |
| 628 | }) |
| 629 | .detach(); |
| 630 | } |
| 631 | |
| 632 | fn resolve_pending_avatars(&mut self) { |
| 633 | for user_id in self.directory.pending_avatars() { |
| 634 | if let Some(cached) = data::cached_avatar(&self.store, &user_id) { |
| 635 | self.directory.set_avatar(&user_id, cached); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | fn sync_presence(&mut self) -> Vec<String> { |
| 641 | let Some(engine) = self.engine.clone() else { |
| 642 | return Vec::new(); |
| 643 | }; |
| 644 | self.directory |
| 645 | .requested_presence_ids() |
| 646 | .into_iter() |
| 647 | .filter(|user_id| { |
| 648 | let Some(kind) = data::presence_kind(&engine, user_id) else { |
| 649 | return false; |
| 650 | }; |
| 651 | let activity_changed = self |
| 652 | .directory |
| 653 | .set_activity(user_id, data::presence_activity(&engine, user_id)); |
| 654 | self.directory.set_presence(user_id, kind) || activity_changed |
| 655 | }) |
| 656 | .collect() |
| 657 | } |
| 658 | |
| 659 | pub fn request_presence(&mut self, user_ids: Vec<String>, cx: &mut Context<Self>) { |
| 660 | let now = Instant::now(); |
| 661 | let pushed = self.live == LiveState::Live; |
| 662 | let wanted = self.directory.stale_presence(user_ids, now, pushed); |
| 663 | if wanted.is_empty() { |
| 664 | return; |
| 665 | } |
| 666 | self.directory.mark_presence_pending(&wanted); |
| 667 | if let Some(engine) = self.engine.clone() { |
| 668 | self.directory.mark_presence_requested(&wanted, now); |
| 669 | drop(data::watch_presence(&engine, wanted.clone())); |
| 670 | let done = data::refresh_presence(&engine, wanted.clone()); |
| 671 | self.on_done(done, cx, move |state, cx| { |
| 672 | state.sync_presence(); |
| 673 | state.directory.save_presence(&state.store, &wanted); |
| 674 | state.directory.settle_presence(&wanted); |
| 675 | cx.emit(AppEvent::Directory); |
| 676 | cx.notify(); |
| 677 | }); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | pub fn move_chat( |
| 682 | &mut self, |
| 683 | conversation_id: &str, |
| 684 | folder_id: Option<&str>, |
| 685 | cx: &mut Context<Self>, |
| 686 | ) { |
| 687 | if self.mode.read_only { |
| 688 | return; |
| 689 | } |
| 690 | let previous_folder = self |
| 691 | .directory |
| 692 | .folder_of(conversation_id) |
| 693 | .map(|folder| folder.id.clone()); |
| 694 | self.directory.assign(conversation_id, folder_id); |
| 695 | if let Some(engine) = self.engine.clone() { |
| 696 | let previous = previous_folder.clone(); |
| 697 | let done = match (folder_id, previous.as_deref()) { |
| 698 | (Some(folder_id), _) => { |
| 699 | Some(data::move_to_folder(&engine, conversation_id, folder_id)) |
| 700 | } |
| 701 | (None, Some(previous)) => { |
| 702 | Some(data::remove_from_folder(&engine, conversation_id, previous)) |
| 703 | } |
| 704 | (None, None) => None, |
| 705 | }; |
| 706 | if let Some(done) = done { |
| 707 | self.on_done(done, cx, |state, cx| { |
| 708 | state.reload_directory(); |
| 709 | cx.emit(AppEvent::Sidebar); |
| 710 | cx.notify(); |
| 711 | }); |
| 712 | } |
| 713 | } |
| 714 | cx.emit(AppEvent::Sidebar); |
| 715 | cx.notify(); |
| 716 | } |
| 717 | |
| 718 | pub fn mark_chat_read(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 719 | if self.keeps_unread(conversation_id) { |
| 720 | self.keep_unread = None; |
| 721 | } |
| 722 | if self.mode.demo { |
| 723 | if let Some(chat) = self |
| 724 | .sidebar |
| 725 | .chats |
| 726 | .iter() |
| 727 | .find(|chat| chat.id == conversation_id) |
| 728 | { |
| 729 | let mut read = chat.clone(); |
| 730 | read.unread = false; |
| 731 | let _ = self.store.upsert_chats(&[read]); |
| 732 | self.reload_sidebar(cx); |
| 733 | } |
| 734 | return; |
| 735 | } |
| 736 | if self.mode.read_only { |
| 737 | return; |
| 738 | } |
| 739 | if let Some(engine) = self.engine.clone() { |
| 740 | let _ = self |
| 741 | .store |
| 742 | .mark_chat_read(conversation_id, chrono::Utc::now()); |
| 743 | self.reload_sidebar(cx); |
| 744 | let conversation_id = conversation_id.to_owned(); |
| 745 | drop(crate::runtime::spawn(async move { |
| 746 | engine.mark_read(&conversation_id).await |
| 747 | })); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | pub fn mark_channel_read(&mut self, channel_id: &str, cx: &mut Context<Self>) { |
| 752 | if self.mode.read_only { |
| 753 | return; |
| 754 | } |
| 755 | let _ = self.store.mark_channel_read(channel_id); |
| 756 | self.reload_sidebar(cx); |
| 757 | if self.mode.demo { |
| 758 | return; |
| 759 | } |
| 760 | if let Some(engine) = self.engine.clone() { |
| 761 | let channel_id = channel_id.to_owned(); |
| 762 | drop(crate::runtime::spawn(async move { |
| 763 | engine.mark_read(&channel_id).await |
| 764 | })); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | pub fn start_new_chat(&mut self, cx: &mut Context<Self>) { |
| 769 | self.new_chat = true; |
| 770 | self.show_call(false, cx); |
| 771 | cx.emit(AppEvent::Selection); |
| 772 | cx.notify(); |
| 773 | } |
| 774 | |
| 775 | pub fn close_new_chat(&mut self, cx: &mut Context<Self>) { |
| 776 | if !self.new_chat { |
| 777 | return; |
| 778 | } |
| 779 | self.new_chat = false; |
| 780 | cx.emit(AppEvent::Selection); |
| 781 | cx.notify(); |
| 782 | } |
| 783 | |
| 784 | pub fn select(&mut self, selection: Selection, cx: &mut Context<Self>) { |
| 785 | self.show_call(false, cx); |
| 786 | self.cancel_prejoin(cx); |
| 787 | self.call_end = None; |
| 788 | let was_drafting = std::mem::take(&mut self.new_chat); |
| 789 | if !was_drafting && self.selection.as_ref() == Some(&selection) { |
| 790 | return; |
| 791 | } |
| 792 | let conversation_id = selection.conversation_id().to_owned(); |
| 793 | self.selection = Some(selection); |
| 794 | self.keep_unread = None; |
| 795 | cx.emit(AppEvent::Selection); |
| 796 | cx.notify(); |
| 797 | self.refresh_live_meeting(&conversation_id, cx); |
| 798 | self.refresh_language_stamps(&conversation_id, cx); |
| 799 | } |
| 800 | |
| 801 | fn apply_typing(&mut self, event: TypingEvent, cx: &mut Context<Self>) { |
| 802 | let is_me = self |
| 803 | .directory |
| 804 | .me |
| 805 | .as_ref() |
| 806 | .is_some_and(|me| me.user_id == event.user_id); |
| 807 | if is_me { |
| 808 | return; |
| 809 | } |
| 810 | if event.active { |
| 811 | let display_name = match event.display_name.trim() { |
| 812 | "" => crate::people::resolve_names(self, std::slice::from_ref(&event.user_id)) |
| 813 | .remove(&event.user_id) |
| 814 | .unwrap_or_default(), |
| 815 | _ => event.display_name.clone(), |
| 816 | }; |
| 817 | self.typing.start( |
| 818 | &event.conversation_id, |
| 819 | &event.user_id, |
| 820 | &display_name, |
| 821 | Instant::now(), |
| 822 | event.received_at, |
| 823 | ); |
| 824 | self.request_avatars(vec![event.user_id.clone()], cx); |
| 825 | self.schedule_typing_expiry(cx); |
| 826 | } else { |
| 827 | self.typing.clear(&event.conversation_id, &event.user_id); |
| 828 | } |
| 829 | cx.emit(AppEvent::Typing); |
| 830 | cx.notify(); |
| 831 | } |
| 832 | |
| 833 | fn clear_typists_who_sent(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 834 | if !self.typing.is_active(conversation_id) { |
| 835 | return; |
| 836 | } |
| 837 | let Ok(recent) = self |
| 838 | .store |
| 839 | .messages(conversation_id, None, RECENT_MESSAGES_FOR_TYPING) |
| 840 | else { |
| 841 | return; |
| 842 | }; |
| 843 | let mut changed = false; |
| 844 | for message in recent { |
| 845 | if let Some(sender_id) = message.sender_id.as_deref() { |
| 846 | changed |= self |
| 847 | .typing |
| 848 | .message_from(conversation_id, sender_id, message.created_at); |
| 849 | } |
| 850 | } |
| 851 | if changed { |
| 852 | cx.emit(AppEvent::Typing); |
| 853 | cx.notify(); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | fn schedule_typing_expiry(&mut self, cx: &mut Context<Self>) { |
| 858 | if self.typing_timer_running { |
| 859 | return; |
| 860 | } |
| 861 | self.typing_timer_running = true; |
| 862 | cx.spawn(async move |this, cx| { |
| 863 | loop { |
| 864 | let next = this.update(cx, |state, _| { |
| 865 | let next = state.typing.next_expiry(); |
| 866 | state.typing_timer_running = next.is_some(); |
| 867 | next |
| 868 | }); |
| 869 | let Ok(Some(next)) = next else { return }; |
| 870 | cx.background_executor() |
| 871 | .timer(next.saturating_duration_since(Instant::now())) |
| 872 | .await; |
| 873 | let alive = this.update(cx, |state, cx| { |
| 874 | if state.typing.expire(Instant::now()) { |
| 875 | cx.emit(AppEvent::Typing); |
| 876 | cx.notify(); |
| 877 | } |
| 878 | }); |
| 879 | if alive.is_err() { |
| 880 | return; |
| 881 | } |
| 882 | } |
| 883 | }) |
| 884 | .detach(); |
| 885 | } |
| 886 | |
| 887 | pub fn reload_sidebar(&mut self, cx: &mut Context<Self>) { |
| 888 | if let Ok(sidebar) = self.store.sidebar() { |
| 889 | self.sidebar = sidebar; |
| 890 | self.apply_bot_titles(); |
| 891 | } |
| 892 | self.calls_list.invalidate(); |
| 893 | if !self.mode.demo { |
| 894 | self.reload_directory(); |
| 895 | self.load_own_time_zone(cx); |
| 896 | } |
| 897 | cx.emit(AppEvent::Sidebar); |
| 898 | cx.notify(); |
| 899 | } |
| 900 | |
| 901 | pub fn apply(&mut self, event: BackendEvent, cx: &mut Context<Self>) { |
| 902 | match event { |
| 903 | BackendEvent::Connection(connection) => { |
| 904 | self.connection = connection; |
| 905 | self.refresh_connection_banner(cx); |
| 906 | cx.emit(AppEvent::Status); |
| 907 | } |
| 908 | BackendEvent::LiveRetry(retry) => self.live_retry = Some(retry), |
| 909 | BackendEvent::Calls(launcher) => { |
| 910 | launcher.set_default_camera(self.call_camera.clone()); |
| 911 | self.call_launcher = Some(launcher); |
| 912 | self.load_own_time_zone(cx); |
| 913 | } |
| 914 | BackendEvent::Calendar(events) => self.apply_calendar(events, cx), |
| 915 | BackendEvent::CalendarNudge(nudge) => self.calendar_nudge = Some(nudge), |
| 916 | BackendEvent::Ring(event) => self.on_engine_event(event, cx), |
| 917 | BackendEvent::ThreadChanged(conversation_id) => { |
| 918 | self.on_thread_changed(&conversation_id, cx) |
| 919 | } |
| 920 | BackendEvent::Engine(engine) => { |
| 921 | crate::agent_api::engine_ready(engine.clone(), cx); |
| 922 | self.engine = Some(engine); |
| 923 | self.resend_outbox(cx); |
| 924 | self.refresh_scheduled(cx); |
| 925 | self.refresh_translation_settings(cx); |
| 926 | self.refresh_recording_policy(cx); |
| 927 | self.refresh_sound_settings(cx); |
| 928 | let waiting = self.directory.waiting_presence_ids(); |
| 929 | self.request_presence(waiting, cx); |
| 930 | self.refresh_own_status(cx); |
| 931 | cx.emit(AppEvent::Status); |
| 932 | } |
| 933 | BackendEvent::Core(CoreEvent::SidebarChanged) => self.reload_sidebar_after_sync(cx), |
| 934 | BackendEvent::Core( |
| 935 | CoreEvent::MessagesChanged { conversation_id } |
| 936 | | CoreEvent::ReceiptsChanged { conversation_id }, |
| 937 | ) => { |
| 938 | self.clear_typists_who_sent(&conversation_id, cx); |
| 939 | self.refresh_language_stamps(&conversation_id, cx); |
| 940 | cx.emit(AppEvent::Messages(conversation_id)); |
| 941 | } |
| 942 | BackendEvent::Core(CoreEvent::MessagesStoredLate { |
| 943 | conversation_id, |
| 944 | message_ids, |
| 945 | }) => self.late_arrivals.insert(&conversation_id, message_ids), |
| 946 | BackendEvent::Core(CoreEvent::PinsChanged { conversation_id }) => { |
| 947 | self.refresh_pins(&conversation_id, cx); |
| 948 | } |
| 949 | BackendEvent::Core(CoreEvent::ImagesChanged { keys }) => { |
| 950 | if let Some(engine) = &self.engine { |
| 951 | for key in &keys { |
| 952 | let image = ImageRef { |
| 953 | id: String::new(), |
| 954 | url: key.clone(), |
| 955 | width: None, |
| 956 | height: None, |
| 957 | alt: None, |
| 958 | }; |
| 959 | if let Some(path) = engine.image_path(&image) { |
| 960 | self.directory.set_image(key, &path); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | cx.emit(AppEvent::Images(keys)); |
| 965 | } |
| 966 | BackendEvent::Core(CoreEvent::PresenceChanged) => { |
| 967 | let changed = self.sync_presence(); |
| 968 | self.directory.save_presence(&self.store, &changed); |
| 969 | cx.emit(AppEvent::Directory); |
| 970 | cx.notify(); |
| 971 | } |
| 972 | BackendEvent::Core(CoreEvent::PresenceStatusChanged(status)) => { |
| 973 | self.apply_own_status(status, cx); |
| 974 | self.reload_directory(); |
| 975 | } |
| 976 | BackendEvent::Core(_) => { |
| 977 | if !self.mode.demo { |
| 978 | self.reload_directory(); |
| 979 | } |
| 980 | cx.emit(AppEvent::Directory); |
| 981 | } |
| 982 | BackendEvent::Typing(event) => self.apply_typing(event, cx), |
| 983 | BackendEvent::Live(live) => { |
| 984 | self.live = live; |
| 985 | self.refresh_connection_banner(cx); |
| 986 | cx.emit(AppEvent::Status); |
| 987 | } |
| 988 | BackendEvent::Favorites(ids) => { |
| 989 | self.favorite_ids = ids; |
| 990 | if !self.mode.demo { |
| 991 | self.reload_directory(); |
| 992 | } |
| 993 | cx.emit(AppEvent::Sidebar); |
| 994 | } |
| 995 | BackendEvent::Synced(time) => { |
| 996 | if self.last_sync.is_none() { |
| 997 | self.refresh_saved(cx); |
| 998 | } |
| 999 | self.last_sync = Some(time); |
| 1000 | cx.emit(AppEvent::Status); |
| 1001 | } |
| 1002 | } |
| 1003 | cx.notify(); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | #[cfg(test)] |
| 1008 | mod tests { |
| 1009 | use std::sync::Arc; |
| 1010 | |
| 1011 | use chatsvc::TypingEvent; |
| 1012 | use chrono::{Duration, Utc}; |
| 1013 | use gpui_kit::{AppContext as _, TestAppContext}; |
| 1014 | use store::{ |
| 1015 | ChannelRecord, ChatRecord, MemberRecord, MessageRecord, SidebarTeam, Store, TeamRecord, |
| 1016 | }; |
| 1017 | use teams_core::CoreEvent; |
| 1018 | |
| 1019 | use super::{AppState, Mode, Selection, chat_title, selection_for, selection_title}; |
| 1020 | use crate::backend::BackendEvent; |
| 1021 | use crate::data::Person; |
| 1022 | use store::Sidebar; |
| 1023 | |
| 1024 | fn chat(id: &str, title: &str, unread: bool) -> ChatRecord { |
| 1025 | ChatRecord { |
| 1026 | id: id.into(), |
| 1027 | kind: "group".into(), |
| 1028 | title: title.into(), |
| 1029 | member_summary: "Member".into(), |
| 1030 | last_message_at: None, |
| 1031 | last_read_at: None, |
| 1032 | unread, |
| 1033 | ..Default::default() |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | #[test] |
| 1038 | fn selection_kind_follows_sidebar() { |
| 1039 | assert_eq!( |
| 1040 | selection_for(&sidebar(), "c1"), |
| 1041 | Some(Selection::Chat("c1".into())) |
| 1042 | ); |
| 1043 | assert_eq!( |
| 1044 | selection_for(&sidebar(), "ch"), |
| 1045 | Some(Selection::Channel("ch".into())) |
| 1046 | ); |
| 1047 | assert_eq!(selection_for(&sidebar(), "x"), None); |
| 1048 | } |
| 1049 | |
| 1050 | fn sidebar() -> Sidebar { |
| 1051 | Sidebar { |
| 1052 | chats: vec![chat("c1", "First", true), chat("c2", "", false)], |
| 1053 | teams: vec![SidebarTeam { |
| 1054 | team: TeamRecord { |
| 1055 | id: "t".into(), |
| 1056 | name: "Team".into(), |
| 1057 | }, |
| 1058 | channels: vec![ChannelRecord { |
| 1059 | id: "ch".into(), |
| 1060 | team_id: "t".into(), |
| 1061 | name: "General".into(), |
| 1062 | membership_type: None, |
| 1063 | last_message_at: None, |
| 1064 | unread: false, |
| 1065 | }], |
| 1066 | hidden: false, |
| 1067 | hidden_channel_ids: Vec::new(), |
| 1068 | notifications: Default::default(), |
| 1069 | }], |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | #[test] |
| 1074 | fn empty_chat_title_falls_back_to_members() { |
| 1075 | assert_eq!(chat_title(&sidebar().chats[1]), "Member"); |
| 1076 | } |
| 1077 | |
| 1078 | #[test] |
| 1079 | fn selection_titles() { |
| 1080 | assert_eq!( |
| 1081 | selection_title(&sidebar(), &Selection::Channel("ch".into())), |
| 1082 | "Team / General" |
| 1083 | ); |
| 1084 | assert_eq!( |
| 1085 | selection_title(&sidebar(), &Selection::Chat("c1".into())), |
| 1086 | "First" |
| 1087 | ); |
| 1088 | } |
| 1089 | |
| 1090 | fn typing_event(user_id: &str, active: bool) -> BackendEvent { |
| 1091 | BackendEvent::Typing(TypingEvent { |
| 1092 | conversation_id: "c1".into(), |
| 1093 | user_id: user_id.into(), |
| 1094 | display_name: format!("Name {user_id}"), |
| 1095 | active, |
| 1096 | received_at: Utc::now(), |
| 1097 | }) |
| 1098 | } |
| 1099 | |
| 1100 | #[gpui_kit::test] |
| 1101 | fn toggle_collapsed_ignores_pending_sections(cx: &mut TestAppContext) { |
| 1102 | cx.update(gpui_kit::init); |
| 1103 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1104 | let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1105 | cx.update(|cx| { |
| 1106 | app.update(cx, |state, cx| { |
| 1107 | state.toggle_collapsed("pending-section:1", cx); |
| 1108 | assert!(state.collapsed.is_empty()); |
| 1109 | }) |
| 1110 | }); |
| 1111 | } |
| 1112 | |
| 1113 | #[gpui_kit::test] |
| 1114 | fn typing_events_track_other_users_and_ignore_me(cx: &mut TestAppContext) { |
| 1115 | cx.update(gpui_kit::init); |
| 1116 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1117 | let app = cx.update(|cx| { |
| 1118 | cx.new(|_| { |
| 1119 | let mut state = AppState::new(store.clone(), Mode::default()); |
| 1120 | state.directory.me = Some(Person { |
| 1121 | user_id: "me".into(), |
| 1122 | display_name: "Me".into(), |
| 1123 | }); |
| 1124 | state |
| 1125 | }) |
| 1126 | }); |
| 1127 | let names = |cx: &mut TestAppContext| cx.update(|cx| app.read(cx).typing.names("c1")); |
| 1128 | cx.update(|cx| { |
| 1129 | app.update(cx, |state, cx| { |
| 1130 | state.apply(typing_event("me", true), cx); |
| 1131 | state.apply(typing_event("ada", true), cx); |
| 1132 | }) |
| 1133 | }); |
| 1134 | assert_eq!(names(cx), vec!["Name ada".to_owned()]); |
| 1135 | |
| 1136 | let message = MessageRecord { |
| 1137 | conversation_id: "c1".into(), |
| 1138 | message_id: "m1".into(), |
| 1139 | sender_id: Some("ada".into()), |
| 1140 | created_at: Utc::now() + Duration::seconds(1), |
| 1141 | ..Default::default() |
| 1142 | }; |
| 1143 | store.upsert_messages(&[message]).unwrap(); |
| 1144 | cx.update(|cx| { |
| 1145 | app.update(cx, |state, cx| { |
| 1146 | state.apply( |
| 1147 | BackendEvent::Core(CoreEvent::MessagesChanged { |
| 1148 | conversation_id: "c1".into(), |
| 1149 | }), |
| 1150 | cx, |
| 1151 | ) |
| 1152 | }) |
| 1153 | }); |
| 1154 | assert!(names(cx).is_empty()); |
| 1155 | |
| 1156 | cx.update(|cx| { |
| 1157 | app.update(cx, |state, cx| { |
| 1158 | state.apply(typing_event("ada", true), cx); |
| 1159 | state.apply(typing_event("ada", false), cx); |
| 1160 | }) |
| 1161 | }); |
| 1162 | assert!(names(cx).is_empty()); |
| 1163 | } |
| 1164 | |
| 1165 | #[gpui_kit::test] |
| 1166 | fn typing_without_a_display_name_uses_the_chat_member_name(cx: &mut TestAppContext) { |
| 1167 | cx.update(gpui_kit::init); |
| 1168 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1169 | let app = cx.update(|cx| { |
| 1170 | cx.new(|_| { |
| 1171 | let mut state = AppState::new(store.clone(), Mode::default()); |
| 1172 | let mut member_chat = chat("c1", "First", false); |
| 1173 | member_chat.members = vec![MemberRecord { |
| 1174 | user_id: Some("ada".into()), |
| 1175 | display_name: "Ada Lovelace".into(), |
| 1176 | }]; |
| 1177 | state.sidebar.chats = vec![member_chat]; |
| 1178 | state |
| 1179 | }) |
| 1180 | }); |
| 1181 | cx.update(|cx| { |
| 1182 | app.update(cx, |state, cx| { |
| 1183 | state.apply( |
| 1184 | BackendEvent::Typing(TypingEvent { |
| 1185 | conversation_id: "c1".into(), |
| 1186 | user_id: "ada".into(), |
| 1187 | display_name: String::new(), |
| 1188 | active: true, |
| 1189 | received_at: Utc::now(), |
| 1190 | }), |
| 1191 | cx, |
| 1192 | ) |
| 1193 | }) |
| 1194 | }); |
| 1195 | assert_eq!( |
| 1196 | cx.update(|cx| app.read(cx).typing.names("c1")), |
| 1197 | vec!["Ada Lovelace".to_owned()] |
| 1198 | ); |
| 1199 | } |
| 1200 | } |