| 1 | use std::future::Future; |
| 2 | |
| 3 | use chrono::{DateTime, Duration, Local, Offset, Utc}; |
| 4 | use gpui_kit::*; |
| 5 | use store::ChatRecord; |
| 6 | |
| 7 | use crate::app_state::{AppState, Selection, chat_title}; |
| 8 | use crate::notice::{NoticeAction, short_error, truncated}; |
| 9 | use crate::sidebar_model::{SectionInput, build_sections, next_chat_id}; |
| 10 | |
| 11 | pub const TITLE_LIMIT: usize = 40; |
| 12 | |
| 13 | impl AppState { |
| 14 | pub fn keeps_unread(&self, chat_id: &str) -> bool { |
| 15 | self.keep_unread.as_deref() == Some(chat_id) |
| 16 | } |
| 17 | |
| 18 | pub fn mark_chat_unread(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 19 | self.mark_chat_unread_from(chat_id, None, cx); |
| 20 | } |
| 21 | |
| 22 | pub fn mark_chat_unread_from( |
| 23 | &mut self, |
| 24 | chat_id: &str, |
| 25 | message_created_at: Option<DateTime<Utc>>, |
| 26 | cx: &mut Context<Self>, |
| 27 | ) { |
| 28 | let Some(chat) = self.chat_record(chat_id) else { |
| 29 | return; |
| 30 | }; |
| 31 | let Some(last_message_at) = chat.last_message_at else { |
| 32 | return; |
| 33 | }; |
| 34 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 35 | return; |
| 36 | }; |
| 37 | let last_read_at = |
| 38 | message_created_at.unwrap_or(last_message_at) - Duration::milliseconds(1); |
| 39 | let _ = self.store.mark_chat_unread(chat_id, last_read_at); |
| 40 | if self.selection == Some(Selection::Chat(chat_id.to_owned())) { |
| 41 | self.keep_unread = Some(chat_id.to_owned()); |
| 42 | } |
| 43 | self.reload_sidebar(cx); |
| 44 | let Some(engine) = engine_or_demo else { |
| 45 | return; |
| 46 | }; |
| 47 | let owned_id = chat_id.to_owned(); |
| 48 | let revert_id = owned_id.clone(); |
| 49 | let restored_read_at = chat.last_read_at.unwrap_or(last_message_at); |
| 50 | self.run_chat_action( |
| 51 | "Mark as unread", |
| 52 | async move { |
| 53 | match message_created_at { |
| 54 | Some(created_at) => engine.mark_unread_from(&owned_id, created_at).await, |
| 55 | None => engine.mark_unread(&owned_id).await, |
| 56 | } |
| 57 | }, |
| 58 | move |state, cx| { |
| 59 | let _ = state.store.mark_chat_read(&revert_id, restored_read_at); |
| 60 | if state.keep_unread.as_deref() == Some(revert_id.as_str()) { |
| 61 | state.keep_unread = None; |
| 62 | } |
| 63 | state.reload_sidebar(cx); |
| 64 | }, |
| 65 | |_, _| {}, |
| 66 | cx, |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | pub fn set_chat_muted(&mut self, chat_id: &str, muted: bool, cx: &mut Context<Self>) { |
| 71 | if self.chat_record(chat_id).is_none() { |
| 72 | return; |
| 73 | } |
| 74 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 75 | return; |
| 76 | }; |
| 77 | let _ = self.store.set_chat_muted(chat_id, muted); |
| 78 | self.reload_sidebar(cx); |
| 79 | let Some(engine) = engine_or_demo else { |
| 80 | return; |
| 81 | }; |
| 82 | let owned_id = chat_id.to_owned(); |
| 83 | let revert_id = owned_id.clone(); |
| 84 | self.run_chat_action( |
| 85 | if muted { "Mute" } else { "Unmute" }, |
| 86 | async move { engine.set_chat_muted(&owned_id, muted).await }, |
| 87 | move |state, cx| { |
| 88 | let _ = state.store.set_chat_muted(&revert_id, !muted); |
| 89 | state.reload_sidebar(cx); |
| 90 | }, |
| 91 | |_, _| {}, |
| 92 | cx, |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | pub fn hide_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 97 | let Some(chat) = self.chat_record(chat_id) else { |
| 98 | return; |
| 99 | }; |
| 100 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 101 | return; |
| 102 | }; |
| 103 | self.remove_chat_locally(chat_id, cx); |
| 104 | let Some(engine) = engine_or_demo else { |
| 105 | return; |
| 106 | }; |
| 107 | let owned_id = chat_id.to_owned(); |
| 108 | let undo_id = owned_id.clone(); |
| 109 | let text = format!("\"{}\" hidden", truncated(&chat_title(&chat), TITLE_LIMIT)); |
| 110 | self.run_chat_action( |
| 111 | "Hide", |
| 112 | async move { engine.hide_chat(&owned_id).await }, |
| 113 | move |state, cx| state.restore_chat_locally(&chat, cx), |
| 114 | move |state, cx| { |
| 115 | let undo = |
| 116 | NoticeAction::new("Undo", move |state, cx| state.unhide_chat(&undo_id, cx)); |
| 117 | state.raise_notice(text, Some(undo), cx); |
| 118 | }, |
| 119 | cx, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | pub fn unhide_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 124 | let Some(engine) = self.chat_action_engine(cx).flatten() else { |
| 125 | return; |
| 126 | }; |
| 127 | let owned_id = chat_id.to_owned(); |
| 128 | self.run_chat_action( |
| 129 | "Undo", |
| 130 | async move { engine.unhide_chat(&owned_id).await }, |
| 131 | |_, _| {}, |
| 132 | |_, _| {}, |
| 133 | cx, |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | pub fn leave_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 138 | let Some(chat) = self.chat_record(chat_id) else { |
| 139 | return; |
| 140 | }; |
| 141 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 142 | return; |
| 143 | }; |
| 144 | self.remove_chat_locally(chat_id, cx); |
| 145 | let Some(engine) = engine_or_demo else { |
| 146 | return; |
| 147 | }; |
| 148 | let owned_id = chat_id.to_owned(); |
| 149 | self.run_chat_action( |
| 150 | "Leave chat", |
| 151 | async move { engine.leave_chat(&owned_id).await }, |
| 152 | move |state, cx| state.restore_chat_locally(&chat, cx), |
| 153 | |_, _| {}, |
| 154 | cx, |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | fn chat_record(&self, chat_id: &str) -> Option<ChatRecord> { |
| 159 | self.sidebar |
| 160 | .chats |
| 161 | .iter() |
| 162 | .find(|chat| chat.id == chat_id) |
| 163 | .cloned() |
| 164 | } |
| 165 | |
| 166 | /// `None` stops the action. `Some(None)` is demo mode, which only changes the local store. |
| 167 | pub(crate) fn chat_action_engine( |
| 168 | &mut self, |
| 169 | cx: &mut Context<Self>, |
| 170 | ) -> Option<Option<std::sync::Arc<crate::backend::Engine>>> { |
| 171 | if self.mode.read_only { |
| 172 | self.raise_notice("Read-only mode: chat not changed".to_owned(), None, cx); |
| 173 | return None; |
| 174 | } |
| 175 | match self.engine.clone() { |
| 176 | Some(engine) => Some(Some(engine)), |
| 177 | None if self.mode.demo => Some(None), |
| 178 | None => None, |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | fn remove_chat_locally(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 183 | let was_selected = self.selection == Some(Selection::Chat(chat_id.to_owned())); |
| 184 | let next = was_selected |
| 185 | .then(|| self.next_chat_after(chat_id)) |
| 186 | .flatten(); |
| 187 | let _ = self.store.remove_chats(&[chat_id.to_owned()]); |
| 188 | self.reload_sidebar(cx); |
| 189 | if was_selected { |
| 190 | match next { |
| 191 | Some(next_id) => self.select(Selection::Chat(next_id), cx), |
| 192 | None => { |
| 193 | self.selection = None; |
| 194 | self.keep_unread = None; |
| 195 | cx.emit(crate::app_state::AppEvent::Selection); |
| 196 | cx.notify(); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | fn restore_chat_locally(&mut self, chat: &ChatRecord, cx: &mut Context<Self>) { |
| 203 | let _ = self.store.upsert_chats(std::slice::from_ref(chat)); |
| 204 | let _ = self.store.set_chat_muted(&chat.id, chat.muted); |
| 205 | self.reload_sidebar(cx); |
| 206 | } |
| 207 | |
| 208 | fn next_chat_after(&self, chat_id: &str) -> Option<String> { |
| 209 | let input = SectionInput { |
| 210 | chats: &self.sidebar.chats, |
| 211 | directory: &self.directory, |
| 212 | collapsed: &self.collapsed, |
| 213 | expanded_lists: &self.expanded_lists, |
| 214 | typing: &self.typing, |
| 215 | now: Utc::now(), |
| 216 | offset: Local::now().offset().fix(), |
| 217 | }; |
| 218 | next_chat_id(&build_sections(&input), chat_id) |
| 219 | } |
| 220 | |
| 221 | pub(crate) fn run_chat_action( |
| 222 | &mut self, |
| 223 | label: &'static str, |
| 224 | call: impl Future<Output = teams_core::Result<()>> + Send + 'static, |
| 225 | revert: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 226 | on_success: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 227 | cx: &mut Context<Self>, |
| 228 | ) { |
| 229 | let receiver = crate::runtime::spawn(call); |
| 230 | cx.spawn(async move |this, cx| { |
| 231 | let failure = match receiver.await { |
| 232 | Ok(Ok(())) => None, |
| 233 | Ok(Err(error)) => Some(short_error(&error)), |
| 234 | Err(_) => Some("cancelled".to_owned()), |
| 235 | }; |
| 236 | this.update(cx, |state, cx| match failure { |
| 237 | None => on_success(state, cx), |
| 238 | Some(reason) => { |
| 239 | revert(state, cx); |
| 240 | state.raise_notice(format!("{label} failed: {reason}"), None, cx); |
| 241 | } |
| 242 | }) |
| 243 | .ok(); |
| 244 | }) |
| 245 | .detach(); |
| 246 | } |
| 247 | } |