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