| 1 | use chrono::{DateTime, Utc}; |
| 2 | use serde::Deserialize; |
| 3 | |
| 4 | pub const UNKNOWN_USER_NAME: &str = "Unknown User"; |
| 5 | |
| 6 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 7 | #[serde(rename_all = "camelCase")] |
| 8 | pub struct User { |
| 9 | pub id: String, |
| 10 | pub display_name: Option<String>, |
| 11 | pub mail: Option<String>, |
| 12 | pub user_principal_name: Option<String>, |
| 13 | pub job_title: Option<String>, |
| 14 | pub department: Option<String>, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 18 | #[serde(rename_all = "camelCase")] |
| 19 | pub struct Member { |
| 20 | pub id: Option<String>, |
| 21 | pub user_id: Option<String>, |
| 22 | pub tenant_id: Option<String>, |
| 23 | pub display_name: Option<String>, |
| 24 | pub email: Option<String>, |
| 25 | } |
| 26 | |
| 27 | impl Member { |
| 28 | pub fn name(&self) -> &str { |
| 29 | [&self.display_name, &self.email] |
| 30 | .into_iter() |
| 31 | .flatten() |
| 32 | .map(|name| name.trim()) |
| 33 | .find(|name| !name.is_empty()) |
| 34 | .unwrap_or(UNKNOWN_USER_NAME) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 39 | #[serde(rename_all = "camelCase")] |
| 40 | pub struct Identity { |
| 41 | pub id: Option<String>, |
| 42 | pub display_name: Option<String>, |
| 43 | } |
| 44 | |
| 45 | #[derive(Debug, Clone, Deserialize, PartialEq, Default)] |
| 46 | pub struct Sender { |
| 47 | pub user: Option<Identity>, |
| 48 | pub application: Option<Identity>, |
| 49 | pub tag: Option<Identity>, |
| 50 | pub conversation: Option<Identity>, |
| 51 | } |
| 52 | |
| 53 | impl Sender { |
| 54 | pub fn display_name(&self) -> Option<&str> { |
| 55 | [&self.user, &self.application, &self.tag, &self.conversation] |
| 56 | .into_iter() |
| 57 | .flatten() |
| 58 | .find_map(|identity| identity.display_name.as_deref()) |
| 59 | } |
| 60 | |
| 61 | pub fn target_id(&self) -> Option<&str> { |
| 62 | [&self.user, &self.application, &self.tag, &self.conversation] |
| 63 | .into_iter() |
| 64 | .flatten() |
| 65 | .find_map(|identity| identity.id.as_deref()) |
| 66 | } |
| 67 | |
| 68 | pub fn is_group(&self) -> bool { |
| 69 | self.tag.is_some() || self.conversation.is_some() |
| 70 | } |
| 71 | |
| 72 | pub fn user_id(&self) -> Option<&str> { |
| 73 | self.user.as_ref().and_then(|user| user.id.as_deref()) |
| 74 | } |
| 75 | |
| 76 | pub fn application_id(&self) -> Option<&str> { |
| 77 | self.application |
| 78 | .as_ref() |
| 79 | .and_then(|application| application.id.as_deref()) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 84 | #[serde(rename_all = "camelCase")] |
| 85 | pub struct Body { |
| 86 | pub content_type: String, |
| 87 | pub content: Option<String>, |
| 88 | } |
| 89 | |
| 90 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 91 | #[serde(rename_all = "camelCase")] |
| 92 | pub struct Attachment { |
| 93 | pub id: Option<String>, |
| 94 | pub content_type: Option<String>, |
| 95 | pub content_url: Option<String>, |
| 96 | pub name: Option<String>, |
| 97 | pub content: Option<String>, |
| 98 | } |
| 99 | |
| 100 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 101 | #[serde(rename_all = "camelCase")] |
| 102 | pub struct Reaction { |
| 103 | pub reaction_type: String, |
| 104 | pub created_date_time: Option<DateTime<Utc>>, |
| 105 | pub user: Option<Sender>, |
| 106 | } |
| 107 | |
| 108 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 109 | #[serde(rename_all = "camelCase")] |
| 110 | pub struct Mention { |
| 111 | pub id: Option<i64>, |
| 112 | pub mention_text: Option<String>, |
| 113 | pub mentioned: Option<Sender>, |
| 114 | } |
| 115 | |
| 116 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 117 | #[serde(rename_all = "camelCase")] |
| 118 | pub struct Message { |
| 119 | pub id: String, |
| 120 | pub message_type: Option<String>, |
| 121 | pub created_date_time: Option<DateTime<Utc>>, |
| 122 | pub last_modified_date_time: Option<DateTime<Utc>>, |
| 123 | pub last_edited_date_time: Option<DateTime<Utc>>, |
| 124 | pub deleted_date_time: Option<DateTime<Utc>>, |
| 125 | pub is_deleted: Option<bool>, |
| 126 | pub reply_to_id: Option<String>, |
| 127 | pub subject: Option<String>, |
| 128 | pub importance: Option<String>, |
| 129 | pub from: Option<Sender>, |
| 130 | pub body: Option<Body>, |
| 131 | pub event_detail: Option<serde_json::Value>, |
| 132 | #[serde(default)] |
| 133 | pub attachments: Vec<Attachment>, |
| 134 | #[serde(default)] |
| 135 | pub reactions: Vec<Reaction>, |
| 136 | #[serde(default)] |
| 137 | pub mentions: Vec<Mention>, |
| 138 | #[serde(default)] |
| 139 | pub replies: Vec<Message>, |
| 140 | } |
| 141 | |
| 142 | impl Message { |
| 143 | pub fn is_deleted(&self) -> bool { |
| 144 | self.deleted_date_time.is_some() || self.is_deleted == Some(true) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 149 | #[serde(rename_all = "camelCase")] |
| 150 | pub struct ChatViewpoint { |
| 151 | pub last_message_read_date_time: Option<DateTime<Utc>>, |
| 152 | pub is_hidden: Option<bool>, |
| 153 | } |
| 154 | |
| 155 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 156 | #[serde(rename_all = "camelCase")] |
| 157 | pub struct Chat { |
| 158 | pub id: String, |
| 159 | pub topic: Option<String>, |
| 160 | pub chat_type: String, |
| 161 | pub last_updated_date_time: Option<DateTime<Utc>>, |
| 162 | #[serde(default)] |
| 163 | pub members: Vec<Member>, |
| 164 | pub last_message_preview: Option<Message>, |
| 165 | pub viewpoint: Option<ChatViewpoint>, |
| 166 | } |
| 167 | |
| 168 | impl Chat { |
| 169 | pub fn title(&self, my_user_id: &str) -> String { |
| 170 | if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) { |
| 171 | return topic.to_owned(); |
| 172 | } |
| 173 | let names: Vec<&str> = self |
| 174 | .members |
| 175 | .iter() |
| 176 | .filter(|member| member.user_id.as_deref() != Some(my_user_id)) |
| 177 | .map(Member::name) |
| 178 | .collect(); |
| 179 | if names.is_empty() { |
| 180 | self.preview_sender_name(my_user_id) |
| 181 | .unwrap_or("(only you)") |
| 182 | .to_owned() |
| 183 | } else { |
| 184 | names.join(", ") |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// The other side of a 1:1 chat that Graph lists without a member, such as a bot, read from the `19:{a}_{b}@unq.gbl.spaces` id. |
| 189 | pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> { |
| 190 | if !self.chat_type.eq_ignore_ascii_case("oneOnOne") |
| 191 | || self |
| 192 | .members |
| 193 | .iter() |
| 194 | .any(|member| member.user_id.as_deref() != Some(my_user_id)) |
| 195 | { |
| 196 | return None; |
| 197 | } |
| 198 | let pair = self |
| 199 | .id |
| 200 | .strip_prefix("19:")? |
| 201 | .strip_suffix("@unq.gbl.spaces")?; |
| 202 | let (first, second) = pair.split_once('_')?; |
| 203 | match (first == my_user_id, second == my_user_id) { |
| 204 | (true, false) => Some(second), |
| 205 | (false, true) => Some(first), |
| 206 | _ => None, |
| 207 | } |
| 208 | .filter(|partner| !partner.is_empty()) |
| 209 | } |
| 210 | |
| 211 | fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> { |
| 212 | let sender = self.last_message_preview.as_ref()?.from.as_ref()?; |
| 213 | let application = sender |
| 214 | .application |
| 215 | .as_ref() |
| 216 | .and_then(|identity| identity.display_name.as_deref()); |
| 217 | let other_user = sender |
| 218 | .user |
| 219 | .as_ref() |
| 220 | .filter(|identity| identity.id.as_deref() != Some(my_user_id)) |
| 221 | .and_then(|identity| identity.display_name.as_deref()); |
| 222 | application.or(other_user).filter(|name| !name.is_empty()) |
| 223 | } |
| 224 | |
| 225 | pub fn last_message_time(&self) -> Option<DateTime<Utc>> { |
| 226 | self.last_message_preview |
| 227 | .as_ref() |
| 228 | .and_then(|preview| preview.created_date_time) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 233 | #[serde(rename_all = "camelCase")] |
| 234 | pub struct Team { |
| 235 | pub id: String, |
| 236 | pub display_name: String, |
| 237 | } |
| 238 | |
| 239 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 240 | #[serde(rename_all = "camelCase")] |
| 241 | pub struct Channel { |
| 242 | pub id: String, |
| 243 | pub display_name: String, |
| 244 | pub membership_type: Option<String>, |
| 245 | } |
| 246 | |
| 247 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 248 | pub struct Photo { |
| 249 | pub bytes: Vec<u8>, |
| 250 | pub content_type: String, |
| 251 | } |
| 252 | |
| 253 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 254 | pub struct Presence { |
| 255 | pub user_id: String, |
| 256 | pub availability: String, |
| 257 | pub activity: Option<String>, |
| 258 | } |