| 1 | use gpui_kit::*; |
| 2 | |
| 3 | use crate::app_state::AppState; |
| 4 | |
| 5 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 6 | pub struct AbsenceNotice { |
| 7 | pub name: String, |
| 8 | } |
| 9 | |
| 10 | impl AbsenceNotice { |
| 11 | pub fn text(&self) -> String { |
| 12 | format!("{} is out of office and may not respond", self.name) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | fn notice_for( |
| 17 | dismissed: bool, |
| 18 | out_of_office: Option<&str>, |
| 19 | profile_name: Option<&str>, |
| 20 | partner_name: &str, |
| 21 | ) -> Option<AbsenceNotice> { |
| 22 | if dismissed { |
| 23 | return None; |
| 24 | } |
| 25 | out_of_office?; |
| 26 | let name = profile_name |
| 27 | .map(str::trim) |
| 28 | .filter(|name| !name.is_empty()) |
| 29 | .unwrap_or(partner_name); |
| 30 | Some(AbsenceNotice { |
| 31 | name: name.to_owned(), |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | impl AppState { |
| 36 | pub fn refresh_partner_absence(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 37 | if self.mode.demo { |
| 38 | return; |
| 39 | } |
| 40 | if let Some(partner) = self.one_on_one_partner(conversation_id) { |
| 41 | self.request_profile(&partner.user_id, false, cx); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn absence_notice(&self, conversation_id: &str) -> Option<AbsenceNotice> { |
| 46 | let partner = self.one_on_one_partner(conversation_id)?; |
| 47 | let demo_profile; |
| 48 | let profile = if self.mode.demo { |
| 49 | demo_profile = crate::demo_profiles::profile(&partner.user_id)?; |
| 50 | &demo_profile |
| 51 | } else { |
| 52 | self.profiles.get(&partner.user_id)?.profile.as_deref()? |
| 53 | }; |
| 54 | notice_for( |
| 55 | self.dismissed_absence_notices.contains(conversation_id), |
| 56 | profile.out_of_office.as_deref(), |
| 57 | profile.display_name.as_deref(), |
| 58 | &partner.name, |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | pub fn dismiss_absence_notice(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 63 | self.dismissed_absence_notices |
| 64 | .insert(conversation_id.to_owned()); |
| 65 | cx.notify(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
| 71 | use super::notice_for; |
| 72 | |
| 73 | #[test] |
| 74 | fn out_of_office_shows_the_notice_until_it_is_closed() { |
| 75 | let notice = notice_for(false, Some(""), Some("Lea Schneider"), "Lea").unwrap(); |
| 76 | assert_eq!( |
| 77 | notice.text(), |
| 78 | "Lea Schneider is out of office and may not respond" |
| 79 | ); |
| 80 | assert_eq!( |
| 81 | notice_for(true, Some(""), Some("Lea Schneider"), "Lea"), |
| 82 | None |
| 83 | ); |
| 84 | assert_eq!(notice_for(false, None, Some("Lea Schneider"), "Lea"), None); |
| 85 | assert_eq!( |
| 86 | notice_for(false, Some("Back Monday"), Some(" "), "Lea") |
| 87 | .unwrap() |
| 88 | .name, |
| 89 | "Lea" |
| 90 | ); |
| 91 | } |
| 92 | } |