BlueGit

ict rusty-teams

Files

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

models.rs

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