lb-rs 26.9.17

The rust library for interacting with your lockbook.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::{future::Future, path::PathBuf};

use tokio::sync::broadcast::Receiver;
use uuid::Uuid;

use crate::model::account::{Account, Username};
use crate::model::api::{
    AccountFilter, AccountIdentifier, AccountInfo, AdminFileInfoResponse, AdminSetUserTierInfo,
    AdminValidateAccount, AdminValidateServer, ServerIndex, StripeAccountTier, SubscriptionInfo,
};
use crate::model::core_config::Config;
use crate::model::crypto::DecryptedDocument;
use crate::model::errors::{LbResult, Warning};
use crate::model::file::{File, ShareMode};
use crate::model::file_metadata::{DocumentHmac, FileType};
use crate::model::path_ops::Filter;
use crate::service::activity::RankingWeights;

use crate::service::events::Event;
use crate::service::import_export::{ExportFileInfo, ImportStatus};
use crate::service::usage::UsageMetrics;
use crate::subscribers::status::Status;

#[cfg(not(target_family = "wasm"))]
use {std::sync::Arc, tokio::runtime::Runtime};

#[cfg(not(target_family = "wasm"))]
use crate::service::debug::DebugInfo;

#[derive(Clone)]
pub struct Lb {
    lb: crate::Lb,
    #[cfg(not(target_family = "wasm"))]
    rt: Arc<Runtime>,
}

impl Lb {
    #[cfg(target_family = "wasm")]
    pub fn init(config: Config) -> LbResult<Self> {
        let lb = crate::Lb::init_dummy(config).unwrap();
        Ok(Self { lb })
    }

    #[cfg(target_family = "wasm")]
    fn block_on<F>(&self, future: F) -> F::Output
    where
        F: Future,
    {
        futures::executor::block_on(future)
    }

    #[cfg(not(target_family = "wasm"))]
    fn block_on<F>(&self, future: F) -> F::Output
    where
        F: Future,
    {
        self.rt.block_on(future)
    }

    #[cfg(not(target_family = "wasm"))]
    pub fn init(config: Config) -> LbResult<Self> {
        let rt = Arc::new(Runtime::new().unwrap());
        let lb = rt.block_on(crate::Lb::init(config))?;
        Ok(Self { rt, lb })
    }

    /// The underlying async [`crate::Lb`], for callers that run their own
    /// runtime (the blocking methods `block_on` this wrapper's runtime, which
    /// panics when called from inside another).
    pub fn async_lb(&self) -> &crate::Lb {
        &self.lb
    }

    pub fn create_account(
        &self, username: &str, api_url: &str, welcome_doc: bool,
    ) -> LbResult<Account> {
        self.block_on(self.lb.create_account(username, api_url, welcome_doc))
    }

    pub fn import_account(&self, key: &str, api_url: Option<&str>) -> LbResult<Account> {
        self.block_on(self.lb.import_account(key, api_url))
    }

    pub fn export_account_private_key(&self) -> LbResult<String> {
        self.lb.export_account_private_key()
    }

    pub fn export_account_phrase(&self) -> LbResult<String> {
        self.lb.export_account_phrase()
    }

    pub fn export_account_qr(&self) -> LbResult<Vec<u8>> {
        self.lb.export_account_qr()
    }

    pub fn get_account(&self) -> LbResult<Account> {
        self.lb.get_account()
    }

    pub fn get_config(&self) -> Config {
        self.lb.config().clone()
    }

    pub fn create_file(&self, name: &str, parent: &Uuid, file_type: FileType) -> LbResult<File> {
        self.block_on(self.lb.create_file(name, parent, file_type))
    }

    pub fn safe_write(
        &self, id: Uuid, old_hmac: Option<DocumentHmac>, content: Vec<u8>, origin: Option<Uuid>,
    ) -> LbResult<DocumentHmac> {
        self.block_on(self.lb.safe_write(id, old_hmac, content, origin))
    }

    pub fn write_document(&self, id: Uuid, content: &[u8]) -> LbResult<()> {
        self.block_on(self.lb.write_document(id, content))
    }

    pub fn get_root(&self) -> LbResult<File> {
        self.block_on(self.lb.root())
    }

    pub fn get_children(&self, id: &Uuid) -> LbResult<Vec<File>> {
        self.block_on(self.lb.get_children(id))
    }

    pub fn get_and_get_children_recursively(&self, id: &Uuid) -> LbResult<Vec<File>> {
        self.block_on(self.lb.get_and_get_children_recursively(id))
    }

    pub fn get_file_by_id(&self, id: Uuid) -> LbResult<File> {
        self.block_on(self.lb.get_file_by_id(id))
    }

    pub fn delete_file(&self, id: &Uuid) -> LbResult<()> {
        self.block_on(self.lb.delete(id))
    }

    pub fn duplicate_file(&self, id: &Uuid) -> LbResult<File> {
        self.block_on(self.lb.duplicate_file(id))
    }

    pub fn read_document(&self, id: Uuid, user_activity: bool) -> LbResult<DecryptedDocument> {
        self.block_on(self.lb.read_document(id, user_activity))
    }

    pub fn read_document_with_hmac(
        &self, id: Uuid, user_activity: bool,
    ) -> LbResult<(Option<DocumentHmac>, DecryptedDocument)> {
        self.block_on(self.lb.read_document_with_hmac(id, user_activity))
    }

    pub fn list_metadatas(&self) -> LbResult<Vec<File>> {
        self.block_on(self.lb.list_metadatas())
    }

    pub fn rename_file(&self, id: &Uuid, new_name: &str) -> LbResult<()> {
        self.block_on(self.lb.rename_file(id, new_name))
    }

    pub fn move_file(&self, id: &Uuid, new_parent: &Uuid) -> LbResult<()> {
        self.block_on(self.lb.move_file(id, new_parent))
    }

    pub fn share_file(&self, id: Uuid, username: &str, mode: ShareMode) -> LbResult<()> {
        self.block_on(self.lb.share_file(id, username, mode))
    }

    pub fn get_pending_shares(&self) -> LbResult<Vec<File>> {
        self.block_on(self.lb.get_pending_shares())
    }

    pub fn get_pending_share_files(&self) -> LbResult<Vec<File>> {
        self.block_on(self.lb.get_pending_share_files())
    }

    pub fn delete_pending_share(&self, id: &Uuid) -> LbResult<()> {
        self.block_on(async { self.lb.reject_share(id).await })
    }

    /// Usernames we've seen (pub key lookup cache) — suggestions + offline hits.
    pub fn known_usernames(&self) -> LbResult<Vec<String>> {
        self.block_on(self.lb.known_usernames())
    }

    /// Whether `username` exists (cache + server). May hit the network.
    pub fn username_exists(&self, username: &str) -> LbResult<bool> {
        self.block_on(self.lb.username_exists(username))
    }

    pub fn create_link_at_path(&self, path_and_name: &str, target_id: Uuid) -> LbResult<File> {
        self.block_on(self.lb.create_link_at_path(path_and_name, target_id))
    }

    pub fn create_at_path(&self, path_and_name: &str) -> LbResult<File> {
        self.block_on(self.lb.create_at_path(path_and_name))
    }

    pub fn get_by_path(&self, path: &str) -> LbResult<File> {
        self.block_on(self.lb.get_by_path(path))
    }

    pub fn get_path_by_id(&self, id: Uuid) -> LbResult<String> {
        self.block_on(self.lb.get_path_by_id(id))
    }

    pub fn list_paths(&self, filter: Option<Filter>) -> LbResult<Vec<String>> {
        self.block_on(self.lb.list_paths(filter))
    }

    pub fn list_paths_with_ids(&self, filter: Option<Filter>) -> LbResult<Vec<(Uuid, String)>> {
        self.block_on(self.lb.list_paths_with_ids(filter))
    }

    pub fn get_local_changes(&self) -> LbResult<Vec<Uuid>> {
        Ok(self.block_on(self.lb.local_changes()))
    }

    pub fn sync(&self) -> LbResult<()> {
        self.block_on(self.lb.sync())
    }

    pub fn get_last_synced(&self) -> LbResult<i64> {
        self.block_on(self.lb.get_last_synced())
    }

    pub fn get_last_synced_human_string(&self) -> LbResult<String> {
        self.block_on(self.lb.get_last_synced_human())
    }

    pub fn get_timestamp_human_string(&self, timestamp: i64) -> String {
        self.lb.get_timestamp_human_string(timestamp)
    }

    pub fn suggested_docs(&self, settings: RankingWeights) -> LbResult<Vec<Uuid>> {
        self.block_on(self.lb.suggested_docs(settings))
    }

    pub fn clear_suggested(&self) -> LbResult<()> {
        self.block_on(self.lb.clear_suggested())
    }

    pub fn clear_suggested_id(&self, target_id: Uuid) -> LbResult<()> {
        self.block_on(self.lb.clear_suggested_id(target_id))
    }

    pub fn pin_file(&self, id: Uuid) -> LbResult<()> {
        self.block_on(self.lb.pin_file(id))
    }

    pub fn unpin_file(&self, id: Uuid) -> LbResult<()> {
        self.block_on(self.lb.unpin_file(id))
    }

    pub fn list_pinned(&self) -> LbResult<Vec<Uuid>> {
        self.block_on(self.lb.list_pinned())
    }

    // TODO: examine why the old get_usage does a bunch of things
    pub fn get_usage(&self) -> LbResult<UsageMetrics> {
        self.block_on(self.lb.get_usage())
    }

    pub fn import_files<F: Fn(ImportStatus)>(
        &self, sources: &[PathBuf], dest: Uuid, update_status: &F,
    ) -> LbResult<()> {
        self.block_on(self.lb.import_files(sources, dest, update_status))
    }

    pub fn export_files(
        &self, id: Uuid, dest: PathBuf, edit: bool,
        export_progress: &Option<Box<dyn Fn(ExportFileInfo)>>,
    ) -> LbResult<()> {
        self.block_on(self.lb.export_file(id, dest, edit, export_progress))
    }

    pub fn get_file_link_url(&self, id: Uuid) -> LbResult<String> {
        self.block_on(self.lb.get_file_link_url(id))
    }

    pub fn path_searcher(&self) -> crate::search::PathSearcher {
        self.block_on(self.lb.path_searcher())
    }

    pub fn content_searcher(&self) -> crate::search::ContentSearcher {
        crate::search::ContentSearcher::new(self)
    }

    pub fn validate(&self) -> LbResult<Vec<Warning>> {
        self.block_on(self.lb.test_repo_integrity(true))
    }

    pub fn upgrade_account_stripe(&self, account_tier: StripeAccountTier) -> LbResult<()> {
        self.block_on(self.lb.upgrade_account_stripe(account_tier))
    }

    pub fn upgrade_account_google_play(
        &self, purchase_token: &str, account_id: &str,
    ) -> LbResult<()> {
        self.block_on(
            self.lb
                .upgrade_account_google_play(purchase_token, account_id),
        )
    }

    pub fn upgrade_account_app_store(
        &self, original_transaction_id: String, app_account_token: String,
    ) -> LbResult<()> {
        self.block_on(
            self.lb
                .upgrade_account_app_store(original_transaction_id, app_account_token),
        )
    }

    pub fn cancel_subscription(&self) -> LbResult<()> {
        self.block_on(self.lb.cancel_subscription())
    }

    pub fn get_subscription_info(&self) -> LbResult<Option<SubscriptionInfo>> {
        self.block_on(self.lb.get_subscription_info())
    }

    pub fn delete_account(&self) -> LbResult<()> {
        self.block_on(self.lb.delete_account())
    }

    pub fn admin_disappear_account(&self, username: &str) -> LbResult<()> {
        self.block_on(self.lb.disappear_account(username))
    }

    pub fn admin_disappear_file(&self, id: Uuid) -> LbResult<()> {
        self.block_on(self.lb.disappear_file(id))
    }

    pub fn admin_list_users(&self, filter: Option<AccountFilter>) -> LbResult<Vec<Username>> {
        self.block_on(self.lb.list_users(filter))
    }

    pub fn admin_get_account_info(&self, identifier: AccountIdentifier) -> LbResult<AccountInfo> {
        self.block_on(self.lb.get_account_info(identifier))
    }

    pub fn admin_validate_account(&self, username: &str) -> LbResult<AdminValidateAccount> {
        self.block_on(self.lb.validate_account(username))
    }

    pub fn admin_validate_server(&self) -> LbResult<AdminValidateServer> {
        self.block_on(self.lb.validate_server())
    }

    pub fn admin_file_info(&self, id: Uuid) -> LbResult<AdminFileInfoResponse> {
        self.block_on(self.lb.file_info(id))
    }

    pub fn admin_rebuild_index(&self, index: ServerIndex) -> LbResult<()> {
        self.block_on(self.lb.rebuild_index(index))
    }

    pub fn admin_set_user_tier(&self, username: &str, info: AdminSetUserTierInfo) -> LbResult<()> {
        self.block_on(self.lb.set_user_tier(username, info))
    }

    pub fn subscribe(&self) -> Receiver<Event> {
        // required for the tokio::spawn that this guy does
        #[cfg(not(target_family = "wasm"))]
        let _rt = self.rt.enter();
        self.lb.subscribe()
    }

    pub fn status(&self) -> Status {
        self.block_on(self.lb.status())
    }

    pub fn app_foregrounded(&self) {
        #[cfg(not(target_family = "wasm"))]
        {
            let rt = self.rt.enter();
            self.lb.app_foregrounded();
            drop(rt);
        }
    }

    pub fn user_active(&self) -> bool {
        self.block_on(async {
            match self.lb.local.get() {
                Some(local) => local.user_active().await,
                None => true,
            }
        })
    }

    #[cfg(not(target_family = "wasm"))]
    pub fn debug_info(&self, os_info: String) -> LbResult<DebugInfo> {
        self.rt.block_on(self.lb.debug_info(os_info, true))
    }

    pub fn recent_panic(&self) -> LbResult<bool> {
        #[cfg(not(target_family = "wasm"))]
        return self.block_on(self.lb.recent_panic());
        #[cfg(target_family = "wasm")]
        Ok(false)
    }

    #[cfg(not(target_family = "wasm"))]
    pub fn write_panic_to_file(&self, error_header: String, bt: String) -> LbResult<String> {
        self.block_on(self.lb.write_panic_to_file(error_header, bt))
    }
}