BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / chat_settings_actions.rs

chat_settings_actions.rs

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