BlueGit

ict rusty-teams

Files

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

models.rs

models.rs 6.3 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 is_deleted: Option<bool>,
113 pub reply_to_id: Option<String>,
114 pub subject: Option<String>,
115 pub from: Option<Sender>,
116 pub body: Option<Body>,
117 pub event_detail: Option<serde_json::Value>,
118 #[serde(default)]
119 pub attachments: Vec<Attachment>,
120 #[serde(default)]
121 pub reactions: Vec<Reaction>,
122 #[serde(default)]
123 pub mentions: Vec<Mention>,
124 #[serde(default)]
125 pub replies: Vec<Message>,
126}
127
128impl Message {
129 pub fn is_deleted(&self) -> bool {
130 self.deleted_date_time.is_some() || self.is_deleted == Some(true)
131 }
132}
133
134#[derive(Debug, Clone, Deserialize, PartialEq)]
135#[serde(rename_all = "camelCase")]
136pub struct ChatViewpoint {
137 pub last_message_read_date_time: Option<DateTime<Utc>>,
138 pub is_hidden: Option<bool>,
139}
140
141#[derive(Debug, Clone, Deserialize, PartialEq)]
142#[serde(rename_all = "camelCase")]
143pub struct Chat {
144 pub id: String,
145 pub topic: Option<String>,
146 pub chat_type: String,
147 pub last_updated_date_time: Option<DateTime<Utc>>,
148 #[serde(default)]
149 pub members: Vec<Member>,
150 pub last_message_preview: Option<Message>,
151 pub viewpoint: Option<ChatViewpoint>,
152}
153
154impl Chat {
155 pub fn title(&self, my_user_id: &str) -> String {
156 if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) {
157 return topic.to_owned();
158 }
159 let names: Vec<&str> = self
160 .members
161 .iter()
162 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
163 .map(|member| {
164 member
165 .display_name
166 .as_deref()
167 .or(member.email.as_deref())
168 .unwrap_or("?")
169 })
170 .collect();
171 if names.is_empty() {
172 self.preview_sender_name(my_user_id)
173 .unwrap_or("(only you)")
174 .to_owned()
175 } else {
176 names.join(", ")
177 }
178 }
179
180 fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> {
181 let sender = self.last_message_preview.as_ref()?.from.as_ref()?;
182 let application = sender
183 .application
184 .as_ref()
185 .and_then(|identity| identity.display_name.as_deref());
186 let other_user = sender
187 .user
188 .as_ref()
189 .filter(|identity| identity.id.as_deref() != Some(my_user_id))
190 .and_then(|identity| identity.display_name.as_deref());
191 application.or(other_user).filter(|name| !name.is_empty())
192 }
193
194 pub fn last_message_time(&self) -> Option<DateTime<Utc>> {
195 self.last_message_preview
196 .as_ref()
197 .and_then(|preview| preview.created_date_time)
198 }
199}
200
201#[derive(Debug, Clone, Deserialize, PartialEq)]
202#[serde(rename_all = "camelCase")]
203pub struct Team {
204 pub id: String,
205 pub display_name: String,
206}
207
208#[derive(Debug, Clone, Deserialize, PartialEq)]
209#[serde(rename_all = "camelCase")]
210pub struct Channel {
211 pub id: String,
212 pub display_name: String,
213 pub membership_type: Option<String>,
214}
215
216#[derive(Debug, Clone, PartialEq, Eq)]
217pub struct Photo {
218 pub bytes: Vec<u8>,
219 pub content_type: String,
220}
221
222#[derive(Debug, Clone, PartialEq, Eq)]
223pub struct Presence {
224 pub user_id: String,
225 pub availability: String,
226 pub activity: Option<String>,
227}