1use crate::io::docs::LocalDocuments;
18
19#[macro_use]
20extern crate tracing;
21
22pub mod blocking;
23pub mod experiments;
24pub mod io;
25pub mod macros;
26pub mod model;
27pub mod search;
28pub mod service;
29pub mod subscribers;
30#[cfg(target_family = "wasm")]
31pub mod wasm;
32
33pub const ATTACHMENTS_FOLDERS: bool = false;
36
37#[derive(Clone)]
40pub struct Lb<S = Network, D = AsyncDocs> {
41 pub config: Config,
42 pub user_last_seen: Arc<RwLock<Instant>>,
43 pub user_wake: Arc<Notify>,
44 pub keychain: Keychain,
45 pub entropy: Entropy,
46 pub db: LbDb,
47 pub docs: D,
48 pub client: S,
49 pub events: EventSubs,
50 pub status: StatusUpdater,
51 pub syncer: Syncer,
52 pub attachments_folders: bool,
57 #[cfg(all(unix, not(target_os = "ios")))]
58 pub(crate) ipc: Option<Arc<Ipc>>,
60 #[cfg(not(any(target_family = "wasm", target_os = "ios")))]
61 pub(crate) sync_lock: Option<std::path::PathBuf>,
64}
65
66impl Lb {
67 #[instrument(name = "Lb::init", level = "info", skip_all, err(Debug))]
68 pub async fn init(config: Config) -> LbResult<Self> {
69 logging::init(&config)?;
70
71 let docs = AsyncDocs::from(&config);
72 let path = Path::new(&config.writeable_path);
73 let db =
74 if config.read_only { io::init_read_only(path) } else { io::init_with_migration(path) }
75 .map_err(|err| LbErrKind::Unexpected(format!("{err:#?}")))?;
76 #[cfg(all(unix, not(target_os = "ios")))]
77 let ipc = if config.read_only {
78 None
79 } else {
80 Some(Arc::new(Ipc::new(&db, config.background_work)?))
81 };
82 #[cfg(not(any(target_family = "wasm", target_os = "ios")))]
83 let sync_lock = Some(Path::new(&config.writeable_path).join("sync.lock"));
84 let client = Network { client_type: config.client_type, ..Network::default() };
85
86 Self::assemble(
87 config,
88 db,
89 docs,
90 client,
91 #[cfg(all(unix, not(target_os = "ios")))]
92 ipc,
93 #[cfg(not(any(target_family = "wasm", target_os = "ios")))]
94 sync_lock,
95 )
96 .await
97 }
98}
99
100impl<S: Server, D: LocalDocuments> Lb<S, D> {
101 pub async fn in_memory(config: Config, server: S, docs: D) -> LbResult<Self> {
105 logging::init(&config)?;
106
107 let db = io::CoreDb::init(&db_rs::config::Config::in_memory())?;
108
109 Self::assemble(
110 config,
111 db,
112 docs,
113 server,
114 #[cfg(all(unix, not(target_os = "ios")))]
115 None,
116 #[cfg(not(any(target_family = "wasm", target_os = "ios")))]
117 None,
118 )
119 .await
120 }
121
122 async fn assemble(
123 config: Config, db: io::CoreDb, docs: D, client: S,
124 #[cfg(all(unix, not(target_os = "ios")))] ipc: Option<Arc<Ipc>>,
125 #[cfg(not(any(target_family = "wasm", target_os = "ios")))] sync_lock: Option<
126 std::path::PathBuf,
127 >,
128 ) -> LbResult<Self> {
129 let keychain = Keychain::from(db.schema.account.as_ref());
130 let entropy = Entropy::new(&config);
131 let db = Arc::new(RwLock::new(db));
132
133 let status = StatusUpdater::default();
134 let syncer = Default::default();
135 let events = EventSubs::default();
136 let user_last_seen = Arc::new(RwLock::new(Instant::now()));
137 let user_wake = Arc::new(Notify::new());
138
139 let result = Self {
140 config,
141 keychain,
142 entropy,
143 db,
144 docs,
145 client,
146 syncer,
147 attachments_folders: ATTACHMENTS_FOLDERS,
148 events,
149 status,
150 user_last_seen,
151 user_wake,
152 #[cfg(all(unix, not(target_os = "ios")))]
153 ipc,
154 #[cfg(not(any(target_family = "wasm", target_os = "ios")))]
155 sync_lock,
156 };
157
158 #[cfg(all(unix, not(target_os = "ios")))]
159 result.setup_ipc().await?;
160
161 #[cfg(not(target_family = "wasm"))]
162 if !result.config.read_only {
163 result.setup_syncer();
164 result.setup_status().await?;
165 }
166
167 Ok(result)
168 }
169}
170
171pub fn get_code_version() -> &'static str {
172 env!("CARGO_PKG_VERSION")
173}
174
175pub fn get_code_commit() -> Option<&'static str> {
176 Some(env!("LB_GIT_COMMIT")).filter(|commit| *commit != "unknown")
177}
178
179pub static DEFAULT_API_LOCATION: &str = "https://app.lockbook.net";
180pub static CORE_CODE_VERSION: &str = env!("CARGO_PKG_VERSION");
181
182#[cfg(all(unix, not(target_os = "ios")))]
183use crate::subscribers::ipc::Ipc;
184use crate::subscribers::syncer::Syncer;
185
186use crate::service::logging;
187use db_rs::View;
188use io::LbDb;
189use io::docs::AsyncDocs;
190use io::network::Network;
191use io::server::Server;
192use model::core_config::Config;
193use model::entropy::Entropy;
194pub use model::errors::{LbErrKind, LbResult};
195use service::events::EventSubs;
196use service::keychain::Keychain;
197use std::path::Path;
198use std::sync::Arc;
199use subscribers::status::StatusUpdater;
200use tokio::sync::{Notify, RwLock};
201pub use uuid::Uuid;
202use web_time::Instant;