diff --git a/crates/in-web/src/drive.rs b/crates/in-web/src/drive.rs index b416243..65b2183 100644 --- a/crates/in-web/src/drive.rs +++ b/crates/in-web/src/drive.rs @@ -7,6 +7,11 @@ //! is trashed is unreachable, and a move that would nest a folder inside //! itself is refused. Anything naming another owner's tree answers 404, //! never 403. +//! +//! `GET /drive?q=` keeps the same page and swaps the folder panels +//! for library-wide name hits — the filterbar's search box, submitted as a +//! plain GET form. The old `GET /search?q=` address 303s here, so old links +//! keep working. use in_core::store::{Folder, Store, StoreError, ThumbState}; use topcoat::router::request::uri; @@ -48,9 +53,13 @@ fn redirect(cx: &Cx, refusal: Option) -> Redirect { )) } +/// How many hits each search list carries at most. +const LIMIT: u32 = 50; + #[query_params] struct DriveQuery { folder: Option, + q: Option, } /// The folder the query names, if it names one this account may open: owned, @@ -169,9 +178,21 @@ async fn drive(cx: &Cx) -> Result { let language = lang(cx).await; let store = app(cx).store.clone(); - let wanted = query_params::(cx) - .ok() - .and_then(|query| query.folder.clone()); + let params = query_params::(cx).ok(); + let wanted = params.as_ref().and_then(|query| query.folder.clone()); + // The filterbar's search box, trimmed: anything else on the query is + // ignored, and an empty box is the plain folder view, never the whole + // library. + let asked = params + .as_ref() + .and_then(|query| query.q.clone()) + .map(|query| query.trim().to_string()) + .filter(|query| !query.is_empty()); + let hits = match asked.as_deref() { + Some(query) => Some(store.search(&user.id, query, LIMIT).await?), + None => None, + }; + let box_text = asked.clone().unwrap_or_default(); let current = current_folder(store.as_ref(), &user.id, wanted.as_deref()).await?; let crumbs = breadcrumbs(store.as_ref(), current.as_ref()).await; let listing = match store @@ -202,22 +223,39 @@ async fn drive(cx: &Cx) -> Result { (topbar(cx, NavPage::Drive, &user, language).await?)
- if current.is_some() { - + if let Some(query) = asked.as_deref() { +

(t(language, Key::SearchResults)) (format!(" “{query}”"))

+ } else { + if current.is_some() { + + } }
-
- - - + +
+
+ (t(language, Key::NewFolder)) +
+ + + +
+
if let Some(refusal) = refusal {

(refusal.message_in(language))

@@ -231,6 +269,37 @@ async fn drive(cx: &Cx) -> Result { } + if let Some(hits) = hits { + if hits.folders.is_empty() && hits.files.is_empty() { +

(t(language, Key::NoResults))

+ } + if !hits.folders.is_empty() { +
+
+

(t(language, Key::FoldersHeading))

+ (format!("{}", hits.folders.len())) +
+
+ for folder in &hits.folders { +

(folder.name.clone())

+ } +
+
+ } + if !hits.files.is_empty() { +
+
+

(t(language, Key::FilesHeading))

+ (format!("{}", hits.files.len())) +
+
+ for file in &hits.files { +

(file.name.clone())

+ } +
+
+ } + } else {

(t(language, Key::FoldersHeading))

@@ -321,12 +390,41 @@ async fn drive(cx: &Cx) -> Result {
+ }
(crate::dropdown::dropdown_script(cx).await?) (upload_script(cx).await?) } } +/// `GET /search`: the old address — a 303 to the drive's search box, keeping +/// the query so old links land on their results. Nothing renders here. +#[page("/search")] +async fn search_redirect(cx: &Cx) -> Result { + let target = match query_value(uri(cx).query().unwrap_or(""), "q") { + Some(raw) if !raw.trim().is_empty() => format!("/drive?q={raw}"), + _ => "/drive".to_string(), + }; + let location = ( + header::LOCATION, + HeaderValue::from_str(&target).unwrap_or(HeaderValue::from_static("/drive")), + ); + view! { + cx => + (StatusCode::SEE_OTHER) + (location) + } +} + +/// The value of one query pair, if present. A hand-edited query names +/// nothing rather than failing the redirect. +fn query_value(query: &str, key: &str) -> Option { + query.split('&').find_map(|pair| { + let (name, value) = pair.split_once('=')?; + (name == key).then(|| value.to_string()) + }) +} + /// The upload control's client half: files under 8 MiB ride the form's own /// multipart post; files at or over it are split into the chunked protocol's /// calls with a progress bar. diff --git a/crates/in-web/src/i18n.rs b/crates/in-web/src/i18n.rs index 429e82f..d0eb7ab 100644 --- a/crates/in-web/src/i18n.rs +++ b/crates/in-web/src/i18n.rs @@ -210,7 +210,9 @@ pub fn t(lang: Lang, key: Key) -> &'static str { UserMenuLabel => "Account", Drive => "Drive", NewFolder => "New folder", + NewFolder => "New folder", FolderName => "Folder name", + NewFolder => "New folder", CreateFolder => "Create folder", RenameFolder => "Rename folder", MoveFolder => "Move folder", @@ -310,7 +312,9 @@ pub fn t(lang: Lang, key: Key) -> &'static str { UserMenuLabel => "Hesap", Drive => "Sürücü", NewFolder => "Yeni klasör", + NewFolder => "Yeni klasör", FolderName => "Klasör adı", + NewFolder => "Yeni klasör", CreateFolder => "Klasörü oluştur", RenameFolder => "Klasörü yeniden adlandır", MoveFolder => "Klasörü taşı", diff --git a/crates/in-web/src/layout.rs b/crates/in-web/src/layout.rs index 2c9235b..60bc52b 100644 --- a/crates/in-web/src/layout.rs +++ b/crates/in-web/src/layout.rs @@ -138,7 +138,6 @@ pub enum NavPage { Drive, Shared, Trash, - Search, Settings, } @@ -146,14 +145,13 @@ impl NavPage { /// Settings is deliberately absent: it lives in the user menu, not the /// page nav — the variant stays so the settings page can still name /// itself active without a link to mark. - const ALL: [Self; 4] = [Self::Drive, Self::Shared, Self::Trash, Self::Search]; + const ALL: [Self; 3] = [Self::Drive, Self::Shared, Self::Trash]; fn href(self) -> &'static str { match self { Self::Drive => "/drive", Self::Shared => "/shared", Self::Trash => "/trash", - Self::Search => "/search", Self::Settings => "/settings", } } @@ -163,7 +161,6 @@ impl NavPage { Self::Drive => Key::NavDrive, Self::Shared => Key::NavShared, Self::Trash => Key::NavTrash, - Self::Search => Key::NavSearch, Self::Settings => Key::NavSettings, } } diff --git a/crates/in-web/src/lib.rs b/crates/in-web/src/lib.rs index 0cb13fc..8013183 100644 --- a/crates/in-web/src/lib.rs +++ b/crates/in-web/src/lib.rs @@ -10,7 +10,6 @@ pub mod i18n; pub mod layout; pub mod live; pub mod pages; -pub mod search; pub mod server; pub mod settings; pub mod share; diff --git a/crates/in-web/src/search.rs b/crates/in-web/src/search.rs deleted file mode 100644 index 5fd4075..0000000 --- a/crates/in-web/src/search.rs +++ /dev/null @@ -1,111 +0,0 @@ -//! Name search over the reader's own drive. -//! -//! `GET /search?q=` matches the query against file and folder names with an -//! owner-scoped `LIKE`, non-trashed rows only. Results link straight to the -//! file view or the containing folder. An empty query renders the empty -//! prompt, not the whole drive. - -use topcoat::Result; -use topcoat::context::Cx; -use topcoat::router::page; -use topcoat::router::request::uri; -use topcoat::view::view; - -use crate::i18n::{Key, lang, t}; -use crate::layout::{NavPage, topbar}; -use crate::server::{app, require_user}; - -/// How many hits each list carries at most. -const LIMIT: u32 = 50; - -/// The search box's text, trimmed. Anything else on the query is ignored. -fn searched(raw: Option<&str>) -> Option { - raw.map(str::trim) - .filter(|query| !query.is_empty()) - .map(str::to_string) -} - -/// The reader's own live files and folders wearing the query in their name. -/// Never another owner's, never trashed, never everything on an empty box. -#[page("/search")] -async fn search(cx: &Cx) -> Result { - let user = match require_user(cx).await { - Ok(user) => user, - Err(refusal) => { - let language = lang(cx).await; - return view! { - cx => -
-

(refusal.message_in(language))

-

(t(language, Key::BackToDrive))

-
- }; - } - }; - let language = lang(cx).await; - let asked = searched(query_value(uri(cx).query().unwrap_or(""), "q").as_deref()); - let hits = match asked.as_deref() { - Some(query) => Some(app(cx).store.search(&user.id, query, LIMIT).await?), - None => None, - }; - let box_text = asked.clone().unwrap_or_default(); - view! { - cx => - (topbar(cx, NavPage::Search, &user, language).await?) -
-

(t(language, Key::SearchResults))

- - if let Some(hits) = hits { - if hits.folders.is_empty() && hits.files.is_empty() { -

(t(language, Key::NoResults))

- } - if !hits.folders.is_empty() { -
-
-

(t(language, Key::FoldersHeading))

- (format!("{}", hits.folders.len())) -
-
- for folder in &hits.folders { -

(folder.name.clone())

- } -
-
- } - if !hits.files.is_empty() { -
-
-

(t(language, Key::FilesHeading))

- (format!("{}", hits.files.len())) -
-
- for file in &hits.files { -

(file.name.clone())

- } -
-
- } - } else { -

(t(language, Key::TypeToSearch))

- } -
- } -} - -/// The value of one query pair, if present. A hand-edited query names -/// nothing rather than failing the page. -fn query_value(query: &str, key: &str) -> Option { - query.split('&').find_map(|pair| { - let (name, value) = pair.split_once('=')?; - (name == key).then(|| value.to_string()) - }) -} diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index bd1d6e2..3049d4f 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -1458,23 +1458,155 @@ async fn search_scopes_to_owner_and_hides_trashed() { let buried = file_id(&app, &ada, "quarterly draft.txt", b"c").await; app.store.delete_file(&buried).await.unwrap(); - // Empty query is the prompt, not the whole drive. - let page = app.get("/search", Some(&admin)).await; + // Empty query is the folder view, not the whole library under a new + // name: the upload control renders, no results caption does. + let page = app.get("/drive", Some(&admin)).await; assert_eq!(page.status, StatusCode::OK, "{}", page.text()); - assert!(!page.text().contains("quarterly report.txt"), "{}", page.text()); + assert!(page.text().contains("id=\"upload-form\""), "{}", page.text()); + assert!(!page.text().contains("Search results"), "{}", page.text()); - let page = app.get("/search?q=quarterly", Some(&admin)).await; + let page = app.get("/drive?q=quarterly", Some(&admin)).await; assert_eq!(page.status, StatusCode::OK, "{}", page.text()); assert!(page.text().contains("quarterly report.txt"), "{}", page.text()); assert!(!page.text().contains("quarterly notes.txt"), "{}", page.text()); assert!(!page.text().contains("quarterly draft.txt"), "{}", page.text()); assert!(app.store.file(&mine).await.unwrap().is_some()); - let page = app.get("/search?q=quarterly", Some(&bob)).await; + let page = app.get("/drive?q=quarterly", Some(&bob)).await; assert!(page.text().contains("quarterly notes.txt"), "{}", page.text()); assert!(!page.text().contains("quarterly report.txt"), "{}", page.text()); } +#[tokio::test] +async fn drive_search_results_replace_the_folder_panels() { + let app = TestApp::build().await; + let cookie = app.sign_in("sub-search-in-place", "inplace@in.test", "InPlace").await; + let owner = owner_of(&app, "sub-search-in-place").await; + let answer = app + .post( + "/api/folder/create", + Some(&cookie), + &[("parent_id", ""), ("name", "holiday photos")], + ) + .await; + assert!(answer.accepted(), "create refused: {:?}", answer.location); + let folder = folder_id(&app, &owner, None, "holiday photos").await; + let file = file_id(&app, &owner, "holiday report.txt", b"sun").await; + + let page = app.get("/drive?q=holiday", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + let body = page.text(); + // Both hits render in place on the drive, wired to their surfaces. + assert!(body.contains("holiday photos"), "{body}"); + assert!(body.contains("holiday report.txt"), "{body}"); + assert!(body.contains(&format!("/drive?folder={folder}")), "{body}"); + assert!(body.contains(&format!("/file/{file}")), "{body}"); + // One chip per list, like the folder panels they replace. + assert_eq!(body.matches("1").count(), 2, "{body}"); + // The caption names the query and the box keeps it. + assert!(body.contains("Search results"), "{body}"); + assert!(body.contains("holiday"), "{body}"); + assert!(body.contains("value=\"holiday\""), "{body}"); + assert!(body.contains("name=\"q\""), "{body}"); + // The folder view is gone: no crumbs, no upload, no row mutations. + assert!(!body.contains("detail-crumbs"), "{body}"); + assert!(!body.contains("id=\"upload-form\""), "{body}"); + assert!(!body.contains("action=\"/api/folder/rename\""), "{body}"); +} + +#[tokio::test] +async fn drive_search_never_shows_other_owners_hits() { + let app = TestApp::build().await; + let ada = app.sign_in("sub-search-ada", "ada2@in.test", "Ada").await; + let bob = app.sign_in("sub-search-bob", "bob2@in.test", "Bob").await; + let bob_id = owner_of(&app, "sub-search-bob").await; + let _ = file_id(&app, &bob_id, "secret ledger.txt", b"b").await; + let answer = app + .post( + "/api/folder/create", + Some(&bob), + &[("parent_id", ""), ("name", "secret drawer")], + ) + .await; + assert!(answer.accepted(), "create refused: {:?}", answer.location); + + // Ada's library-wide search stays hers: Bob's file and folder hits never + // appear, and the quiet note says so. + let page = app.get("/drive?q=secret", Some(&ada)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + let body = page.text(); + assert!(!body.contains("secret ledger.txt"), "{body}"); + assert!(!body.contains("secret drawer"), "{body}"); + assert!(body.contains("Nothing found."), "{body}"); + + // Sanity: the same query finds them for their owner. + let page = app.get("/drive?q=secret", Some(&bob)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + assert!(page.text().contains("secret ledger.txt"), "{}", page.text()); + assert!(page.text().contains("secret drawer"), "{}", page.text()); +} + +#[tokio::test] +async fn drive_search_empty_query_is_the_folder_view() { + let app = TestApp::build().await; + let cookie = app.sign_in("sub-search-empty", "empty@in.test", "Empty").await; + let owner = owner_of(&app, "sub-search-empty").await; + let _ = file_id(&app, &owner, "plain.txt", b"p").await; + + for path in ["/drive?q=", "/drive?q=%20%20"] { + let page = app.get(path, Some(&cookie)).await; + assert_eq!(page.status, StatusCode::OK, "{}: {}", path, page.text()); + let body = page.text(); + // The folder listing renders as usual — the file is there through + // its directory, not through a result — with the box held empty. + assert!(body.contains("plain.txt"), "{path}: {body}"); + assert!(body.contains("id=\"upload-form\""), "{path}: {body}"); + assert!(body.contains("name=\"q\""), "{path}: {body}"); + assert!(!body.contains("Search results"), "{path}: {body}"); + } +} + +#[tokio::test] +async fn old_search_address_redirects_to_drive() { + let app = TestApp::build().await; + let cookie = app.sign_in("sub-search-old", "old@in.test", "Old").await; + + let page = app.get("/search?q=quarterly", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::SEE_OTHER, "{}", page.text()); + assert_eq!(page.location.as_deref(), Some("/drive?q=quarterly")); + + // The raw pair rides through untouched, so encoded queries survive. + let page = app.get("/search?q=holiday+report", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::SEE_OTHER, "{}", page.text()); + assert_eq!(page.location.as_deref(), Some("/drive?q=holiday+report")); + + // No query is the plain drive; no session still redirects, never 401s. + let page = app.get("/search", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::SEE_OTHER, "{}", page.text()); + assert_eq!(page.location.as_deref(), Some("/drive")); + let page = app.get("/search?q=quarterly", None).await; + assert_eq!(page.status, StatusCode::SEE_OTHER, "{}", page.text()); + assert_eq!(page.location.as_deref(), Some("/drive?q=quarterly")); +} + +#[tokio::test] +async fn topbar_nav_has_no_search_link() { + let app = TestApp::build().await; + let cookie = app.sign_in("sub-search-nav", "nav@in.test", "Nav").await; + let page = app.get("/drive", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + let body = page.text(); + let nav_start = body.find("topbar-nav-links").expect("no nav"); + let nav_end = body[nav_start..].find("").expect("nav never closes"); + let nav = &body[nav_start..nav_start + nav_end]; + // Drive, Shared, Trash — and nothing else. + assert!(nav.contains("href=\"/drive\""), "{nav}"); + assert!(nav.contains("href=\"/shared\""), "{nav}"); + assert!(nav.contains("href=\"/trash\""), "{nav}"); + assert!(!nav.contains("/search"), "search leaked into the nav: {nav}"); + assert!(!nav.contains("Search"), "search leaked into the nav: {nav}"); +} + // -- settings --------------------------------------------------------------- #[tokio::test] @@ -2119,7 +2251,7 @@ async fn settings_panels_nest_inside_the_stage() { assert!(body.contains("