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