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