| 1 | use chatsvc::substrate_search::{QueryRequest, QueryScope, SearchFile, SearchSession}; |
| 2 | use graph::{CloudFile, DriveEntry, DriveFolder, FileReference, QuickSite}; |
| 3 | |
| 4 | use crate::engine::SyncEngine; |
| 5 | use crate::error::{Error, Result}; |
| 6 | use crate::remote::Remote; |
| 7 | use crate::stored::FileKind; |
| 8 | |
| 9 | const RECENT_FETCH: usize = 40; |
| 10 | pub const RECENT_SHOWN: usize = 20; |
| 11 | const SEARCH_SIZE: usize = 20; |
| 12 | |
| 13 | pub(crate) fn recent_documents(files: Vec<CloudFile>) -> Vec<CloudFile> { |
| 14 | files |
| 15 | .into_iter() |
| 16 | .filter(|file| FileKind::from_name(&file.name).is_document()) |
| 17 | .take(RECENT_SHOWN) |
| 18 | .collect() |
| 19 | } |
| 20 | |
| 21 | fn found_file(file: SearchFile) -> Option<CloudFile> { |
| 22 | Some(CloudFile { |
| 23 | name: file.name, |
| 24 | size: file.size.unwrap_or(0), |
| 25 | web_url: file.url?, |
| 26 | drive_id: None, |
| 27 | item_id: None, |
| 28 | last_opened: file.modified_at, |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | impl<R: Remote> SyncEngine<R> { |
| 33 | /// The documents Teams lists under "Recent cloud files", newest first. |
| 34 | pub async fn recent_cloud_files(&self) -> Result<Vec<CloudFile>> { |
| 35 | Ok(recent_documents(self.remote.recent_files(RECENT_FETCH).await?)) |
| 36 | } |
| 37 | |
| 38 | /// The picker's Home list: every recently opened file with its owner, newest first. |
| 39 | pub async fn picker_recent(&self) -> Result<Vec<(CloudFile, Option<String>)>> { |
| 40 | let (files, owners) = self.remote.recent_files_with_owners(RECENT_FETCH).await?; |
| 41 | Ok(files |
| 42 | .into_iter() |
| 43 | .map(|file| { |
| 44 | let owner = owners.iter().find(|(id, _)| file.item_id.as_deref() == Some(id)).map(|(_, owner)| owner.clone()); |
| 45 | (file, owner) |
| 46 | }) |
| 47 | .collect()) |
| 48 | } |
| 49 | |
| 50 | pub async fn picker_shared(&self) -> Result<Vec<DriveEntry>> { |
| 51 | self.remote.shared_with_me().await |
| 52 | } |
| 53 | |
| 54 | pub async fn picker_sites(&self) -> Result<Vec<QuickSite>> { |
| 55 | self.remote.quick_access_sites().await |
| 56 | } |
| 57 | |
| 58 | /// The root of the user's OneDrive, or of a site's document library, as a folder to list. |
| 59 | pub async fn picker_root(&self, site_id: Option<&str>) -> Result<DriveFolder> { |
| 60 | self.remote.drive_root(site_id).await |
| 61 | } |
| 62 | |
| 63 | pub async fn picker_folder(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> { |
| 64 | let mut entries = self.remote.list_children(folder).await?; |
| 65 | entries.sort_by(|first, second| { |
| 66 | second.is_folder().cmp(&first.is_folder()).then_with(|| first.name.to_lowercase().cmp(&second.name.to_lowercase())) |
| 67 | }); |
| 68 | Ok(entries) |
| 69 | } |
| 70 | |
| 71 | pub async fn search_cloud_files(&self, search_session: &SearchSession, text: &str) -> Result<Vec<CloudFile>> { |
| 72 | let request = QueryRequest { |
| 73 | size: SEARCH_SIZE, |
| 74 | ..QueryRequest::new(text, QueryScope::Files) |
| 75 | }; |
| 76 | let results = self.server_query(search_session, &request).await?; |
| 77 | Ok(results.files.items.into_iter().filter_map(found_file).collect()) |
| 78 | } |
| 79 | |
| 80 | /// Shares `file` with the chat like an upload; a file the user cannot share still goes out as a link. |
| 81 | pub async fn attach_cloud_file(&self, conversation_id: &str, file: &CloudFile) -> Result<FileReference> { |
| 82 | let uploaded = self.remote.cloud_file_reference(file).await?; |
| 83 | let reference = uploaded |
| 84 | .reference() |
| 85 | .ok_or_else(|| Error::Unsupported("a cloud file without an eTag"))?; |
| 86 | self.share_attachment(conversation_id, &uploaded).await.ok(); |
| 87 | Ok(reference) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | #[cfg(test)] |
| 92 | mod tests { |
| 93 | use super::*; |
| 94 | |
| 95 | fn file(name: &str) -> CloudFile { |
| 96 | CloudFile { |
| 97 | name: name.to_owned(), |
| 98 | size: 0, |
| 99 | web_url: format!("https://x/{name}"), |
| 100 | drive_id: None, |
| 101 | item_id: None, |
| 102 | last_opened: None, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn recent_cloud_files_keep_documents_like_teams() { |
| 108 | let names: Vec<String> = recent_documents(vec![file("a.pdf"), file("clip.mp4"), file("b.pptx"), file("c.jpg"), file("d.xlsx")]) |
| 109 | .into_iter() |
| 110 | .map(|file| file.name) |
| 111 | .collect(); |
| 112 | assert_eq!(names, ["a.pdf", "b.pptx", "d.xlsx"]); |
| 113 | } |
| 114 | } |