Skip to main content

lb_rs/
blocking.rs

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 create_account(
68        &self, username: &str, api_url: &str, welcome_doc: bool,
69    ) -> LbResult<Account> {
70        self.block_on(self.lb.create_account(username, api_url, welcome_doc))
71    }
72
73    pub fn import_account(&self, key: &str, api_url: Option<&str>) -> LbResult<Account> {
74        self.block_on(self.lb.import_account(key, api_url))
75    }
76
77    pub fn export_account_private_key(&self) -> LbResult<String> {
78        self.lb.export_account_private_key()
79    }
80
81    pub fn export_account_phrase(&self) -> LbResult<String> {
82        self.lb.export_account_phrase()
83    }
84
85    pub fn export_account_qr(&self) -> LbResult<Vec<u8>> {
86        self.lb.export_account_qr()
87    }
88
89    pub fn get_account(&self) -> LbResult<Account> {
90        self.lb.get_account()
91    }
92
93    pub fn get_config(&self) -> Config {
94        self.lb.config().clone()
95    }
96
97    pub fn create_file(&self, name: &str, parent: &Uuid, file_type: FileType) -> LbResult<File> {
98        self.block_on(self.lb.create_file(name, parent, file_type))
99    }
100
101    pub fn safe_write(
102        &self, id: Uuid, old_hmac: Option<DocumentHmac>, content: Vec<u8>,
103    ) -> LbResult<DocumentHmac> {
104        self.block_on(self.lb.safe_write(id, old_hmac, content))
105    }
106
107    pub fn write_document(&self, id: Uuid, content: &[u8]) -> LbResult<()> {
108        self.block_on(self.lb.write_document(id, content))
109    }
110
111    pub fn get_root(&self) -> LbResult<File> {
112        self.block_on(self.lb.root())
113    }
114
115    pub fn get_children(&self, id: &Uuid) -> LbResult<Vec<File>> {
116        self.block_on(self.lb.get_children(id))
117    }
118
119    pub fn get_and_get_children_recursively(&self, id: &Uuid) -> LbResult<Vec<File>> {
120        self.block_on(self.lb.get_and_get_children_recursively(id))
121    }
122
123    pub fn get_file_by_id(&self, id: Uuid) -> LbResult<File> {
124        self.block_on(self.lb.get_file_by_id(id))
125    }
126
127    pub fn delete_file(&self, id: &Uuid) -> LbResult<()> {
128        self.block_on(self.lb.delete(id))
129    }
130
131    pub fn read_document(&self, id: Uuid, user_activity: bool) -> LbResult<DecryptedDocument> {
132        self.block_on(self.lb.read_document(id, user_activity))
133    }
134
135    pub fn read_document_with_hmac(
136        &self, id: Uuid, user_activity: bool,
137    ) -> LbResult<(Option<DocumentHmac>, DecryptedDocument)> {
138        self.block_on(self.lb.read_document_with_hmac(id, user_activity))
139    }
140
141    pub fn list_metadatas(&self) -> LbResult<Vec<File>> {
142        self.block_on(self.lb.list_metadatas())
143    }
144
145    pub fn rename_file(&self, id: &Uuid, new_name: &str) -> LbResult<()> {
146        self.block_on(self.lb.rename_file(id, new_name))
147    }
148
149    pub fn move_file(&self, id: &Uuid, new_parent: &Uuid) -> LbResult<()> {
150        self.block_on(self.lb.move_file(id, new_parent))
151    }
152
153    pub fn share_file(&self, id: Uuid, username: &str, mode: ShareMode) -> LbResult<()> {
154        self.block_on(self.lb.share_file(id, username, mode))
155    }
156
157    pub fn get_pending_shares(&self) -> LbResult<Vec<File>> {
158        self.block_on(self.lb.get_pending_shares())
159    }
160
161    pub fn get_pending_share_files(&self) -> LbResult<Vec<File>> {
162        self.block_on(self.lb.get_pending_share_files())
163    }
164
165    pub fn delete_pending_share(&self, id: &Uuid) -> LbResult<()> {
166        self.block_on(async { self.lb.reject_share(id).await })
167    }
168
169    pub fn create_link_at_path(&self, path_and_name: &str, target_id: Uuid) -> LbResult<File> {
170        self.block_on(self.lb.create_link_at_path(path_and_name, target_id))
171    }
172
173    pub fn create_at_path(&self, path_and_name: &str) -> LbResult<File> {
174        self.block_on(self.lb.create_at_path(path_and_name))
175    }
176
177    pub fn get_by_path(&self, path: &str) -> LbResult<File> {
178        self.block_on(self.lb.get_by_path(path))
179    }
180
181    pub fn get_path_by_id(&self, id: Uuid) -> LbResult<String> {
182        self.block_on(self.lb.get_path_by_id(id))
183    }
184
185    pub fn list_paths(&self, filter: Option<Filter>) -> LbResult<Vec<String>> {
186        self.block_on(self.lb.list_paths(filter))
187    }
188
189    pub fn list_paths_with_ids(&self, filter: Option<Filter>) -> LbResult<Vec<(Uuid, String)>> {
190        self.block_on(self.lb.list_paths_with_ids(filter))
191    }
192
193    pub fn get_local_changes(&self) -> LbResult<Vec<Uuid>> {
194        Ok(self.block_on(self.lb.local_changes()))
195    }
196
197    pub fn sync(&self) -> LbResult<()> {
198        self.block_on(self.lb.sync())
199    }
200
201    pub fn get_last_synced(&self) -> LbResult<i64> {
202        self.block_on(self.lb.get_last_synced())
203    }
204
205    pub fn get_last_synced_human_string(&self) -> LbResult<String> {
206        self.block_on(self.lb.get_last_synced_human())
207    }
208
209    pub fn get_timestamp_human_string(&self, timestamp: i64) -> String {
210        self.lb.get_timestamp_human_string(timestamp)
211    }
212
213    pub fn suggested_docs(&self, settings: RankingWeights) -> LbResult<Vec<Uuid>> {
214        self.block_on(self.lb.suggested_docs(settings))
215    }
216
217    pub fn clear_suggested(&self) -> LbResult<()> {
218        self.block_on(self.lb.clear_suggested())
219    }
220
221    pub fn clear_suggested_id(&self, target_id: Uuid) -> LbResult<()> {
222        self.block_on(self.lb.clear_suggested_id(target_id))
223    }
224
225    pub fn pin_file(&self, id: Uuid) -> LbResult<()> {
226        self.block_on(self.lb.pin_file(id))
227    }
228
229    pub fn unpin_file(&self, id: Uuid) -> LbResult<()> {
230        self.block_on(self.lb.unpin_file(id))
231    }
232
233    pub fn list_pinned(&self) -> LbResult<Vec<Uuid>> {
234        self.block_on(self.lb.list_pinned())
235    }
236
237    // TODO: examine why the old get_usage does a bunch of things
238    pub fn get_usage(&self) -> LbResult<UsageMetrics> {
239        self.block_on(self.lb.get_usage())
240    }
241
242    pub fn import_files<F: Fn(ImportStatus)>(
243        &self, sources: &[PathBuf], dest: Uuid, update_status: &F,
244    ) -> LbResult<()> {
245        self.block_on(self.lb.import_files(sources, dest, update_status))
246    }
247
248    pub fn export_files(
249        &self, id: Uuid, dest: PathBuf, edit: bool,
250        export_progress: &Option<Box<dyn Fn(ExportFileInfo)>>,
251    ) -> LbResult<()> {
252        self.block_on(self.lb.export_file(id, dest, edit, export_progress))
253    }
254
255    pub fn get_file_link_url(&self, id: Uuid) -> LbResult<String> {
256        self.block_on(self.lb.get_file_link_url(id))
257    }
258
259    pub fn path_searcher(&self) -> crate::search::PathSearcher {
260        self.block_on(self.lb.path_searcher())
261    }
262
263    pub fn content_searcher(&self) -> crate::search::ContentSearcher {
264        crate::search::ContentSearcher::new(self)
265    }
266
267    pub fn validate(&self) -> LbResult<Vec<Warning>> {
268        self.block_on(self.lb.test_repo_integrity(true))
269    }
270
271    pub fn upgrade_account_stripe(&self, account_tier: StripeAccountTier) -> LbResult<()> {
272        self.block_on(self.lb.upgrade_account_stripe(account_tier))
273    }
274
275    pub fn upgrade_account_google_play(
276        &self, purchase_token: &str, account_id: &str,
277    ) -> LbResult<()> {
278        self.block_on(
279            self.lb
280                .upgrade_account_google_play(purchase_token, account_id),
281        )
282    }
283
284    pub fn upgrade_account_app_store(
285        &self, original_transaction_id: String, app_account_token: String,
286    ) -> LbResult<()> {
287        self.block_on(
288            self.lb
289                .upgrade_account_app_store(original_transaction_id, app_account_token),
290        )
291    }
292
293    pub fn cancel_subscription(&self) -> LbResult<()> {
294        self.block_on(self.lb.cancel_subscription())
295    }
296
297    pub fn get_subscription_info(&self) -> LbResult<Option<SubscriptionInfo>> {
298        self.block_on(self.lb.get_subscription_info())
299    }
300
301    pub fn delete_account(&self) -> LbResult<()> {
302        self.block_on(self.lb.delete_account())
303    }
304
305    pub fn admin_disappear_account(&self, username: &str) -> LbResult<()> {
306        self.block_on(self.lb.disappear_account(username))
307    }
308
309    pub fn admin_disappear_file(&self, id: Uuid) -> LbResult<()> {
310        self.block_on(self.lb.disappear_file(id))
311    }
312
313    pub fn admin_list_users(&self, filter: Option<AccountFilter>) -> LbResult<Vec<Username>> {
314        self.block_on(self.lb.list_users(filter))
315    }
316
317    pub fn admin_get_account_info(&self, identifier: AccountIdentifier) -> LbResult<AccountInfo> {
318        self.block_on(self.lb.get_account_info(identifier))
319    }
320
321    pub fn admin_validate_account(&self, username: &str) -> LbResult<AdminValidateAccount> {
322        self.block_on(self.lb.validate_account(username))
323    }
324
325    pub fn admin_validate_server(&self) -> LbResult<AdminValidateServer> {
326        self.block_on(self.lb.validate_server())
327    }
328
329    pub fn admin_file_info(&self, id: Uuid) -> LbResult<AdminFileInfoResponse> {
330        self.block_on(self.lb.file_info(id))
331    }
332
333    pub fn admin_rebuild_index(&self, index: ServerIndex) -> LbResult<()> {
334        self.block_on(self.lb.rebuild_index(index))
335    }
336
337    pub fn admin_set_user_tier(&self, username: &str, info: AdminSetUserTierInfo) -> LbResult<()> {
338        self.block_on(self.lb.set_user_tier(username, info))
339    }
340
341    pub fn subscribe(&self) -> Receiver<Event> {
342        // required for the tokio::spawn that this guy does
343        #[cfg(not(target_family = "wasm"))]
344        let _rt = self.rt.enter();
345        self.lb.subscribe()
346    }
347
348    pub fn status(&self) -> Status {
349        self.block_on(self.lb.status())
350    }
351
352    pub fn app_foregrounded(&self) {
353        #[cfg(not(target_family = "wasm"))]
354        {
355            let rt = self.rt.enter();
356            self.lb.app_foregrounded();
357            drop(rt);
358        }
359    }
360
361    #[cfg(not(target_family = "wasm"))]
362    pub fn debug_info(&self, os_info: String) -> LbResult<DebugInfo> {
363        self.rt.block_on(self.lb.debug_info(os_info, true))
364    }
365
366    pub fn recent_panic(&self) -> LbResult<bool> {
367        #[cfg(not(target_family = "wasm"))]
368        return self.block_on(self.lb.recent_panic());
369        #[cfg(target_family = "wasm")]
370        Ok(false)
371    }
372
373    #[cfg(not(target_family = "wasm"))]
374    pub fn write_panic_to_file(&self, error_header: String, bt: String) -> LbResult<String> {
375        self.block_on(self.lb.write_panic_to_file(error_header, bt))
376    }
377}