BlueGit

ict rusty-teams

Files

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

models.rs

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