| 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 | } |
| 36 | |
| 37 | impl Sender { |
| 38 | pub fn display_name(&self) -> Option<&str> { |
| 39 | [&self.user, &self.application] |
| 40 | .into_iter() |
| 41 | .flatten() |
| 42 | .find_map(|identity| identity.display_name.as_deref()) |
| 43 | } |
| 44 | |
| 45 | pub fn user_id(&self) -> Option<&str> { |
| 46 | self.user.as_ref().and_then(|user| user.id.as_deref()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 51 | #[serde(rename_all = "camelCase")] |
| 52 | pub struct Body { |
| 53 | pub content_type: String, |
| 54 | pub content: Option<String>, |
| 55 | } |
| 56 | |
| 57 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 58 | #[serde(rename_all = "camelCase")] |
| 59 | pub struct Attachment { |
| 60 | pub id: Option<String>, |
| 61 | pub content_type: Option<String>, |
| 62 | pub content_url: Option<String>, |
| 63 | pub name: Option<String>, |
| 64 | pub content: Option<String>, |
| 65 | } |
| 66 | |
| 67 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 68 | #[serde(rename_all = "camelCase")] |
| 69 | pub struct Reaction { |
| 70 | pub reaction_type: String, |
| 71 | pub created_date_time: Option<DateTime<Utc>>, |
| 72 | pub user: Option<Sender>, |
| 73 | } |
| 74 | |
| 75 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 76 | #[serde(rename_all = "camelCase")] |
| 77 | pub struct Mention { |
| 78 | pub id: Option<i64>, |
| 79 | pub mention_text: Option<String>, |
| 80 | pub mentioned: Option<Sender>, |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 84 | #[serde(rename_all = "camelCase")] |
| 85 | pub struct Message { |
| 86 | pub id: String, |
| 87 | pub message_type: Option<String>, |
| 88 | pub created_date_time: Option<DateTime<Utc>>, |
| 89 | pub last_modified_date_time: Option<DateTime<Utc>>, |
| 90 | pub last_edited_date_time: Option<DateTime<Utc>>, |
| 91 | pub deleted_date_time: Option<DateTime<Utc>>, |
| 92 | pub reply_to_id: Option<String>, |
| 93 | pub subject: Option<String>, |
| 94 | pub from: Option<Sender>, |
| 95 | pub body: Option<Body>, |
| 96 | #[serde(default)] |
| 97 | pub attachments: Vec<Attachment>, |
| 98 | #[serde(default)] |
| 99 | pub reactions: Vec<Reaction>, |
| 100 | #[serde(default)] |
| 101 | pub mentions: Vec<Mention>, |
| 102 | #[serde(default)] |
| 103 | pub replies: Vec<Message>, |
| 104 | } |
| 105 | |
| 106 | impl Message { |
| 107 | pub fn is_deleted(&self) -> bool { |
| 108 | self.deleted_date_time.is_some() |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 113 | #[serde(rename_all = "camelCase")] |
| 114 | pub struct ChatViewpoint { |
| 115 | pub last_message_read_date_time: Option<DateTime<Utc>>, |
| 116 | pub is_hidden: Option<bool>, |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 120 | #[serde(rename_all = "camelCase")] |
| 121 | pub struct Chat { |
| 122 | pub id: String, |
| 123 | pub topic: Option<String>, |
| 124 | pub chat_type: String, |
| 125 | pub last_updated_date_time: Option<DateTime<Utc>>, |
| 126 | #[serde(default)] |
| 127 | pub members: Vec<Member>, |
| 128 | pub last_message_preview: Option<Message>, |
| 129 | pub viewpoint: Option<ChatViewpoint>, |
| 130 | } |
| 131 | |
| 132 | impl Chat { |
| 133 | pub fn title(&self, my_user_id: &str) -> String { |
| 134 | if let Some(topic) = self.topic.as_deref().filter(|topic| !topic.is_empty()) { |
| 135 | return topic.to_owned(); |
| 136 | } |
| 137 | let names: Vec<&str> = self |
| 138 | .members |
| 139 | .iter() |
| 140 | .filter(|member| member.user_id.as_deref() != Some(my_user_id)) |
| 141 | .map(|member| { |
| 142 | member |
| 143 | .display_name |
| 144 | .as_deref() |
| 145 | .or(member.email.as_deref()) |
| 146 | .unwrap_or("?") |
| 147 | }) |
| 148 | .collect(); |
| 149 | if names.is_empty() { |
| 150 | "(only you)".to_owned() |
| 151 | } else { |
| 152 | names.join(", ") |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pub fn last_message_time(&self) -> Option<DateTime<Utc>> { |
| 157 | self.last_message_preview |
| 158 | .as_ref() |
| 159 | .and_then(|preview| preview.created_date_time) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 164 | #[serde(rename_all = "camelCase")] |
| 165 | pub struct Team { |
| 166 | pub id: String, |
| 167 | pub display_name: String, |
| 168 | } |
| 169 | |
| 170 | #[derive(Debug, Clone, Deserialize, PartialEq)] |
| 171 | #[serde(rename_all = "camelCase")] |
| 172 | pub struct Channel { |
| 173 | pub id: String, |
| 174 | pub display_name: String, |
| 175 | pub membership_type: Option<String>, |
| 176 | } |
| 177 | |
| 178 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 179 | pub struct Photo { |
| 180 | pub bytes: Vec<u8>, |
| 181 | pub content_type: String, |
| 182 | } |
| 183 | |
| 184 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 185 | pub struct Presence { |
| 186 | pub user_id: String, |
| 187 | pub availability: String, |
| 188 | pub activity: Option<String>, |
| 189 | } |