use chrono::{DateTime, Utc}; use serde::Deserialize; pub const UNKNOWN_USER_NAME: &str = "Unknown User"; #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct User { pub id: String, pub display_name: Option, pub mail: Option, pub user_principal_name: Option, pub job_title: Option, pub department: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Member { pub id: Option, pub user_id: Option, pub tenant_id: Option, pub display_name: Option, pub email: Option, } impl Member { pub fn name(&self) -> &str { [&self.display_name, &self.email] .into_iter() .flatten() .map(|name| name.trim()) .find(|name| !name.is_empty()) .unwrap_or(UNKNOWN_USER_NAME) } } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Identity { pub id: Option, pub display_name: Option, } #[derive(Debug, Clone, Deserialize, PartialEq, Default)] pub struct Sender { pub user: Option, pub application: Option, pub tag: Option, pub conversation: Option, } impl Sender { pub fn display_name(&self) -> Option<&str> { [&self.user, &self.application, &self.tag, &self.conversation] .into_iter() .flatten() .find_map(|identity| identity.display_name.as_deref()) } pub fn target_id(&self) -> Option<&str> { [&self.user, &self.application, &self.tag, &self.conversation] .into_iter() .flatten() .find_map(|identity| identity.id.as_deref()) } pub fn is_group(&self) -> bool { self.tag.is_some() || self.conversation.is_some() } pub fn user_id(&self) -> Option<&str> { self.user.as_ref().and_then(|user| user.id.as_deref()) } pub fn application_id(&self) -> Option<&str> { self.application .as_ref() .and_then(|application| application.id.as_deref()) } } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Body { pub content_type: String, pub content: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Attachment { pub id: Option, pub content_type: Option, pub content_url: Option, pub name: Option, pub content: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Reaction { pub reaction_type: String, pub created_date_time: Option>, pub user: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Mention { pub id: Option, pub mention_text: Option, pub mentioned: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Message { pub id: String, pub message_type: Option, pub created_date_time: Option>, pub last_modified_date_time: Option>, pub last_edited_date_time: Option>, pub deleted_date_time: Option>, pub is_deleted: Option, pub reply_to_id: Option, pub subject: Option, pub importance: Option, pub from: Option, pub body: Option, pub event_detail: Option, #[serde(default)] pub attachments: Vec, #[serde(default)] pub reactions: Vec, #[serde(default)] pub mentions: Vec, #[serde(default)] pub replies: Vec, } impl Message { pub fn is_deleted(&self) -> bool { self.deleted_date_time.is_some() || self.is_deleted == Some(true) } } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct ChatViewpoint { pub last_message_read_date_time: Option>, pub is_hidden: Option, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Chat { pub id: String, pub topic: Option, pub chat_type: String, pub last_updated_date_time: Option>, #[serde(default)] pub members: Vec, pub last_message_preview: Option, pub viewpoint: Option, } impl Chat { pub fn title(&self, my_user_id: &str) -> String { if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) { return topic.to_owned(); } let names: Vec<&str> = self .members .iter() .filter(|member| member.user_id.as_deref() != Some(my_user_id)) .map(Member::name) .collect(); if names.is_empty() { self.preview_sender_name(my_user_id) .unwrap_or("(only you)") .to_owned() } else { names.join(", ") } } /// 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. pub fn memberless_partner_id(&self, my_user_id: &str) -> Option<&str> { if !self.chat_type.eq_ignore_ascii_case("oneOnOne") || self .members .iter() .any(|member| member.user_id.as_deref() != Some(my_user_id)) { return None; } let pair = self .id .strip_prefix("19:")? .strip_suffix("@unq.gbl.spaces")?; let (first, second) = pair.split_once('_')?; match (first == my_user_id, second == my_user_id) { (true, false) => Some(second), (false, true) => Some(first), _ => None, } .filter(|partner| !partner.is_empty()) } fn preview_sender_name(&self, my_user_id: &str) -> Option<&str> { let sender = self.last_message_preview.as_ref()?.from.as_ref()?; let application = sender .application .as_ref() .and_then(|identity| identity.display_name.as_deref()); let other_user = sender .user .as_ref() .filter(|identity| identity.id.as_deref() != Some(my_user_id)) .and_then(|identity| identity.display_name.as_deref()); application.or(other_user).filter(|name| !name.is_empty()) } pub fn last_message_time(&self) -> Option> { self.last_message_preview .as_ref() .and_then(|preview| preview.created_date_time) } } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Team { pub id: String, pub display_name: String, } #[derive(Debug, Clone, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Channel { pub id: String, pub display_name: String, pub membership_type: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Photo { pub bytes: Vec, pub content_type: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Presence { pub user_id: String, pub availability: String, pub activity: Option, }