| 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 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")] |
| 27 | pub struct Identity { |
| 28 | pub id: Option<String>, |
| 29 | pub display_name: Option<String>, |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug, Clone, Deserialize, PartialEq, Default)] |
| 33 | pub struct Sender { |
| 34 | pub user: Option<Identity>, |
| 35 | pub application: Option<Identity>, |
| 36 | pub tag: Option<Identity>, |
| 37 | pub conversation: Option<Identity>, |
| 38 | } |
| 39 | |
| 40 | impl 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")] |
| 72 | pub 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")] |
| 79 | pub 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")] |
| 89 | pub 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")] |
| 97 | pub 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")] |
| 105 | pub 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 | |
| 128 | impl 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")] |
| 136 | pub 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")] |
| 143 | pub 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 | |
| 154 | impl 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 | /// 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. |
| 181 | pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> { |
| 182 | if !self.chat_type.eq_ignore_ascii_case("oneOnOne") |
| 183 | || self |
| 184 | .members |
| 185 | .iter() |
| 186 | .any(|member| member.user_id.as_deref() != Some(my_user_id)) |
| 187 | { |
| 188 | return None; |
| 189 | } |
| 190 | let pair = self |
| 191 | .id |
| 192 | .strip_prefix("19:")? |
| 193 | .strip_suffix("@unq.gbl.spaces")?; |
| 194 | let (first, second) = pair.split_once('_')?; |
| 195 | match (first == my_user_id, second == my_user_id) { |
| 196 | (true, false) => Some(second), |
| 197 | (false, true) => Some(first), |
| 198 | _ => None, |
| 199 | } |
| 200 | .filter(|partner| !partner.is_empty()) |
| 201 | } |
| 202 | |
| 203 | fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> { |
| 204 | let sender = self.last_message_preview.as_ref()?.from.as_ref()?; |
| 205 | let application = sender |
| 206 | .application |
| 207 | .as_ref() |
| 208 | .and_then(|identity| identity.display_name.as_deref()); |
| 209 | let other_user = sender |
| 210 | .user |
| 211 | .as_ref() |
| 212 | .filter(|identity| identity.id.as_deref() != Some(my_user_id)) |
| 213 | .and_then(|identity| identity.display_name.as_deref()); |
| 214 | application.or(other_user).filter(|name| !name.is_empty()) |
| 215 | } |
| 216 | |
| 217 | pub fn last_message_time(&self) -> Option<DateTime<Utc>> { |
| 218 | self.last_message_preview |
| 219 | .as_ref() |
| 220 | .and_then(|preview| preview.created_date_time) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 225 | #[serde(rename_all = "camelCase")] |
| 226 | pub struct Team { |
| 227 | pub id: String, |
| 228 | pub display_name: String, |
| 229 | } |
| 230 | |
| 231 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 232 | #[serde(rename_all = "camelCase")] |
| 233 | pub struct Channel { |
| 234 | pub id: String, |
| 235 | pub display_name: String, |
| 236 | pub membership_type: Option<String>, |
| 237 | } |
| 238 | |
| 239 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 240 | pub struct Photo { |
| 241 | pub bytes: Vec<u8>, |
| 242 | pub content_type: String, |
| 243 | } |
| 244 | |
| 245 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 246 | pub struct Presence { |
| 247 | pub user_id: String, |
| 248 | pub availability: String, |
| 249 | pub activity: Option<String>, |
| 250 | } |