| 1 | use chatsvc::sound_settings::SoundSettings; |
| 2 | use gpui_kit::Context; |
| 3 | |
| 4 | use crate::app_state::{AppEvent, AppState}; |
| 5 | use crate::runtime; |
| 6 | |
| 7 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 8 | |
| 9 | pub fn user_mri(user_id: &str) -> String { |
| 10 | if user_id.starts_with("8:") { |
| 11 | user_id.to_owned() |
| 12 | } else { |
| 13 | format!("{ORGID_PREFIX}{user_id}") |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl AppState { |
| 18 | pub fn refresh_sound_settings(&mut self, cx: &mut Context<Self>) { |
| 19 | let Some(engine) = self.engine.clone() else { |
| 20 | return; |
| 21 | }; |
| 22 | let read = runtime::spawn(async move { engine.sound_settings().await }); |
| 23 | cx.spawn(async move |this, cx| { |
| 24 | let settings = match read.await { |
| 25 | Ok(Ok(settings)) => settings, |
| 26 | Ok(Err(error)) => { |
| 27 | log::warn!("sound settings not read: {error}"); |
| 28 | return; |
| 29 | } |
| 30 | Err(_) => return, |
| 31 | }; |
| 32 | this.update(cx, |state, cx| state.apply_account_sounds(settings, cx)) |
| 33 | .ok(); |
| 34 | }) |
| 35 | .detach(); |
| 36 | } |
| 37 | |
| 38 | pub fn save_sound_settings( |
| 39 | &mut self, |
| 40 | previous: SoundSettings, |
| 41 | updated: SoundSettings, |
| 42 | cx: &mut Context<Self>, |
| 43 | ) { |
| 44 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 45 | self.apply_account_sounds(previous, cx); |
| 46 | return; |
| 47 | }; |
| 48 | let Some(engine) = engine_or_demo else { |
| 49 | self.account_sounds = Some(updated); |
| 50 | return; |
| 51 | }; |
| 52 | let Some(user_mri) = self.directory.me.as_ref().map(|me| user_mri(&me.user_id)) else { |
| 53 | self.apply_account_sounds(previous, cx); |
| 54 | return; |
| 55 | }; |
| 56 | self.account_sounds = Some(updated.clone()); |
| 57 | let restore = previous.clone(); |
| 58 | self.run_chat_action( |
| 59 | "Sound setting", |
| 60 | async move { |
| 61 | engine |
| 62 | .save_sound_settings(&user_mri, &previous, &updated) |
| 63 | .await |
| 64 | }, |
| 65 | move |state, cx| state.apply_account_sounds(restore, cx), |
| 66 | |_, _| {}, |
| 67 | cx, |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | fn apply_account_sounds(&mut self, settings: SoundSettings, cx: &mut Context<Self>) { |
| 72 | self.account_sounds = Some(settings); |
| 73 | cx.emit(AppEvent::SoundSettings); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[cfg(test)] |
| 78 | mod tests { |
| 79 | use super::*; |
| 80 | |
| 81 | #[test] |
| 82 | fn the_ringtone_endpoint_gets_a_skype_mri() { |
| 83 | assert_eq!(user_mri("f74e540a"), "8:orgid:f74e540a"); |
| 84 | assert_eq!(user_mri("8:orgid:f74e540a"), "8:orgid:f74e540a"); |
| 85 | } |
| 86 | } |