BlueGit

ict rusty-teams

Files

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

absence_notice.rs

absence_notice.rs 2.4 KiB Download Raw
1use gpui_kit::*;
2
3use crate::app_state::AppState;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct AbsenceNotice {
7 pub name: String,
8}
9
10impl AbsenceNotice {
11 pub fn text(&self) -> String {
12 format!("{} is out of office and may not respond", self.name)
13 }
14}
15
16fn 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 { name: name.to_owned() })
31}
32
33impl AppState {
34 pub fn refresh_partner_absence(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
35 if self.mode.demo {
36 return;
37 }
38 if let Some(partner) = self.one_on_one_partner(conversation_id) {
39 self.request_profile(&partner.user_id, false, cx);
40 }
41 }
42
43 pub fn absence_notice(&self, conversation_id: &str) -> Option<AbsenceNotice> {
44 let partner = self.one_on_one_partner(conversation_id)?;
45 let demo_profile;
46 let profile = if self.mode.demo {
47 demo_profile = crate::demo_profiles::profile(&partner.user_id)?;
48 &demo_profile
49 } else {
50 self.profiles.get(&partner.user_id)?.profile.as_deref()?
51 };
52 notice_for(
53 self.dismissed_absence_notices.contains(conversation_id),
54 profile.out_of_office.as_deref(),
55 profile.display_name.as_deref(),
56 &partner.name,
57 )
58 }
59
60 pub fn dismiss_absence_notice(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
61 self.dismissed_absence_notices.insert(conversation_id.to_owned());
62 cx.notify();
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::notice_for;
69
70 #[test]
71 fn out_of_office_shows_the_notice_until_it_is_closed() {
72 let notice = notice_for(false, Some(""), Some("Lea Schneider"), "Lea").unwrap();
73 assert_eq!(notice.text(), "Lea Schneider is out of office and may not respond");
74 assert_eq!(notice_for(true, Some(""), Some("Lea Schneider"), "Lea"), None);
75 assert_eq!(notice_for(false, None, Some("Lea Schneider"), "Lea"), None);
76 assert_eq!(notice_for(false, Some("Back Monday"), Some(" "), "Lea").unwrap().name, "Lea");
77 }
78}