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