mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-04 16:56:59 +01:00
prompt refactor
This commit is contained in:
1 parent
39ab4aae48
commit
ea56818a80
5 files changed
+161
-2
No files matched your search
@@ -160,6 +160,40 @@ impl Db {
|
||||
Ok(count > 0)
|
||||
}
|
||||
|
||||
/// Resolve `uid` relative to an ancestor node.
|
||||
///
|
||||
/// This is used when a remote subtree has its own local sync mount: the
|
||||
/// sync folder stores the ancestor UID, while search results identify the
|
||||
/// selected descendant. Returning `None` for an unrelated or incomplete
|
||||
/// chain keeps callers from accidentally joining a Drive-wide path onto the
|
||||
/// wrong mountpoint. The ancestor itself resolves to the empty path.
|
||||
pub fn path_relative_to(&self, ancestor_uid: &str, uid: &str) -> Result<Option<String>> {
|
||||
let conn = self.conn.lock();
|
||||
let mut stmt = conn.prepare(
|
||||
"WITH RECURSIVE anc(uid, parent_uid, name, depth) AS (
|
||||
SELECT uid, parent_uid, name, 0 FROM nodes WHERE uid = ?1
|
||||
UNION ALL
|
||||
SELECT n.uid, n.parent_uid, n.name, anc.depth + 1
|
||||
FROM nodes n JOIN anc ON n.uid = anc.parent_uid
|
||||
WHERE anc.depth < 1024
|
||||
)
|
||||
SELECT uid, name FROM anc ORDER BY depth",
|
||||
)?;
|
||||
let rows = stmt.query_map(params![uid], |row| {
|
||||
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
|
||||
})?;
|
||||
let mut parts = Vec::new();
|
||||
for row in rows {
|
||||
let (current_uid, name) = row?;
|
||||
if current_uid == ancestor_uid {
|
||||
parts.reverse();
|
||||
return Ok(Some(parts.join("/")));
|
||||
}
|
||||
parts.push(name);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Full-text search over node names, newest schema's trigram index giving
|
||||
/// substring (not just prefix) matches. Returns up to `limit` non-trashed
|
||||
/// hits, each with its mountpoint-relative path resolved. Queries shorter
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::control::{ActivityEntry, ActivityKind};
|
||||
use crate::localindex::LocalEntry;
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
use proton_drive_rs::{Node, NodeKind};
|
||||
use serde_json::json;
|
||||
|
||||
/// `Db::open` applies the pragmas that `open_in_memory` skips, so the WAL
|
||||
/// settings can only be checked against a real file.
|
||||
@@ -1181,6 +1182,96 @@ fn pending_mode_is_queued_until_the_mode_is_reached() {
|
||||
assert_eq!(folder.pending_mode, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_path_resolves_relative_to_a_sync_root_uid() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let root = node_from(serde_json::Value::Null, "root", "My Files", json!("Folder"));
|
||||
let videos = node_from(json!(uid("root")), "videos", "Videos", json!("Folder"));
|
||||
let anime = node_from(json!(uid("videos")), "anime", "anime", json!("Folder"));
|
||||
let show = node_from(json!(uid("anime")), "show", "Oshi no Ko", json!("Folder"));
|
||||
let episode = node_from(
|
||||
json!(uid("show")),
|
||||
"episode",
|
||||
"episode 01.mkv",
|
||||
json!({"File": {
|
||||
"media_type": "video/x-matroska",
|
||||
"total_size_on_storage": 42,
|
||||
"claimed_size": 42,
|
||||
"claimed_modification_time": null
|
||||
}}),
|
||||
);
|
||||
db.upsert_nodes(&[root, videos, anime, show, episode])
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
db.path_relative_to(&uid("videos").to_string(), &uid("episode").to_string())
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("anime/Oshi no Ko/episode 01.mkv")
|
||||
);
|
||||
assert_eq!(
|
||||
db.path_relative_to(&uid("videos").to_string(), &uid("videos").to_string())
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("")
|
||||
);
|
||||
assert_eq!(
|
||||
db.path_relative_to(&uid("anime").to_string(), &uid("episode").to_string())
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some("Oshi no Ko/episode 01.mkv")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_path_rejects_unrelated_missing_and_broken_ancestor_chains() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let root = node_from(serde_json::Value::Null, "root", "My Files", json!("Folder"));
|
||||
let videos = node_from(json!(uid("root")), "videos", "Videos", json!("Folder"));
|
||||
let episode = node_from(
|
||||
json!(uid("videos")),
|
||||
"episode",
|
||||
"episode.mkv",
|
||||
json!({"File": {
|
||||
"media_type": "video/x-matroska",
|
||||
"total_size_on_storage": 42,
|
||||
"claimed_size": 42,
|
||||
"claimed_modification_time": null
|
||||
}}),
|
||||
);
|
||||
let unrelated = node_from(
|
||||
json!(uid("root")),
|
||||
"documents",
|
||||
"Documents",
|
||||
json!("Folder"),
|
||||
);
|
||||
let orphan = node_from(
|
||||
json!(uid("missing-parent")),
|
||||
"orphan",
|
||||
"orphan.mkv",
|
||||
json!({"File": {
|
||||
"media_type": "video/x-matroska",
|
||||
"total_size_on_storage": 42,
|
||||
"claimed_size": 42,
|
||||
"claimed_modification_time": null
|
||||
}}),
|
||||
);
|
||||
db.upsert_nodes(&[root, videos, episode, unrelated, orphan])
|
||||
.unwrap();
|
||||
|
||||
for (ancestor, descendant) in [
|
||||
("documents", "episode"),
|
||||
("videos", "missing-node"),
|
||||
("videos", "orphan"),
|
||||
] {
|
||||
assert_eq!(
|
||||
db.path_relative_to(&uid(ancestor).to_string(), &uid(descendant).to_string())
|
||||
.unwrap(),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_index_touch_access_and_lru_order() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
|
||||
Reference in new issue
Block a user