BlueGit

ict rusty-teams

Files

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

profile_state.rs

profile_state.rs 5.8 KiB Download Raw
1use std::collections::HashMap;
2use std::sync::Arc;
3use std::time::Instant;
4
5use gpui_kit::*;
6use teams_core::PersonProfile;
7
8use crate::app_state::{AppEvent, AppState, Selection};
9use crate::data::existing_one_on_one;
10use crate::demo_profiles;
11use crate::notice::short_error;
12use crate::profile_model::is_stale;
13
14#[derive(Default)]
15pub struct ProfileEntry {
16 pub profile: Option<Arc<PersonProfile>>,
17 pub fetching: bool,
18 pub failed: bool,
19 fetched_at: Option<Instant>,
20}
21
22impl ProfileEntry {
23 fn is_fresh(&self, now: Instant) -> bool {
24 self.profile.is_some() && self.fetched_at.is_some_and(|at| !is_stale(at, now))
25 }
26}
27
28pub struct ProfileRequest {
29 pub user_id: String,
30 pub anchor: Point<Pixels>,
31}
32
33pub type ProfileCache = HashMap<String, ProfileEntry>;
34
35impl AppState {
36 pub fn open_profile_card(
37 &mut self,
38 user_id: &str,
39 anchor: Point<Pixels>,
40 cx: &mut Context<Self>,
41 ) {
42 self.profile_request = Some(ProfileRequest {
43 user_id: user_id.to_owned(),
44 anchor,
45 });
46 self.request_profile(user_id, false, cx);
47 self.request_avatars(vec![user_id.to_owned()], cx);
48 cx.emit(AppEvent::Profile);
49 cx.notify();
50 }
51
52 pub fn take_profile_request(&mut self) -> Option<ProfileRequest> {
53 self.profile_request.take()
54 }
55
56 pub fn request_profile(&mut self, user_id: &str, force: bool, cx: &mut Context<Self>) {
57 let now = Instant::now();
58 let entry = self.profiles.entry(user_id.to_owned()).or_default();
59 if entry.fetching || (!force && entry.is_fresh(now)) {
60 return;
61 }
62 let owned_id = user_id.to_owned();
63 let receiver = if self.mode.demo {
64 crate::runtime::spawn(demo_profiles::load(owned_id.clone()))
65 } else if let Some(engine) = self.engine.clone() {
66 let id = owned_id.clone();
67 crate::runtime::spawn(async move {
68 engine
69 .person_profile(&id)
70 .await
71 .map_err(|error| short_error(&error))
72 })
73 } else {
74 entry.failed = true;
75 cx.notify();
76 return;
77 };
78 entry.fetching = true;
79 entry.failed = false;
80 cx.notify();
81 cx.spawn(async move |this, cx| {
82 let result = receiver
83 .await
84 .unwrap_or_else(|_| Err("cancelled".to_owned()));
85 this.update(cx, |state, cx| state.finish_profile(&owned_id, result, cx))
86 .ok();
87 })
88 .detach();
89 }
90
91 fn finish_profile(
92 &mut self,
93 user_id: &str,
94 result: Result<PersonProfile, String>,
95 cx: &mut Context<Self>,
96 ) {
97 let entry = self.profiles.entry(user_id.to_owned()).or_default();
98 entry.fetching = false;
99 match result {
100 Ok(profile) => {
101 let related: Vec<String> = profile
102 .manager
103 .iter()
104 .chain(&profile.direct_reports)
105 .map(|person| person.id.clone())
106 .collect();
107 entry.profile = Some(Arc::new(profile));
108 entry.failed = false;
109 entry.fetched_at = Some(Instant::now());
110 self.request_avatars(related, cx);
111 }
112 Err(_) => entry.failed = true,
113 }
114 cx.notify();
115 }
116
117 pub fn copy_profile_email(&mut self, email: &str, cx: &mut Context<Self>) {
118 cx.write_to_clipboard(ClipboardItem::new_string(email.to_owned()));
119 self.raise_notice("Email copied".to_owned(), None, cx);
120 }
121
122 pub fn open_chat_with(&mut self, user_id: &str, cx: &mut Context<Self>) {
123 self.with_one_on_one(user_id, cx, |state, chat_id, cx| {
124 state.select(Selection::Chat(chat_id.to_owned()), cx)
125 });
126 }
127
128 /// Teams starts a 1:1 call from the profile card; the 1:1 chat is the call thread.
129 pub fn call_person(&mut self, user_id: &str, with_video: bool, cx: &mut Context<Self>) {
130 self.with_one_on_one(user_id, cx, move |state, chat_id, cx| {
131 if with_video {
132 state.start_chat_video_call(chat_id, cx);
133 } else {
134 state.start_chat_call(chat_id, cx);
135 }
136 });
137 }
138
139 fn with_one_on_one(
140 &mut self,
141 user_id: &str,
142 cx: &mut Context<Self>,
143 then: impl FnOnce(&mut Self, &str, &mut Context<Self>) + 'static,
144 ) {
145 let existing =
146 existing_one_on_one(&self.sidebar.chats, self.directory.me.as_ref(), user_id)
147 .map(|chat| chat.id.clone());
148 if let Some(chat_id) = existing {
149 then(self, &chat_id, cx);
150 return;
151 }
152 if self.mode.read_only {
153 self.raise_notice("Read-only mode: chat not created".to_owned(), None, cx);
154 return;
155 }
156 let Some(engine) = self.engine.clone() else {
157 self.raise_notice("No chat with this person yet".to_owned(), None, cx);
158 return;
159 };
160 let user_id = user_id.to_owned();
161 let receiver = crate::runtime::spawn(async move {
162 engine
163 .create_one_on_one(&user_id)
164 .await
165 .map_err(|error| short_error(&error))
166 });
167 cx.spawn(async move |this, cx| {
168 let outcome = receiver
169 .await
170 .unwrap_or_else(|_| Err("cancelled".to_owned()));
171 this.update(cx, |state, cx| match outcome {
172 Ok(chat_id) => {
173 state.reload_sidebar(cx);
174 then(state, &chat_id, cx);
175 }
176 Err(reason) => {
177 state.raise_notice(format!("Chat could not be opened: {reason}"), None, cx)
178 }
179 })
180 .ok();
181 })
182 .detach();
183 }
184}