use std::collections::{HashMap, HashSet}; use std::sync::Arc; use std::time::Instant; use chrono::{DateTime, Utc}; use gpui_kit::*; use store::{ChatRecord, Sidebar, Store}; use teams_core::{ChatApp, CoreEvent, ImageRef}; use crate::backend::{BackendEvent, ConnectionState, Engine, LiveState}; use crate::card_state::{CardState, TaskDialogState}; use crate::data::{self, Directory}; use crate::notice::Notice; use crate::notify; const COLLAPSED_META_KEY: &str = "ui.collapsed_sections"; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Selection { Chat(String), Channel(String), } impl Selection { pub fn conversation_id(&self) -> &str { match self { Selection::Chat(id) | Selection::Channel(id) => id, } } } pub enum AppEvent { Sidebar, Selection, Messages(String), Images(Vec), Jump, Status, Directory, Cards(String), TaskDialog, } #[derive(Debug, Clone, Copy, Default)] pub struct Mode { pub demo: bool, pub read_only: bool, pub demo_sync: Option, } pub struct AppState { pub store: Arc, pub engine: Option>, pub sidebar: Sidebar, pub favorite_ids: Vec, pub selection: Option, pub new_chat: bool, pub connection: ConnectionState, pub live: LiveState, pub last_sync: Option>, pub mode: Mode, pub directory: Directory, pub collapsed: HashSet, pub followed_channels: HashSet, pub start_on_channels: bool, pub pending_jump: Option<(String, String)>, pub cards: CardState, pub task_dialog: Option, pub bot_apps: HashMap>, pub bot_apps_pending: HashSet, pub notice: Option, pub notice_count: u64, pub keep_unread: Option, } pub struct AppHandle(pub Entity); impl Global for AppHandle {} impl EventEmitter for AppState {} pub fn chat_title(chat: &ChatRecord) -> String { if !chat.title.trim().is_empty() { chat.title.clone() } else if !chat.member_summary.trim().is_empty() { chat.member_summary.clone() } else { "Chat".to_owned() } } pub fn selection_title(sidebar: &Sidebar, selection: &Selection) -> String { match selection { Selection::Chat(id) => sidebar .chats .iter() .find(|chat| &chat.id == id) .map(chat_title) .unwrap_or_else(|| "Chat".to_owned()), Selection::Channel(id) => sidebar .teams .iter() .find_map(|team| { team.channels .iter() .find(|channel| &channel.id == id) .map(|channel| format!("{} / {}", team.team.name, channel.name)) }) .unwrap_or_else(|| "Channel".to_owned()), } } impl AppState { pub fn new(store: Arc, mode: Mode) -> Self { let sidebar = store.sidebar().unwrap_or_default(); let mut state = AppState { store, engine: None, sidebar, favorite_ids: Vec::new(), selection: None, new_chat: false, connection: if mode.demo { ConnectionState::Online } else { ConnectionState::Connecting }, live: LiveState::Off, last_sync: None, mode, directory: Directory::default(), collapsed: HashSet::new(), followed_channels: HashSet::new(), start_on_channels: false, pending_jump: None, cards: CardState::default(), task_dialog: None, bot_apps: HashMap::new(), bot_apps_pending: HashSet::new(), notice: None, notice_count: 0, keep_unread: None, }; state.collapsed = state.load_collapsed(); state.followed_channels = notify::load_followed_channels(&state.store); if !mode.demo { state.directory.load_cached_presence(&state.store); state.reload_directory(); } state } fn load_collapsed(&self) -> HashSet { self.store .meta(COLLAPSED_META_KEY) .ok() .flatten() .map(|value| { value .lines() .map(str::to_owned) .filter(|id| !id.is_empty()) .collect() }) .unwrap_or_default() } pub fn toggle_followed_channel(&mut self, channel_id: &str, cx: &mut Context) { if !self.followed_channels.remove(channel_id) { self.followed_channels.insert(channel_id.to_owned()); } notify::save_followed_channels(&self.store, &self.followed_channels); cx.notify(); } pub fn toggle_collapsed(&mut self, section_id: &str, cx: &mut Context) { if !self.collapsed.remove(section_id) { self.collapsed.insert(section_id.to_owned()); } let mut ids: Vec<&str> = self.collapsed.iter().map(String::as_str).collect(); ids.sort_unstable(); let _ = self.store.set_meta(COLLAPSED_META_KEY, &ids.join("\n")); cx.emit(AppEvent::Sidebar); cx.notify(); } pub fn reload_directory(&mut self) { self.directory.me = data::me(&self.store); self.directory.folders = data::folders(&self.store, &self.favorite_ids); self.directory.pinned_channels = data::pinned_channels(&self.store); if self.directory.pinned_channels.is_empty() { let known: HashSet<&str> = self .sidebar .teams .iter() .flat_map(|team| team.channels.iter().map(|channel| channel.id.as_str())) .collect(); self.directory.pinned_channels = self .favorite_ids .iter() .filter(|id| known.contains(id.as_str())) .cloned() .collect(); } self.resolve_pending_avatars(); self.sync_presence(); self.directory.unread_counts = match &self.engine { Some(engine) => data::unread_counts(engine, &self.sidebar.chats), None => Default::default(), }; } pub fn request_avatars(&mut self, user_ids: Vec, cx: &mut Context) { let wanted = self.directory.missing_avatars(user_ids); if wanted.is_empty() { return; } let mut changed = false; let mut uncached = Vec::new(); for user_id in wanted { match data::cached_avatar(&self.store, &user_id) { Some(cached) => { self.directory.set_avatar(&user_id, cached); changed = true; } None => uncached.push(user_id), } } if let Some(engine) = self.engine.clone().filter(|_| !uncached.is_empty()) { self.directory.mark_avatars_pending(&uncached); let done = data::fetch_avatars(&engine, uncached); self.on_done(done, cx, |state, cx| { state.resolve_pending_avatars(); cx.emit(AppEvent::Directory); cx.notify(); }); changed = true; } if changed { cx.emit(AppEvent::Directory); cx.notify(); } } pub fn request_images(&mut self, images: Vec, cx: &mut Context) { let Some(engine) = self.engine.clone() else { return; }; let wanted = self .directory .images_to_request(images.iter().map(|image| image.url.clone())); let mut changed = false; for image in images .into_iter() .filter(|image| wanted.contains(&image.url)) { match engine.image_path(&image) { Some(path) => { self.directory.set_image(&image.url, &path); changed = true; } None => { let done = data::fetch_image(&engine, image); self.on_done(done, cx, |_, _| {}); } } } if changed { cx.emit(AppEvent::Images(wanted)); cx.notify(); } } pub fn jump_to_message( &mut self, selection: Selection, message_id: String, cx: &mut Context, ) { self.pending_jump = Some((selection.conversation_id().to_owned(), message_id)); if self.selection.as_ref() == Some(&selection) { cx.emit(AppEvent::Jump); } else { self.select(selection, cx); } } fn on_done( &self, done: data::Done, cx: &mut Context, then: impl FnOnce(&mut Self, &mut Context) + 'static, ) { cx.spawn(async move |this, cx| { let _ = done.await; this.update(cx, |state, cx| then(state, cx)).ok(); }) .detach(); } fn resolve_pending_avatars(&mut self) { for user_id in self.directory.pending_avatars() { if let Some(cached) = data::cached_avatar(&self.store, &user_id) { self.directory.set_avatar(&user_id, cached); } } } fn sync_presence(&mut self) -> Vec { let Some(engine) = self.engine.clone() else { return Vec::new(); }; self.directory .requested_presence_ids() .into_iter() .filter(|user_id| { data::presence_kind(&engine, user_id) .is_some_and(|kind| self.directory.set_presence(user_id, kind)) }) .collect() } pub fn request_presence(&mut self, user_ids: Vec, cx: &mut Context) { let now = Instant::now(); let pushed = self.live == LiveState::Live; let wanted = self.directory.stale_presence(user_ids, now, pushed); if wanted.is_empty() { return; } self.directory.mark_presence_pending(&wanted); if let Some(engine) = self.engine.clone() { self.directory.mark_presence_requested(&wanted, now); drop(data::watch_presence(&engine, wanted.clone())); let done = data::refresh_presence(&engine, wanted.clone()); self.on_done(done, cx, move |state, cx| { state.sync_presence(); state.directory.save_presence(&state.store, &wanted); state.directory.settle_presence(&wanted); cx.emit(AppEvent::Directory); cx.notify(); }); } } pub fn move_chat( &mut self, conversation_id: &str, folder_id: Option<&str>, cx: &mut Context, ) { if self.mode.read_only { return; } let previous_folder = self .directory .folder_of(conversation_id) .map(|folder| folder.id.clone()); self.directory.assign(conversation_id, folder_id); if let Some(engine) = self.engine.clone() { let previous = previous_folder.clone(); let done = match (folder_id, previous.as_deref()) { (Some(folder_id), _) => { Some(data::move_to_folder(&engine, conversation_id, folder_id)) } (None, Some(previous)) => { Some(data::remove_from_folder(&engine, conversation_id, previous)) } (None, None) => None, }; if let Some(done) = done { self.on_done(done, cx, |state, cx| { state.reload_directory(); cx.emit(AppEvent::Sidebar); cx.notify(); }); } } cx.emit(AppEvent::Sidebar); cx.notify(); } pub fn pin_chat(&mut self, conversation_id: &str, cx: &mut Context) { let favorites = self.directory.favorites().map(|folder| folder.id.clone()); self.move_chat(conversation_id, favorites.as_deref(), cx); } pub fn mark_chat_read(&mut self, conversation_id: &str, cx: &mut Context) { if self.keeps_unread(conversation_id) { self.keep_unread = None; } if self.mode.demo { if let Some(chat) = self .sidebar .chats .iter() .find(|chat| chat.id == conversation_id) { let mut read = chat.clone(); read.unread = false; let _ = self.store.upsert_chats(&[read]); self.reload_sidebar(cx); } return; } if self.mode.read_only { return; } if let Some(engine) = self.engine.clone() { let _ = self .store .mark_chat_read(conversation_id, chrono::Utc::now()); self.reload_sidebar(cx); let conversation_id = conversation_id.to_owned(); drop(crate::runtime::spawn(async move { engine.mark_read(&conversation_id).await })); } } pub fn start_new_chat(&mut self, cx: &mut Context) { self.new_chat = true; cx.emit(AppEvent::Selection); cx.notify(); } pub fn close_new_chat(&mut self, cx: &mut Context) { if !self.new_chat { return; } self.new_chat = false; cx.emit(AppEvent::Selection); cx.notify(); } pub fn select(&mut self, selection: Selection, cx: &mut Context) { let was_drafting = std::mem::take(&mut self.new_chat); if !was_drafting && self.selection.as_ref() == Some(&selection) { return; } self.selection = Some(selection); self.keep_unread = None; cx.emit(AppEvent::Selection); cx.notify(); } pub fn reload_sidebar(&mut self, cx: &mut Context) { if let Ok(sidebar) = self.store.sidebar() { self.sidebar = sidebar; self.apply_bot_titles(); } if !self.mode.demo { self.reload_directory(); } cx.emit(AppEvent::Sidebar); cx.notify(); } pub fn apply(&mut self, event: BackendEvent, cx: &mut Context) { match event { BackendEvent::Connection(connection) => { self.connection = connection; cx.emit(AppEvent::Status); } BackendEvent::Engine(engine) => { self.engine = Some(engine); let waiting = self.directory.waiting_presence_ids(); self.request_presence(waiting, cx); cx.emit(AppEvent::Status); } BackendEvent::Core(CoreEvent::SidebarChanged) => self.reload_sidebar(cx), BackendEvent::Core( CoreEvent::MessagesChanged { conversation_id } | CoreEvent::ReceiptsChanged { conversation_id }, ) => { cx.emit(AppEvent::Messages(conversation_id)); } BackendEvent::Core(CoreEvent::ImagesChanged { keys }) => { if let Some(engine) = &self.engine { for key in &keys { let image = ImageRef { id: String::new(), url: key.clone(), width: None, height: None, }; if let Some(path) = engine.image_path(&image) { self.directory.set_image(key, &path); } } } cx.emit(AppEvent::Images(keys)); } BackendEvent::Core(CoreEvent::PresenceChanged) => { let changed = self.sync_presence(); self.directory.save_presence(&self.store, &changed); cx.emit(AppEvent::Directory); cx.notify(); } BackendEvent::Core(_) => { if !self.mode.demo { self.reload_directory(); } cx.emit(AppEvent::Directory); } BackendEvent::Live(live) => { self.live = live; cx.emit(AppEvent::Status); } BackendEvent::Favorites(ids) => { self.favorite_ids = ids; if !self.mode.demo { self.reload_directory(); } cx.emit(AppEvent::Sidebar); } BackendEvent::Synced(time) => { self.last_sync = Some(time); cx.emit(AppEvent::Status); } } cx.notify(); } } #[cfg(test)] mod tests { use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord}; use super::{Selection, chat_title, selection_title}; use store::Sidebar; fn chat(id: &str, title: &str, unread: bool) -> ChatRecord { ChatRecord { id: id.into(), kind: "group".into(), title: title.into(), member_summary: "Member".into(), last_message_at: None, last_read_at: None, unread, ..Default::default() } } fn sidebar() -> Sidebar { Sidebar { chats: vec![chat("c1", "First", true), chat("c2", "", false)], teams: vec![SidebarTeam { team: TeamRecord { id: "t".into(), name: "Team".into(), }, channels: vec![ChannelRecord { id: "ch".into(), team_id: "t".into(), name: "General".into(), membership_type: None, last_message_at: None, unread: false, }], hidden: false, hidden_channel_ids: Vec::new(), }], } } #[test] fn empty_chat_title_falls_back_to_members() { assert_eq!(chat_title(&sidebar().chats[1]), "Member"); } #[test] fn selection_titles() { assert_eq!( selection_title(&sidebar(), &Selection::Channel("ch".into())), "Team / General" ); assert_eq!( selection_title(&sidebar(), &Selection::Chat("c1".into())), "First" ); } }