| 1 | use chrono::{DateTime, Utc}; |
| 2 | use serde::Deserialize; |
| 3 | use serde_json::json; |
| 4 | use session::{Method, Scope}; |
| 5 | |
| 6 | use crate::client::Graph; |
| 7 | use crate::error::{Error, Result}; |
| 8 | use crate::files::{DriveFolder, FILES_SCOPE}; |
| 9 | use crate::urls; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] |
| 12 | #[serde(from = "RawEntry")] |
| 13 | pub struct DriveEntry { |
| 14 | pub drive_id: String, |
| 15 | pub id: String, |
| 16 | pub name: String, |
| 17 | pub web_url: String, |
| 18 | pub size: u64, |
| 19 | pub modified_at: Option<DateTime<Utc>>, |
| 20 | pub modified_by: Option<String>, |
| 21 | pub child_count: Option<u64>, |
| 22 | pub download_url: Option<String>, |
| 23 | } |
| 24 | |
| 25 | impl DriveEntry { |
| 26 | pub fn is_folder(&self) -> bool { |
| 27 | self.child_count.is_some() |
| 28 | } |
| 29 | |
| 30 | pub fn folder(&self) -> DriveFolder { |
| 31 | DriveFolder { |
| 32 | drive_id: self.drive_id.clone(), |
| 33 | item_id: self.id.clone(), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[derive(Deserialize)] |
| 39 | #[serde(rename_all = "camelCase")] |
| 40 | struct RawEntry { |
| 41 | id: String, |
| 42 | name: String, |
| 43 | #[serde(default)] |
| 44 | web_url: String, |
| 45 | #[serde(default)] |
| 46 | size: u64, |
| 47 | last_modified_date_time: Option<DateTime<Utc>>, |
| 48 | last_modified_by: Option<RawActor>, |
| 49 | folder: Option<RawFolder>, |
| 50 | #[serde(default, rename = "@microsoft.graph.downloadUrl")] |
| 51 | download_url: Option<String>, |
| 52 | parent_reference: Option<RawParent>, |
| 53 | } |
| 54 | |
| 55 | #[derive(Deserialize)] |
| 56 | struct RawActor { |
| 57 | user: Option<RawIdentity>, |
| 58 | application: Option<RawIdentity>, |
| 59 | } |
| 60 | |
| 61 | #[derive(Deserialize)] |
| 62 | #[serde(rename_all = "camelCase")] |
| 63 | struct RawIdentity { |
| 64 | display_name: Option<String>, |
| 65 | } |
| 66 | |
| 67 | #[derive(Deserialize)] |
| 68 | #[serde(rename_all = "camelCase")] |
| 69 | struct RawFolder { |
| 70 | #[serde(default)] |
| 71 | child_count: u64, |
| 72 | } |
| 73 | |
| 74 | #[derive(Deserialize)] |
| 75 | #[serde(rename_all = "camelCase")] |
| 76 | struct RawParent { |
| 77 | drive_id: Option<String>, |
| 78 | } |
| 79 | |
| 80 | impl From<RawEntry> for DriveEntry { |
| 81 | fn from(raw: RawEntry) -> Self { |
| 82 | let modified_by = raw.last_modified_by.and_then(|actor| { |
| 83 | actor |
| 84 | .user |
| 85 | .or(actor.application) |
| 86 | .and_then(|identity| identity.display_name) |
| 87 | }); |
| 88 | DriveEntry { |
| 89 | drive_id: raw |
| 90 | .parent_reference |
| 91 | .and_then(|parent| parent.drive_id) |
| 92 | .unwrap_or_default(), |
| 93 | id: raw.id, |
| 94 | name: raw.name, |
| 95 | web_url: raw.web_url, |
| 96 | size: raw.size, |
| 97 | modified_at: raw.last_modified_date_time, |
| 98 | modified_by, |
| 99 | child_count: raw.folder.map(|folder| folder.child_count), |
| 100 | download_url: raw.download_url.filter(|url| !url.is_empty()), |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Graph { |
| 106 | pub async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> { |
| 107 | let body = self |
| 108 | .get( |
| 109 | &urls::channel_files_folder(team_id, channel_id), |
| 110 | &Scope::graph(FILES_SCOPE), |
| 111 | ) |
| 112 | .await?; |
| 113 | let entry: DriveEntry = serde_json::from_value(body)?; |
| 114 | if entry.drive_id.is_empty() { |
| 115 | return Err(Error::Upload("the channel folder has no drive".into())); |
| 116 | } |
| 117 | Ok(entry) |
| 118 | } |
| 119 | |
| 120 | pub async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> { |
| 121 | self.get_all( |
| 122 | &urls::folder_children(&folder.drive_id, &folder.item_id), |
| 123 | Scope::graph(FILES_SCOPE), |
| 124 | ) |
| 125 | .await |
| 126 | } |
| 127 | |
| 128 | pub async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> { |
| 129 | let answer = self |
| 130 | .session() |
| 131 | .request( |
| 132 | Method::Post, |
| 133 | &urls::folder_create(&parent.drive_id, &parent.item_id), |
| 134 | &Scope::graph(FILES_SCOPE), |
| 135 | Some(json!({ |
| 136 | "name": name, |
| 137 | "folder": {}, |
| 138 | "@microsoft.graph.conflictBehavior": "rename", |
| 139 | })), |
| 140 | ) |
| 141 | .await?; |
| 142 | let mut entry: DriveEntry = serde_json::from_value(answer.body)?; |
| 143 | if entry.drive_id.is_empty() { |
| 144 | entry.drive_id.clone_from(&parent.drive_id); |
| 145 | } |
| 146 | Ok(entry) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[cfg(test)] |
| 151 | mod tests { |
| 152 | use serde_json::json; |
| 153 | use session::Scope; |
| 154 | |
| 155 | use super::*; |
| 156 | use crate::page::Page; |
| 157 | |
| 158 | fn page(body: serde_json::Value) -> Page<DriveEntry> { |
| 159 | Page::parse(body, Scope::graph(FILES_SCOPE)).unwrap() |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn children_parse_files_and_folders() { |
| 164 | let parsed = page(json!({"value": [ |
| 165 | { |
| 166 | "id": "f1", "name": "Plans", "webUrl": "https://x/Plans", "size": 0, |
| 167 | "folder": {"childCount": 3}, |
| 168 | "lastModifiedDateTime": "2026-10-01T08:30:00Z", |
| 169 | "lastModifiedBy": {"user": {"displayName": "Mara Lindqvist"}}, |
| 170 | "parentReference": {"driveId": "b!d"} |
| 171 | }, |
| 172 | { |
| 173 | "id": "i1", "name": "Plan.pdf", "webUrl": "https://x/Plan.pdf", "size": 2048, |
| 174 | "file": {"mimeType": "application/pdf"}, |
| 175 | "@microsoft.graph.downloadUrl": "https://dl.example/plan", |
| 176 | "lastModifiedBy": {"application": {"displayName": "Flow"}}, |
| 177 | "parentReference": {"driveId": "b!d"} |
| 178 | } |
| 179 | ]})); |
| 180 | let [folder, file] = parsed.items.as_slice() else { |
| 181 | panic!("two entries"); |
| 182 | }; |
| 183 | assert!(folder.is_folder()); |
| 184 | assert_eq!(folder.child_count, Some(3)); |
| 185 | assert_eq!(folder.modified_by.as_deref(), Some("Mara Lindqvist")); |
| 186 | assert_eq!(folder.folder().drive_id, "b!d"); |
| 187 | assert!(!file.is_folder()); |
| 188 | assert_eq!(file.size, 2048); |
| 189 | assert_eq!(file.modified_by.as_deref(), Some("Flow")); |
| 190 | assert_eq!( |
| 191 | file.download_url.as_deref(), |
| 192 | Some("https://dl.example/plan") |
| 193 | ); |
| 194 | assert_eq!(file.modified_at, None); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn a_next_link_continues_the_listing() { |
| 199 | let first = page(json!({ |
| 200 | "value": [{"id": "1", "name": "a"}], |
| 201 | "@odata.nextLink": "https://graph.microsoft.com/v1.0/drives/d/items/i/children?$skiptoken=x" |
| 202 | })); |
| 203 | let last = page(json!({"value": [{"id": "2", "name": "b"}]})); |
| 204 | assert!(first.next_link.is_some()); |
| 205 | assert!(last.next_link.is_none()); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn listing_keeps_the_download_url_by_not_selecting_fields() { |
| 210 | let url = urls::folder_children("b!d", "01ABC"); |
| 211 | assert!(url.ends_with("/children?$top=200")); |
| 212 | assert!(!url.contains("$select")); |
| 213 | } |
| 214 | } |