1use std::collections::HashMap;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5use tokio::runtime::Runtime;
6use tokio::sync::broadcast::Receiver;
7use uuid::Uuid;
8
9use crate::model::account::{Account, Username};
10use crate::model::api::{
11 AccountFilter, AccountIdentifier, AccountInfo, AdminFileInfoResponse, AdminSetUserTierInfo,
12 AdminValidateAccount, AdminValidateServer, ServerIndex, StripeAccountTier, SubscriptionInfo,
13};
14use crate::model::core_config::Config;
15use crate::model::crypto::DecryptedDocument;
16use crate::model::errors::{LbResult, Warning};
17use crate::model::file::{File, ShareMode};
18use crate::model::file_metadata::{DocumentHmac, FileType};
19use crate::model::path_ops::Filter;
20use crate::service::activity::RankingWeights;
21use crate::service::debug::DebugInfo;
22use crate::service::events::Event;
23use crate::service::import_export::{ExportFileInfo, ImportStatus};
24use crate::service::sync::{SyncProgress, SyncStatus};
25use crate::service::usage::{UsageItemMetric, UsageMetrics};
26use crate::subscribers::search::{SearchConfig, SearchResult};
27use crate::subscribers::status::Status;
28
29#[derive(Clone)]
30pub struct Lb {
31 lb: crate::Lb,
32 rt: Arc<Runtime>,
33}
34
35impl Lb {
36 pub fn init(config: Config) -> LbResult<Self> {
37 let rt = Arc::new(Runtime::new().unwrap());
38 let lb = rt.block_on(crate::Lb::init(config))?;
39 Ok(Self { rt, lb })
40 }
41
42 pub fn create_account(
43 &self, username: &str, api_url: &str, welcome_doc: bool,
44 ) -> LbResult<Account> {
45 self.rt
46 .block_on(self.lb.create_account(username, api_url, welcome_doc))
47 }
48
49 pub fn import_account(&self, key: &str, api_url: Option<&str>) -> LbResult<Account> {
50 self.rt.block_on(self.lb.import_account(key, api_url))
51 }
52
53 pub fn export_account_private_key(&self) -> LbResult<String> {
54 self.lb.export_account_private_key_v1()
55 }
56
57 pub fn export_account_phrase(&self) -> LbResult<String> {
58 self.lb.export_account_phrase()
59 }
60
61 pub fn export_account_qr(&self) -> LbResult<Vec<u8>> {
62 self.lb.export_account_qr()
63 }
64
65 pub fn get_account(&self) -> LbResult<&Account> {
66 self.lb.get_account()
67 }
68
69 pub fn get_config(&self) -> Config {
70 self.lb.config.clone()
71 }
72
73 pub fn create_file(&self, name: &str, parent: &Uuid, file_type: FileType) -> LbResult<File> {
74 self.rt
75 .block_on(self.lb.create_file(name, parent, file_type))
76 }
77
78 pub fn safe_write(
79 &self, id: Uuid, old_hmac: Option<DocumentHmac>, content: Vec<u8>,
80 ) -> LbResult<DocumentHmac> {
81 self.rt.block_on(self.lb.safe_write(id, old_hmac, content))
82 }
83
84 pub fn write_document(&self, id: Uuid, content: &[u8]) -> LbResult<()> {
85 self.rt.block_on(self.lb.write_document(id, content))
86 }
87
88 pub fn get_root(&self) -> LbResult<File> {
89 self.rt.block_on(self.lb.root())
90 }
91
92 pub fn get_children(&self, id: &Uuid) -> LbResult<Vec<File>> {
93 self.rt.block_on(self.lb.get_children(id))
94 }
95
96 pub fn get_and_get_children_recursively(&self, id: &Uuid) -> LbResult<Vec<File>> {
97 self.rt
98 .block_on(self.lb.get_and_get_children_recursively(id))
99 }
100
101 pub fn get_file_by_id(&self, id: Uuid) -> LbResult<File> {
102 self.rt.block_on(self.lb.get_file_by_id(id))
103 }
104
105 pub fn delete_file(&self, id: &Uuid) -> LbResult<()> {
106 self.rt.block_on(self.lb.delete(id))
107 }
108
109 pub fn read_document(&self, id: Uuid, user_activity: bool) -> LbResult<DecryptedDocument> {
110 self.rt.block_on(self.lb.read_document(id, user_activity))
111 }
112
113 pub fn read_document_with_hmac(
114 &self, id: Uuid, user_activity: bool,
115 ) -> LbResult<(Option<DocumentHmac>, DecryptedDocument)> {
116 self.rt
117 .block_on(self.lb.read_document_with_hmac(id, user_activity))
118 }
119
120 pub fn list_metadatas(&self) -> LbResult<Vec<File>> {
121 self.rt.block_on(self.lb.list_metadatas())
122 }
123
124 pub fn rename_file(&self, id: &Uuid, new_name: &str) -> LbResult<()> {
125 self.rt.block_on(self.lb.rename_file(id, new_name))
126 }
127
128 pub fn move_file(&self, id: &Uuid, new_parent: &Uuid) -> LbResult<()> {
129 self.rt.block_on(self.lb.move_file(id, new_parent))
130 }
131
132 pub fn share_file(&self, id: Uuid, username: &str, mode: ShareMode) -> LbResult<()> {
133 self.rt.block_on(self.lb.share_file(id, username, mode))
134 }
135
136 pub fn get_pending_shares(&self) -> LbResult<Vec<File>> {
137 self.rt.block_on(self.lb.get_pending_shares())
138 }
139
140 pub fn get_pending_share_files(&self) -> LbResult<Vec<File>> {
141 self.rt.block_on(self.lb.get_pending_share_files())
142 }
143
144 pub fn delete_pending_share(&self, id: &Uuid) -> LbResult<()> {
145 self.rt.block_on(async { self.lb.reject_share(id).await })
146 }
147
148 pub fn create_link_at_path(&self, path_and_name: &str, target_id: Uuid) -> LbResult<File> {
149 self.rt
150 .block_on(self.lb.create_link_at_path(path_and_name, target_id))
151 }
152
153 pub fn create_at_path(&self, path_and_name: &str) -> LbResult<File> {
154 self.rt.block_on(self.lb.create_at_path(path_and_name))
155 }
156
157 pub fn get_by_path(&self, path: &str) -> LbResult<File> {
158 self.rt.block_on(self.lb.get_by_path(path))
159 }
160
161 pub fn get_path_by_id(&self, id: Uuid) -> LbResult<String> {
162 self.rt.block_on(self.lb.get_path_by_id(id))
163 }
164
165 pub fn list_paths(&self, filter: Option<Filter>) -> LbResult<Vec<String>> {
166 self.rt.block_on(self.lb.list_paths(filter))
167 }
168
169 pub fn get_local_changes(&self) -> LbResult<Vec<Uuid>> {
170 Ok(self.rt.block_on(self.lb.local_changes()))
171 }
172
173 pub fn calculate_work(&self) -> LbResult<SyncStatus> {
174 self.rt.block_on(self.lb.calculate_work())
175 }
176
177 pub fn sync(&self, f: Option<Box<dyn Fn(SyncProgress) + Send>>) -> LbResult<SyncStatus> {
178 self.rt.block_on(self.lb.sync(f))
179 }
180
181 pub fn get_last_synced(&self) -> LbResult<i64> {
182 self.rt.block_on(async {
183 let tx = self.lb.ro_tx().await;
184 let db = tx.db();
185 Ok(db.last_synced.get().copied().unwrap_or(0))
186 })
187 }
188
189 pub fn get_last_synced_human_string(&self) -> LbResult<String> {
190 self.rt.block_on(self.lb.get_last_synced_human())
191 }
192
193 pub fn get_timestamp_human_string(&self, timestamp: i64) -> String {
194 self.lb.get_timestamp_human_string(timestamp)
195 }
196
197 pub fn suggested_docs(&self, settings: RankingWeights) -> LbResult<Vec<Uuid>> {
198 self.rt.block_on(self.lb.suggested_docs(settings))
199 }
200
201 pub fn clear_suggested(&self) -> LbResult<()> {
202 self.rt.block_on(self.lb.clear_suggested())
203 }
204
205 pub fn clear_suggested_id(&self, target_id: Uuid) -> LbResult<()> {
206 self.rt.block_on(self.lb.clear_suggested_id(target_id))
207 }
208
209 pub fn get_usage(&self) -> LbResult<UsageMetrics> {
211 self.rt.block_on(self.lb.get_usage())
212 }
213
214 pub fn get_uncompressed_usage_breakdown(&self) -> LbResult<HashMap<Uuid, usize>> {
215 self.rt.block_on(self.lb.get_uncompressed_usage_breakdown())
216 }
217
218 pub fn get_uncompressed_usage(&self) -> LbResult<UsageItemMetric> {
219 self.rt.block_on(self.lb.get_uncompressed_usage())
220 }
221
222 pub fn import_files<F: Fn(ImportStatus)>(
223 &self, sources: &[PathBuf], dest: Uuid, update_status: &F,
224 ) -> LbResult<()> {
225 self.rt
226 .block_on(self.lb.import_files(sources, dest, update_status))
227 }
228
229 pub fn export_files(
230 &self, id: Uuid, dest: PathBuf, edit: bool,
231 export_progress: &Option<Box<dyn Fn(ExportFileInfo)>>,
232 ) -> LbResult<()> {
233 self.rt
234 .block_on(self.lb.export_file(id, dest, edit, export_progress))
235 }
236
237 pub fn search_file_paths(&self, input: &str) -> LbResult<Vec<SearchResult>> {
238 self.rt
239 .block_on(async { self.lb.search(input, SearchConfig::Paths).await })
240 }
241
242 pub fn search(&self, input: &str, cfg: SearchConfig) -> LbResult<Vec<SearchResult>> {
243 self.rt.block_on(self.lb.search(input, cfg))
244 }
245
246 pub fn validate(&self) -> LbResult<Vec<Warning>> {
247 self.rt.block_on(self.lb.test_repo_integrity())
248 }
249
250 pub fn upgrade_account_stripe(&self, account_tier: StripeAccountTier) -> LbResult<()> {
251 self.rt
252 .block_on(self.lb.upgrade_account_stripe(account_tier))
253 }
254
255 pub fn upgrade_account_google_play(
256 &self, purchase_token: &str, account_id: &str,
257 ) -> LbResult<()> {
258 self.rt.block_on(
259 self.lb
260 .upgrade_account_google_play(purchase_token, account_id),
261 )
262 }
263
264 pub fn upgrade_account_app_store(
265 &self, original_transaction_id: String, app_account_token: String,
266 ) -> LbResult<()> {
267 self.rt.block_on(
268 self.lb
269 .upgrade_account_app_store(original_transaction_id, app_account_token),
270 )
271 }
272
273 pub fn cancel_subscription(&self) -> LbResult<()> {
274 self.rt.block_on(self.lb.cancel_subscription())
275 }
276
277 pub fn get_subscription_info(&self) -> LbResult<Option<SubscriptionInfo>> {
278 self.rt.block_on(self.lb.get_subscription_info())
279 }
280
281 pub fn delete_account(&self) -> LbResult<()> {
282 self.rt.block_on(self.lb.delete_account())
283 }
284
285 pub fn admin_disappear_account(&self, username: &str) -> LbResult<()> {
286 self.rt.block_on(self.lb.disappear_account(username))
287 }
288
289 pub fn admin_disappear_file(&self, id: Uuid) -> LbResult<()> {
290 self.rt.block_on(self.lb.disappear_file(id))
291 }
292
293 pub fn admin_list_users(&self, filter: Option<AccountFilter>) -> LbResult<Vec<Username>> {
294 self.rt.block_on(self.lb.list_users(filter))
295 }
296
297 pub fn admin_get_account_info(&self, identifier: AccountIdentifier) -> LbResult<AccountInfo> {
298 self.rt.block_on(self.lb.get_account_info(identifier))
299 }
300
301 pub fn admin_validate_account(&self, username: &str) -> LbResult<AdminValidateAccount> {
302 self.rt.block_on(self.lb.validate_account(username))
303 }
304
305 pub fn admin_validate_server(&self) -> LbResult<AdminValidateServer> {
306 self.rt.block_on(self.lb.validate_server())
307 }
308
309 pub fn admin_file_info(&self, id: Uuid) -> LbResult<AdminFileInfoResponse> {
310 self.rt.block_on(self.lb.file_info(id))
311 }
312
313 pub fn admin_rebuild_index(&self, index: ServerIndex) -> LbResult<()> {
314 self.rt.block_on(self.lb.rebuild_index(index))
315 }
316
317 pub fn admin_set_user_tier(&self, username: &str, info: AdminSetUserTierInfo) -> LbResult<()> {
318 self.rt.block_on(self.lb.set_user_tier(username, info))
319 }
320
321 pub fn subscribe(&self) -> Receiver<Event> {
322 self.lb.subscribe()
323 }
324
325 pub fn status(&self) -> Status {
326 self.rt.block_on(self.lb.status())
327 }
328
329 pub fn debug_info(&self, os_info: String) -> LbResult<DebugInfo> {
330 self.rt.block_on(self.lb.debug_info(os_info))
331 }
332
333 pub fn recent_panic(&self) -> LbResult<bool> {
334 self.rt.block_on(self.lb.recent_panic())
335 }
336
337 pub fn write_panic_to_file(&self, error_header: String, bt: String) -> LbResult<String> {
338 self.rt
339 .block_on(self.lb.write_panic_to_file(error_header, bt))
340 }
341}