| 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( |
| 36 | self.remote.recent_files(RECENT_FETCH).await?, |
| 37 | )) |
| 38 | } |
| 39 | |
| 40 | /// The picker's Home list: every recently opened file with its owner, newest first. |
| 41 | pub async fn picker_recent(&self) -> Result<Vec<(CloudFile, Option<String>)>> { |
| 42 | let (files, owners) = self.remote.recent_files_with_owners(RECENT_FETCH).await?; |
| 43 | Ok(files |
| 44 | .into_iter() |
| 45 | .map(|file| { |
| 46 | let owner = owners |
| 47 | .iter() |
| 48 | .find(|(id, _)| file.item_id.as_deref() == Some(id)) |
| 49 | .map(|(_, owner)| owner.clone()); |
| 50 | (file, owner) |
| 51 | }) |
| 52 | .collect()) |
| 53 | } |
| 54 | |
| 55 | pub async fn picker_shared(&self) -> Result<Vec<DriveEntry>> { |
| 56 | self.remote.shared_with_me().await |
| 57 | } |
| 58 | |
| 59 | pub async fn picker_sites(&self) -> Result<Vec<QuickSite>> { |
| 60 | self.remote.quick_access_sites().await |
| 61 | } |
| 62 | |
| 63 | /// The root of the user's OneDrive, or of a site's document library, as a folder to list. |
| 64 | pub async fn picker_root(&self, site_id: Option<&str>) -> Result<DriveFolder> { |
| 65 | self.remote.drive_root(site_id).await |
| 66 | } |
| 67 | |
| 68 | pub async fn picker_folder(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> { |
| 69 | let mut entries = self.remote.list_children(folder).await?; |
| 70 | entries.sort_by(|first, second| { |
| 71 | second |
| 72 | .is_folder() |
| 73 | .cmp(&first.is_folder()) |
| 74 | .then_with(|| first.name.to_lowercase().cmp(&second.name.to_lowercase())) |
| 75 | }); |
| 76 | Ok(entries) |
| 77 | } |
| 78 | |
| 79 | pub async fn search_cloud_files( |
| 80 | &self, |
| 81 | search_session: &SearchSession, |
| 82 | text: &str, |
| 83 | ) -> Result<Vec<CloudFile>> { |
| 84 | let request = QueryRequest { |
| 85 | size: SEARCH_SIZE, |
| 86 | ..QueryRequest::new(text, QueryScope::Files) |
| 87 | }; |
| 88 | let results = self.server_query(search_session, &request).await?; |
| 89 | Ok(results |
| 90 | .files |
| 91 | .items |
| 92 | .into_iter() |
| 93 | .filter_map(found_file) |
| 94 | .collect()) |
| 95 | } |
| 96 | |
| 97 | /// Shares `file` with the chat like an upload; a file the user cannot share still goes out as a link. |
| 98 | pub async fn attach_cloud_file( |
| 99 | &self, |
| 100 | conversation_id: &str, |
| 101 | file: &CloudFile, |
| 102 | ) -> Result<FileReference> { |
| 103 | let uploaded = self.remote.cloud_file_reference(file).await?; |
| 104 | let reference = uploaded |
| 105 | .reference() |
| 106 | .ok_or_else(|| Error::Unsupported("a cloud file without an eTag"))?; |
| 107 | self.share_attachment(conversation_id, &uploaded).await.ok(); |
| 108 | Ok(reference) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[cfg(test)] |
| 113 | mod tests { |
| 114 | use super::*; |
| 115 | |
| 116 | fn file(name: &str) -> CloudFile { |
| 117 | CloudFile { |
| 118 | name: name.to_owned(), |
| 119 | size: 0, |
| 120 | web_url: format!("https://x/{name}"), |
| 121 | drive_id: None, |
| 122 | item_id: None, |
| 123 | last_opened: None, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn recent_cloud_files_keep_documents_like_teams() { |
| 129 | let names: Vec<String> = recent_documents(vec![ |
| 130 | file("a.pdf"), |
| 131 | file("clip.mp4"), |
| 132 | file("b.pptx"), |
| 133 | file("c.jpg"), |
| 134 | file("d.xlsx"), |
| 135 | ]) |
| 136 | .into_iter() |
| 137 | .map(|file| file.name) |
| 138 | .collect(); |
| 139 | assert_eq!(names, ["a.pdf", "b.pptx", "d.xlsx"]); |
| 140 | } |
| 141 | } |