BlueGit

ict rusty-teams

Files

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

models.rs

models.rs 6.2 KiB Download Raw
1use chrono::{DateTime, Utc};
2use serde::Deserialize;
3
4#[derive(Debug, Clone, Deserialize, PartialEq)]
5#[serde(rename_all = "camelCase")]
6pub struct User {
7 pub id: String,
8 pub display_name: Option<String>,
9 pub mail: Option<String>,
10 pub user_principal_name: Option<String>,
11 pub job_title: Option<String>,
12 pub department: Option<String>,
13}
14
15#[derive(Debug, Clone, Deserialize, PartialEq)]
16#[serde(rename_all = "camelCase")]
17pub struct Member {
18 pub user_id: Option<String>,
19 pub tenant_id: Option<String>,
20 pub display_name: Option<String>,
21 pub email: Option<String>,
22}
23
24#[derive(Debug, Clone, Deserialize, PartialEq)]
25#[serde(rename_all = "camelCase")]
26pub struct Identity {
27 pub id: Option<String>,
28 pub display_name: Option<String>,
29}
30
31#[derive(Debug, Clone, Deserialize, PartialEq, Default)]
32pub struct Sender {
33 pub user: Option<Identity>,
34 pub application: Option<Identity>,
35 pub tag: Option<Identity>,
36 pub conversation: Option<Identity>,
37}
38
39impl Sender {
40 pub fn display_name(&self) -> Option<&str> {
41 [&self.user, &self.application, &self.tag, &self.conversation]
42 .into_iter()
43 .flatten()
44 .find_map(|identity| identity.display_name.as_deref())
45 }
46
47 pub fn target_id(&self) -> Option<&str> {
48 [&self.user, &self.application, &self.tag, &self.conversation]
49 .into_iter()
50 .flatten()
51 .find_map(|identity| identity.id.as_deref())
52 }
53
54 pub fn is_group(&self) -> bool {
55 self.tag.is_some() || self.conversation.is_some()
56 }
57
58 pub fn user_id(&self) -> Option<&str> {
59 self.user.as_ref().and_then(|user| user.id.as_deref())
60 }
61
62 pub fn application_id(&self) -> Option<&str> {
63 self.application
64 .as_ref()
65 .and_then(|application| application.id.as_deref())
66 }
67}
68
69#[derive(Debug, Clone, Deserialize, PartialEq)]
70#[serde(rename_all = "camelCase")]
71pub struct Body {
72 pub content_type: String,
73 pub content: Option<String>,
74}
75
76#[derive(Debug, Clone, Deserialize, PartialEq)]
77#[serde(rename_all = "camelCase")]
78pub struct Attachment {
79 pub id: Option<String>,
80 pub content_type: Option<String>,
81 pub content_url: Option<String>,
82 pub name: Option<String>,
83 pub content: Option<String>,
84}
85
86#[derive(Debug, Clone, Deserialize, PartialEq)]
87#[serde(rename_all = "camelCase")]
88pub struct Reaction {
89 pub reaction_type: String,
90 pub created_date_time: Option<DateTime<Utc>>,
91 pub user: Option<Sender>,
92}
93
94#[derive(Debug, Clone, Deserialize, PartialEq)]
95#[serde(rename_all = "camelCase")]
96pub struct Mention {
97 pub id: Option<i64>,
98 pub mention_text: Option<String>,
99 pub mentioned: Option<Sender>,
100}
101
102#[derive(Debug, Clone, Deserialize, PartialEq)]
103#[serde(rename_all = "camelCase")]
104pub struct Message {
105 pub id: String,
106 pub message_type: Option<String>,
107 pub created_date_time: Option<DateTime<Utc>>,
108 pub last_modified_date_time: Option<DateTime<Utc>>,
109 pub last_edited_date_time: Option<DateTime<Utc>>,
110 pub deleted_date_time: Option<DateTime<Utc>>,
111 pub reply_to_id: Option<String>,
112 pub subject: Option<String>,
113 pub from: Option<Sender>,
114 pub body: Option<Body>,
115 #[serde(default)]
116 pub attachments: Vec<Attachment>,
117 #[serde(default)]
118 pub reactions: Vec<Reaction>,
119 #[serde(default)]
120 pub mentions: Vec<Mention>,
121 #[serde(default)]
122 pub replies: Vec<Message>,
123}
124
125impl Message {
126 pub fn is_deleted(&self) -> bool {
127 self.deleted_date_time.is_some()
128 }
129}
130
131#[derive(Debug, Clone, Deserialize, PartialEq)]
132#[serde(rename_all = "camelCase")]
133pub struct ChatViewpoint {
134 pub last_message_read_date_time: Option<DateTime<Utc>>,
135 pub is_hidden: Option<bool>,
136}
137
138#[derive(Debug, Clone, Deserialize, PartialEq)]
139#[serde(rename_all = "camelCase")]
140pub struct Chat {
141 pub id: String,
142 pub topic: Option<String>,
143 pub chat_type: String,
144 pub last_updated_date_time: Option<DateTime<Utc>>,
145 #[serde(default)]
146 pub members: Vec<Member>,
147 pub last_message_preview: Option<Message>,
148 pub viewpoint: Option<ChatViewpoint>,
149}
150
151impl Chat {
152 pub fn title(&self, my_user_id: &str) -> String {
153 if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) {
154 return topic.to_owned();
155 }
156 let names: Vec<&str> = self
157 .members
158 .iter()
159 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
160 .map(|member| {
161 member
162 .display_name
163 .as_deref()
164 .or(member.email.as_deref())
165 .unwrap_or("?")
166 })
167 .collect();
168 if names.is_empty() {
169 self.preview_sender_name(my_user_id)
170 .unwrap_or("(only you)")
171 .to_owned()
172 } else {
173 names.join(", ")
174 }
175 }
176
177 fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> {
178 let sender = self.last_message_preview.as_ref()?.from.as_ref()?;
179 let application = sender
180 .application
181 .as_ref()
182 .and_then(|identity| identity.display_name.as_deref());
183 let other_user = sender
184 .user
185 .as_ref()
186 .filter(|identity| identity.id.as_deref() != Some(my_user_id))
187 .and_then(|identity| identity.display_name.as_deref());
188 application.or(other_user).filter(|name| !name.is_empty())
189 }
190
191 pub fn last_message_time(&self) -> Option<DateTime<Utc>> {
192 self.last_message_preview
193 .as_ref()
194 .and_then(|preview| preview.created_date_time)
195 }
196}
197
198#[derive(Debug, Clone, Deserialize, PartialEq)]
199#[serde(rename_all = "camelCase")]
200pub struct Team {
201 pub id: String,
202 pub display_name: String,
203}
204
205#[derive(Debug, Clone, Deserialize, PartialEq)]
206#[serde(rename_all = "camelCase")]
207pub struct Channel {
208 pub id: String,
209 pub display_name: String,
210 pub membership_type: Option<String>,
211}
212
213#[derive(Debug, Clone, PartialEq, Eq)]
214pub struct Photo {
215 pub bytes: Vec<u8>,
216 pub content_type: String,
217}
218
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub struct Presence {
221 pub user_id: String,
222 pub availability: String,
223 pub activity: Option<String>,
224}