Skip to main content

lb_rs/service/
path.rs

1use crate::Lb;
2use crate::model::errors::{LbErrKind, LbResult};
3use crate::model::file::File;
4use crate::model::path_ops::Filter;
5use crate::model::tree_like::TreeLike;
6use crate::service::events::Actor;
7use uuid::Uuid;
8
9impl Lb {
10    #[instrument(level = "debug", skip(self), err(Debug))]
11    pub async fn create_link_at_path(&self, path: &str, target_id: Uuid) -> LbResult<File> {
12        let mut tx = self.begin_tx().await;
13        let db = tx.db();
14
15        let mut tree = (&db.base_metadata)
16            .to_staged(&mut db.local_metadata)
17            .to_lazy();
18
19        let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
20
21        let id = tree.create_link_at_path(path, target_id, root, &self.keychain)?;
22
23        let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
24
25        self.events.meta_changed(Actor::User);
26
27        Ok(ui_file)
28    }
29
30    #[instrument(level = "debug", skip(self), err(Debug))]
31    pub async fn create_at_path(&self, path: &str) -> LbResult<File> {
32        let mut tx = self.begin_tx().await;
33        let db = tx.db();
34
35        let mut tree = (&db.base_metadata)
36            .to_staged(&mut db.local_metadata)
37            .to_lazy();
38
39        let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
40
41        let id = tree.create_at_path(path, root, &self.keychain)?;
42
43        let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
44
45        self.events.meta_changed(Actor::User);
46
47        Ok(ui_file)
48    }
49
50    #[instrument(level = "debug", skip(self), err(Debug))]
51    pub async fn get_by_path(&self, path: &str) -> LbResult<File> {
52        let tx = self.ro_tx().await;
53        let db = tx.db();
54
55        let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
56
57        let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
58
59        let id = tree.path_to_id(path, root, &self.keychain)?;
60
61        let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
62
63        Ok(ui_file)
64    }
65
66    #[instrument(level = "debug", skip(self), err(Debug))]
67    pub async fn get_path_by_id(&self, id: Uuid) -> LbResult<String> {
68        let tx = self.ro_tx().await;
69        let db = tx.db();
70
71        let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
72        let path = tree.id_to_path(&id, &self.keychain)?;
73
74        Ok(path)
75    }
76
77    #[instrument(level = "debug", skip(self), err(Debug))]
78    pub async fn list_paths(&self, filter: Option<Filter>) -> LbResult<Vec<String>> {
79        Ok(self
80            .list_paths_with_ids(filter)
81            .await?
82            .into_iter()
83            .map(|(_, path)| path)
84            .collect())
85    }
86
87    #[instrument(level = "debug", skip(self), err(Debug))]
88    pub async fn list_paths_with_ids(
89        &self, filter: Option<Filter>,
90    ) -> LbResult<Vec<(Uuid, String)>> {
91        let tx = self.ro_tx().await;
92        let db = tx.db();
93
94        let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
95        let paths = tree.list_paths(filter, &self.keychain)?;
96
97        Ok(paths)
98    }
99}