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