BlueGit

ict rusty-teams

Files

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

models.rs

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