BlueGit

ict rusty-teams

Files

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

models.rs

models.rs 8.3 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 #[serde(default)]
116 pub display_name: Option<String>,
117 #[serde(default)]
118 pub reaction_content_url: Option<String>,
119 #[serde(default)]
120 pub reaction_id: Option<String>,
121 pub created_date_time: Option<DateTime<Utc>>,
122 pub user: Option<Sender>,
123}
124
125#[derive(Debug, Clone, Deserialize, PartialEq)]
126#[serde(rename_all = "camelCase")]
127pub struct Mention {
128 pub id: Option<i64>,
129 pub mention_text: Option<String>,
130 pub mentioned: Option<Sender>,
131}
132
133#[derive(Debug, Clone, Deserialize, PartialEq)]
134#[serde(rename_all = "camelCase")]
135pub struct Message {
136 pub id: String,
137 pub message_type: Option<String>,
138 pub created_date_time: Option<DateTime<Utc>>,
139 pub last_modified_date_time: Option<DateTime<Utc>>,
140 pub last_edited_date_time: Option<DateTime<Utc>>,
141 pub deleted_date_time: Option<DateTime<Utc>>,
142 pub is_deleted: Option<bool>,
143 pub reply_to_id: Option<String>,
144 pub subject: Option<String>,
145 pub importance: Option<String>,
146 pub from: Option<Sender>,
147 pub body: Option<Body>,
148 pub event_detail: Option<serde_json::Value>,
149 #[serde(default)]
150 pub attachments: Vec<Attachment>,
151 #[serde(default)]
152 pub reactions: Vec<Reaction>,
153 #[serde(default)]
154 pub mentions: Vec<Mention>,
155 #[serde(default)]
156 pub replies: Vec<Message>,
157}
158
159impl Message {
160 pub fn is_deleted(&self) -> bool {
161 self.deleted_date_time.is_some() || self.is_deleted == Some(true)
162 }
163}
164
165#[derive(Debug, Clone, Deserialize, PartialEq)]
166#[serde(rename_all = "camelCase")]
167pub struct ChatViewpoint {
168 pub last_message_read_date_time: Option<DateTime<Utc>>,
169 pub is_hidden: Option<bool>,
170}
171
172#[derive(Debug, Clone, Deserialize, PartialEq)]
173#[serde(rename_all = "camelCase")]
174pub struct Chat {
175 pub id: String,
176 pub topic: Option<String>,
177 pub chat_type: String,
178 pub created_date_time: Option<DateTime<Utc>>,
179 pub last_updated_date_time: Option<DateTime<Utc>>,
180 #[serde(default)]
181 pub members: Vec<Member>,
182 pub last_message_preview: Option<Message>,
183 pub viewpoint: Option<ChatViewpoint>,
184 #[serde(default)]
185 pub online_meeting_info: Option<OnlineMeetingInfo>,
186}
187
188#[derive(Debug, Clone, Deserialize, PartialEq)]
189#[serde(rename_all = "camelCase")]
190pub struct OnlineMeetingInfo {
191 pub join_web_url: Option<String>,
192}
193
194impl Chat {
195 pub fn title(&self, my_user_id: &str) -> String {
196 if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) {
197 return topic.to_owned();
198 }
199 let names: Vec<&str> = self
200 .members
201 .iter()
202 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
203 .map(Member::name)
204 .collect();
205 if names.is_empty() {
206 self.preview_sender_name(my_user_id)
207 .unwrap_or("(only you)")
208 .to_owned()
209 } else {
210 names.join(", ")
211 }
212 }
213
214 /// 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.
215 pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> {
216 if !self.chat_type.eq_ignore_ascii_case("oneOnOne")
217 || self
218 .members
219 .iter()
220 .any(|member| member.user_id.as_deref() != Some(my_user_id))
221 {
222 return None;
223 }
224 let pair = self
225 .id
226 .strip_prefix("19:")?
227 .strip_suffix("@unq.gbl.spaces")?;
228 let (first, second) = pair.split_once('_')?;
229 match (first == my_user_id, second == my_user_id) {
230 (true, false) => Some(second),
231 (false, true) => Some(first),
232 _ => None,
233 }
234 .filter(|partner| !partner.is_empty())
235 }
236
237 fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> {
238 let sender = self.last_message_preview.as_ref()?.from.as_ref()?;
239 let application = sender
240 .application
241 .as_ref()
242 .and_then(|identity| identity.display_name.as_deref());
243 let other_user = sender
244 .user
245 .as_ref()
246 .filter(|identity| identity.id.as_deref() != Some(my_user_id))
247 .and_then(|identity| identity.display_name.as_deref());
248 application.or(other_user).filter(|name| !name.is_empty())
249 }
250
251 pub fn last_message_time(&self) -> Option<DateTime<Utc>> {
252 self.last_message_preview
253 .as_ref()
254 .and_then(|preview| preview.created_date_time)
255 }
256}
257
258#[derive(Debug, Clone, Deserialize, PartialEq)]
259#[serde(rename_all = "camelCase")]
260pub struct Team {
261 pub id: String,
262 pub display_name: String,
263}
264
265#[derive(Debug, Clone, Deserialize, PartialEq)]
266#[serde(rename_all = "camelCase")]
267pub struct Tag {
268 pub id: String,
269 pub display_name: String,
270 pub member_count: Option<u32>,
271}
272
273#[derive(Debug, Clone, Deserialize, PartialEq)]
274#[serde(rename_all = "camelCase")]
275pub struct Channel {
276 pub id: String,
277 pub display_name: String,
278 pub membership_type: Option<String>,
279}
280
281#[derive(Debug, Clone, PartialEq, Eq)]
282pub struct Photo {
283 pub bytes: Vec<u8>,
284 pub content_type: String,
285}
286
287#[derive(Debug, Clone, PartialEq, Eq)]
288pub struct Presence {
289 pub user_id: String,
290 pub availability: String,
291 pub activity: Option<String>,
292}