BlueGit

ict rusty-teams

Files

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

models.rs

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