BlueGit

ict rusty-teams

Files

rusty-teams / crates / graph / src / models.rs

models.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
models.rs 8.2 KiB Download Raw
1use chrono::{DateTime, Utc};
2use serde::Deserialize;
3
4pub const UNKNOWN_USER_NAME: &str = "Unknown User";
5/// Graph names a deleted account "?" in chat members.
6const DELETED_USER_NAME: &str = "?";
7
8#[derive(Debug, Clone, Deserialize, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub struct User {
11 pub id: String,
12 pub display_name: Option<String>,
13 pub mail: Option<String>,
14 pub user_principal_name: Option<String>,
15 pub job_title: Option<String>,
16 pub department: Option<String>,
17}
18
19#[derive(Debug, Clone, Deserialize, PartialEq)]
20#[serde(rename_all = "camelCase")]
21pub struct Member {
22 pub id: Option<String>,
23 pub user_id: Option<String>,
24 pub tenant_id: Option<String>,
25 pub display_name: Option<String>,
26 pub email: Option<String>,
27 #[serde(default)]
28 pub roles: Vec<String>,
29}
30
31impl Member {
32 pub fn is_owner(&self) -> bool {
33 self.roles
34 .iter()
35 .any(|role| role.eq_ignore_ascii_case("owner"))
36 }
37
38 pub fn name(&self) -> &str {
39 [&self.display_name, &self.email]
40 .into_iter()
41 .flatten()
42 .map(|name| name.trim())
43 .find(|name| !name.is_empty() && *name != DELETED_USER_NAME)
44 .unwrap_or(UNKNOWN_USER_NAME)
45 }
46}
47
48#[derive(Debug, Clone, Deserialize, PartialEq)]
49#[serde(rename_all = "camelCase")]
50pub struct Identity {
51 pub id: Option<String>,
52 pub display_name: Option<String>,
53}
54
55#[derive(Debug, Clone, Deserialize, PartialEq, Default)]
56pub struct Sender {
57 pub user: Option<Identity>,
58 pub application: Option<Identity>,
59 pub tag: Option<Identity>,
60 pub conversation: Option<Identity>,
61}
62
63impl Sender {
64 pub fn display_name(&self) -> Option<&str> {
65 [&self.user, &self.application, &self.tag, &self.conversation]
66 .into_iter()
67 .flatten()
68 .find_map(|identity| identity.display_name.as_deref())
69 }
70
71 pub fn target_id(&self) -> Option<&str> {
72 [&self.user, &self.application, &self.tag, &self.conversation]
73 .into_iter()
74 .flatten()
75 .find_map(|identity| identity.id.as_deref())
76 }
77
78 pub fn is_group(&self) -> bool {
79 self.tag.is_some() || self.conversation.is_some()
80 }
81
82 pub fn user_id(&self) -> Option<&str> {
83 self.user.as_ref().and_then(|user| user.id.as_deref())
84 }
85
86 pub fn application_id(&self) -> Option<&str> {
87 self.application
88 .as_ref()
89 .and_then(|application| application.id.as_deref())
90 }
91}
92
93#[derive(Debug, Clone, Deserialize, PartialEq)]
94#[serde(rename_all = "camelCase")]
95pub struct Body {
96 pub content_type: String,
97 pub content: Option<String>,
98}
99
100#[derive(Debug, Clone, Deserialize, PartialEq)]
101#[serde(rename_all = "camelCase")]
102pub struct Attachment {
103 pub id: Option<String>,
104 pub content_type: Option<String>,
105 pub content_url: Option<String>,
106 pub name: Option<String>,
107 pub content: Option<String>,
108 pub teams_app_id: Option<String>,
109}
110
111#[derive(Debug, Clone, Deserialize, PartialEq)]
112#[serde(rename_all = "camelCase")]
113pub struct Reaction {
114 pub reaction_type: String,
115 pub created_date_time: Option<DateTime<Utc>>,
116 pub user: Option<Sender>,
117}
118
119#[derive(Debug, Clone, Deserialize, PartialEq)]
120#[serde(rename_all = "camelCase")]
121pub struct Mention {
122 pub id: Option<i64>,
123 pub mention_text: Option<String>,
124 pub mentioned: Option<Sender>,
125}
126
127#[derive(Debug, Clone, Deserialize, PartialEq)]
128#[serde(rename_all = "camelCase")]
129pub struct Message {
130 pub id: String,
131 pub message_type: Option<String>,
132 pub created_date_time: Option<DateTime<Utc>>,
133 pub last_modified_date_time: Option<DateTime<Utc>>,
134 pub last_edited_date_time: Option<DateTime<Utc>>,
135 pub deleted_date_time: Option<DateTime<Utc>>,
136 pub is_deleted: Option<bool>,
137 pub reply_to_id: Option<String>,
138 pub subject: Option<String>,
139 pub importance: Option<String>,
140 pub from: Option<Sender>,
141 pub body: Option<Body>,
142 pub event_detail: Option<serde_json::Value>,
143 #[serde(default)]
144 pub attachments: Vec<Attachment>,
145 #[serde(default)]
146 pub reactions: Vec<Reaction>,
147 #[serde(default)]
148 pub mentions: Vec<Mention>,
149 #[serde(default)]
150 pub replies: Vec<Message>,
151}
152
153impl Message {
154 pub fn is_deleted(&self) -> bool {
155 self.deleted_date_time.is_some() || self.is_deleted == Some(true)
156 }
157}
158
159#[derive(Debug, Clone, Deserialize, PartialEq)]
160#[serde(rename_all = "camelCase")]
161pub struct ChatViewpoint {
162 pub last_message_read_date_time: Option<DateTime<Utc>>,
163 pub is_hidden: Option<bool>,
164}
165
166#[derive(Debug, Clone, Deserialize, PartialEq)]
167#[serde(rename_all = "camelCase")]
168pub struct Chat {
169 pub id: String,
170 pub topic: Option<String>,
171 pub chat_type: String,
172 pub created_date_time: Option<DateTime<Utc>>,
173 pub last_updated_date_time: Option<DateTime<Utc>>,
174 #[serde(default)]
175 pub members: Vec<Member>,
176 pub last_message_preview: Option<Message>,
177 pub viewpoint: Option<ChatViewpoint>,
178 #[serde(default)]
179 pub online_meeting_info: Option<OnlineMeetingInfo>,
180}
181
182#[derive(Debug, Clone, Deserialize, PartialEq)]
183#[serde(rename_all = "camelCase")]
184pub struct OnlineMeetingInfo {
185 pub join_web_url: Option<String>,
186}
187
188impl Chat {
189 pub fn title(&self, my_user_id: &str) -> String {
190 if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) {
191 return topic.to_owned();
192 }
193 let names: Vec<&str> = self
194 .members
195 .iter()
196 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
197 .map(Member::name)
198 .collect();
199 if names.is_empty() {
200 self.preview_sender_name(my_user_id)
201 .unwrap_or("(only you)")
202 .to_owned()
203 } else {
204 names.join(", ")
205 }
206 }
207
208 /// The other side of a 1:1 chat that Graph lists without a member, such as a bot, read from the `19:{a}_{b}@unq.gbl.spaces` id.
209 pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> {
210 if !self.chat_type.eq_ignore_ascii_case("oneOnOne")
211 || self
212 .members
213 .iter()
214 .any(|member| member.user_id.as_deref() != Some(my_user_id))
215 {
216 return None;
217 }
218 let pair = self
219 .id
220 .strip_prefix("19:")?
221 .strip_suffix("@unq.gbl.spaces")?;
222 let (first, second) = pair.split_once('_')?;
223 match (first == my_user_id, second == my_user_id) {
224 (true, false) => Some(second),
225 (false, true) => Some(first),
226 _ => None,
227 }
228 .filter(|partner| !partner.is_empty())
229 }
230
231 fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> {
232 let sender = self.last_message_preview.as_ref()?.from.as_ref()?;
233 let application = sender
234 .application
235 .as_ref()
236 .and_then(|identity| identity.display_name.as_deref());
237 let other_user = sender
238 .user
239 .as_ref()
240 .filter(|identity| identity.id.as_deref() != Some(my_user_id))
241 .and_then(|identity| identity.display_name.as_deref());
242 application.or(other_user).filter(|name| !name.is_empty())
243 }
244
245 pub fn last_message_time(&self) -> Option<DateTime<Utc>> {
246 self.last_message_preview
247 .as_ref()
248 .and_then(|preview| preview.created_date_time)
249 }
250}
251
252#[derive(Debug, Clone, Deserialize, PartialEq)]
253#[serde(rename_all = "camelCase")]
254pub struct Team {
255 pub id: String,
256 pub display_name: String,
257}
258
259#[derive(Debug, Clone, Deserialize, PartialEq)]
260#[serde(rename_all = "camelCase")]
261pub struct Tag {
262 pub id: String,
263 pub display_name: String,
264 pub member_count: Option<u32>,
265}
266
267#[derive(Debug, Clone, Deserialize, PartialEq)]
268#[serde(rename_all = "camelCase")]
269pub struct Channel {
270 pub id: String,
271 pub display_name: String,
272 pub membership_type: Option<String>,
273}
274
275#[derive(Debug, Clone, PartialEq, Eq)]
276pub struct Photo {
277 pub bytes: Vec<u8>,
278 pub content_type: String,
279}
280
281#[derive(Debug, Clone, PartialEq, Eq)]
282pub struct Presence {
283 pub user_id: String,
284 pub availability: String,
285 pub activity: Option<String>,
286}