| 1 | use gpui_kit::Context; |
| 2 | use store::Store; |
| 3 | use teams_core::{ |
| 4 | ChatAccountSettings, ChatNotificationMode, ChatSettingChange, ChatSettingsDocument, |
| 5 | DesktopNotifications, |
| 6 | }; |
| 7 | |
| 8 | use crate::app_state::{AppEvent, AppState}; |
| 9 | use crate::rows::Receipt; |
| 10 | use crate::runtime; |
| 11 | |
| 12 | const META_MARKDOWN_MODE: &str = "compose.markdown_mode"; |
| 13 | |
| 14 | pub fn stored_markdown_mode(store: &Store) -> bool { |
| 15 | store.meta(META_MARKDOWN_MODE).ok().flatten().as_deref() == Some("true") |
| 16 | } |
| 17 | |
| 18 | /// Teams shows no read state at all while the own read receipts setting is off. |
| 19 | pub fn shown_receipt(settings: &ChatAccountSettings, receipt: Receipt) -> Receipt { |
| 20 | if settings.read_receipts { |
| 21 | receipt |
| 22 | } else { |
| 23 | Receipt::Hidden |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl AppState { |
| 28 | pub fn account_chat_settings(&self) -> ChatAccountSettings { |
| 29 | self.chat_settings.settings() |
| 30 | } |
| 31 | |
| 32 | pub fn refresh_chat_settings(&mut self, cx: &mut Context<Self>) { |
| 33 | let Some(engine) = self.engine.clone() else { |
| 34 | return; |
| 35 | }; |
| 36 | let read = runtime::spawn(async move { engine.refresh_chat_settings().await }); |
| 37 | cx.spawn(async move |this, cx| { |
| 38 | let document = match read.await { |
| 39 | Ok(Ok(document)) => document, |
| 40 | Ok(Err(error)) => { |
| 41 | log::warn!("chat settings not read: {error}"); |
| 42 | return; |
| 43 | } |
| 44 | Err(_) => return, |
| 45 | }; |
| 46 | this.update(cx, |state, cx| state.apply_chat_settings(document, cx)) |
| 47 | .ok(); |
| 48 | }) |
| 49 | .detach(); |
| 50 | } |
| 51 | |
| 52 | pub fn save_chat_setting(&mut self, change: ChatSettingChange, cx: &mut Context<Self>) { |
| 53 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 54 | return; |
| 55 | }; |
| 56 | let previous = self.chat_settings.clone(); |
| 57 | let mut updated = previous.clone(); |
| 58 | updated.apply(change); |
| 59 | self.apply_chat_settings(updated, cx); |
| 60 | let Some(engine) = engine_or_demo else { |
| 61 | return; |
| 62 | }; |
| 63 | self.run_chat_action( |
| 64 | "Setting", |
| 65 | async move { engine.save_chat_setting(change).await.map(|_| ()) }, |
| 66 | move |state, cx| state.apply_chat_settings(previous, cx), |
| 67 | |_, _| {}, |
| 68 | cx, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | fn apply_chat_settings(&mut self, document: ChatSettingsDocument, cx: &mut Context<Self>) { |
| 73 | if self.chat_settings == document { |
| 74 | return; |
| 75 | } |
| 76 | let _ = teams_core::store_chat_settings(&self.store, &document); |
| 77 | self.chat_settings = document; |
| 78 | cx.emit(AppEvent::ChatSettings); |
| 79 | cx.notify(); |
| 80 | } |
| 81 | |
| 82 | pub fn set_markdown_mode(&mut self, enabled: bool, cx: &mut Context<Self>) { |
| 83 | self.markdown_mode = enabled; |
| 84 | let _ = self |
| 85 | .store |
| 86 | .set_meta(META_MARKDOWN_MODE, if enabled { "true" } else { "false" }); |
| 87 | cx.emit(AppEvent::ChatSettings); |
| 88 | cx.notify(); |
| 89 | } |
| 90 | |
| 91 | pub fn chat_desktop_notifications(&self, chat_id: &str) -> DesktopNotifications { |
| 92 | match &self.engine { |
| 93 | Some(engine) => engine.chat_desktop_notifications(chat_id), |
| 94 | None => self |
| 95 | .demo_chat_desktop |
| 96 | .get(chat_id) |
| 97 | .copied() |
| 98 | .unwrap_or_default(), |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn set_chat_notifications( |
| 103 | &mut self, |
| 104 | chat_id: &str, |
| 105 | mode: ChatNotificationMode, |
| 106 | desktop: DesktopNotifications, |
| 107 | cx: &mut Context<Self>, |
| 108 | ) { |
| 109 | let Some(previous_muted) = self.chat_record(chat_id).map(|chat| chat.muted) else { |
| 110 | return; |
| 111 | }; |
| 112 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 113 | return; |
| 114 | }; |
| 115 | let muted = mode == ChatNotificationMode::Mute; |
| 116 | let _ = self.store.set_chat_muted(chat_id, muted); |
| 117 | self.reload_sidebar(cx); |
| 118 | let Some(engine) = engine_or_demo else { |
| 119 | self.demo_chat_desktop.insert(chat_id.to_owned(), desktop); |
| 120 | return; |
| 121 | }; |
| 122 | let owned_id = chat_id.to_owned(); |
| 123 | let revert_id = owned_id.clone(); |
| 124 | self.run_chat_action( |
| 125 | "Chat notifications", |
| 126 | async move { |
| 127 | engine |
| 128 | .set_chat_notifications(&owned_id, mode, desktop) |
| 129 | .await |
| 130 | }, |
| 131 | move |state, cx| { |
| 132 | let _ = state.store.set_chat_muted(&revert_id, previous_muted); |
| 133 | state.reload_sidebar(cx); |
| 134 | }, |
| 135 | |_, _| {}, |
| 136 | cx, |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use super::*; |
| 144 | |
| 145 | #[test] |
| 146 | fn receipts_disappear_while_the_own_setting_is_off() { |
| 147 | let mut settings = ChatAccountSettings::default(); |
| 148 | assert_eq!(shown_receipt(&settings, Receipt::Read), Receipt::Read); |
| 149 | assert_eq!(shown_receipt(&settings, Receipt::Sent), Receipt::Sent); |
| 150 | settings.read_receipts = false; |
| 151 | assert_eq!(shown_receipt(&settings, Receipt::Read), Receipt::Hidden); |
| 152 | assert_eq!(shown_receipt(&settings, Receipt::Sent), Receipt::Hidden); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn markdown_mode_is_a_local_setting_that_starts_off() { |
| 157 | let store = Store::open_in_memory().unwrap(); |
| 158 | assert!(!stored_markdown_mode(&store)); |
| 159 | store.set_meta(META_MARKDOWN_MODE, "true").unwrap(); |
| 160 | assert!(stored_markdown_mode(&store)); |
| 161 | } |
| 162 | } |