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 created_date_time: Option<DateTime<Utc>>,
171 pub last_updated_date_time: Option<DateTime<Utc>>,
172 #[serde(default)]
173 pub members: Vec<Member>,
174 pub last_message_preview: Option<Message>,
175 pub viewpoint: Option<ChatViewpoint>,
176 #[serde(default)]
177 pub online_meeting_info: Option<OnlineMeetingInfo>,
178}
179
180#[derive(Debug, Clone, Deserialize, PartialEq)]
181#[serde(rename_all = "camelCase")]
182pub struct OnlineMeetingInfo {
183 pub join_web_url: Option<String>,
184}
185
186impl Chat {
187 pub fn title(&self, my_user_id: &str) -> String {
188 if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) {
189 return topic.to_owned();
190 }
191 let names: Vec<&str> = self
192 .members
193 .iter()
194 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
195 .map(Member::name)
196 .collect();
197 if names.is_empty() {
198 self.preview_sender_name(my_user_id)
199 .unwrap_or("(only you)")
200 .to_owned()
201 } else {
202 names.join(", ")
203 }
204 }
205
206 /// 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.
207 pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> {
208 if !self.chat_type.eq_ignore_ascii_case("oneOnOne")
209 || self
210 .members
211 .iter()
212 .any(|member| member.user_id.as_deref() != Some(my_user_id))
213 {
214 return None;
215 }
216 let pair = self
217 .id
218 .strip_prefix("19:")?
219 .strip_suffix("@unq.gbl.spaces")?;
220 let (first, second) = pair.split_once('_')?;
221 match (first == my_user_id, second == my_user_id) {
222 (true, false) => Some(second),
223 (false, true) => Some(first),
224 _ => None,
225 }
226 .filter(|partner| !partner.is_empty())
227 }
228
229 fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> {
230 let sender = self.last_message_preview.as_ref()?.from.as_ref()?;
231 let application = sender
232 .application
233 .as_ref()
234 .and_then(|identity| identity.display_name.as_deref());
235 let other_user = sender
236 .user
237 .as_ref()
238 .filter(|identity| identity.id.as_deref() != Some(my_user_id))
239 .and_then(|identity| identity.display_name.as_deref());
240 application.or(other_user).filter(|name| !name.is_empty())
241 }
242
243 pub fn last_message_time(&self) -> Option<DateTime<Utc>> {
244 self.last_message_preview
245 .as_ref()
246 .and_then(|preview| preview.created_date_time)
247 }
248}
249
250#[derive(Debug, Clone, Deserialize, PartialEq)]
251#[serde(rename_all = "camelCase")]
252pub struct Team {
253 pub id: String,
254 pub display_name: String,
255}
256
257#[derive(Debug, Clone, Deserialize, PartialEq)]
258#[serde(rename_all = "camelCase")]
259pub struct Tag {
260 pub id: String,
261 pub display_name: String,
262 pub member_count: Option<u32>,
263}
264
265#[derive(Debug, Clone, Deserialize, PartialEq)]
266#[serde(rename_all = "camelCase")]
267pub struct Channel {
268 pub id: String,
269 pub display_name: String,
270 pub membership_type: Option<String>,
271}
272
273#[derive(Debug, Clone, PartialEq, Eq)]
274pub struct Photo {
275 pub bytes: Vec<u8>,
276 pub content_type: String,
277}
278
279#[derive(Debug, Clone, PartialEq, Eq)]
280pub struct Presence {
281 pub user_id: String,
282 pub availability: String,
283 pub activity: Option<String>,
284}