syncweb_core/net/network_context.rs
1use iroh_blobs::Hash;
2use iroh_docs::NamespaceId;
3
4use crate::{Result, net::NetworkManager, storage::node_db::NodeDatabase};
5
6/// Access control context for network-scoped blob and folder operations.
7///
8/// Wraps a `NetworkManager` and provides methods to verify that the local
9/// node can access specific blobs or folders through its network memberships.
10#[derive(Clone, Debug)]
11pub struct NetworkContext {
12 database: NodeDatabase,
13 local_node: String,
14}
15
16impl NetworkContext {
17 /// Create a new network context from a `NetworkManager`.
18 #[must_use]
19 pub fn from_manager(manager: &NetworkManager) -> Self {
20 let local_node = manager.local_node().to_string();
21 Self {
22 database: manager.database().clone(),
23 local_node,
24 }
25 }
26
27 /// Create a new network context directly from a database and local node ID.
28 #[must_use]
29 pub fn new(database: NodeDatabase, local_node: &str) -> Self {
30 Self {
31 database,
32 local_node: local_node.to_owned(),
33 }
34 }
35
36 /// Returns true if the local node can access the given folder namespace
37 /// through at least one network membership.
38 ///
39 /// # Errors
40 ///
41 /// Returns an error if the database query fails.
42 pub fn can_access_folder(&self, namespace_id: &NamespaceId) -> Result<bool> {
43 self.database
44 .can_access_folder(&namespace_id.to_string(), &self.local_node)
45 }
46
47 /// Returns true if the local node can access the given blob through at
48 /// least one folder in a network the node belongs to.
49 ///
50 /// Uses the `blob_folders` reverse index for O(1) lookup.
51 ///
52 /// # Errors
53 ///
54 /// Returns an error if the database query fails.
55 pub fn can_access_blob(&self, hash: &Hash) -> Result<bool> {
56 self.database.can_access_blob(hash, &self.local_node)
57 }
58
59 /// List all network IDs (as strings) that contain the given folder.
60 ///
61 /// # Errors
62 ///
63 /// Returns an error if the database query fails.
64 pub fn networks_for_folder(&self, namespace_id: &NamespaceId) -> Result<Vec<String>> {
65 self.database.networks_for_folder(&namespace_id.to_string())
66 }
67}