BlueGit

ict rusty-teams

Files

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

models.rs

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