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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Synchronization types that are sent
//! between the client and server.
use crate::sdk::{
    commit::{CommitHash, CommitState, Comparison},
    device::DevicePublicKey,
    events::{
        AccountDiff, AccountEvent, AccountPatch, DeviceDiff, DeviceEvent,
        DevicePatch, FolderDiff, FolderPatch,
    },
    vault::{secret::SecretId, VaultId},
    Result,
};
use crate::sync::MaybeConflict;
use indexmap::{IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
use sos_sdk::events::WriteEvent;
use std::{
    collections::HashMap,
    fmt,
    hash::{Hash, Hasher},
};
use url::Url;

#[cfg(feature = "files")]
use crate::sdk::{
    events::{FileDiff, FileEvent, FilePatch},
    storage::files::{ExternalFile, ExternalFileName},
    vault::secret::SecretPath,
};

/// Types of event logs.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum EventLogType {
    /// Identity folder event log.
    Identity,
    /// Account event log.
    Account,
    /// Device event log.
    Device,
    /// Files event log.
    #[cfg(feature = "files")]
    Files,
    /// Folder event log.
    Folder(VaultId),
}

/// Server origin information.
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct Origin {
    name: String,
    url: Url,
}

impl Origin {
    /// Create a new origin.
    pub fn new(name: String, url: Url) -> Self {
        Self { name, url }
    }

    /// Name of the origin server.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// URL of the origin server.
    pub fn url(&self) -> &Url {
        &self.url
    }
}

impl PartialEq for Origin {
    fn eq(&self, other: &Self) -> bool {
        self.url == other.url
    }
}

impl Hash for Origin {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.url.hash(state);
    }
}

impl fmt::Display for Origin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.name, self.url)
    }
}

impl From<Url> for Origin {
    fn from(url: Url) -> Self {
        let name = url.authority().to_owned();
        Self { name, url }
    }
}

/// Combined sync status, diff and comparisons.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SyncPacket {
    /// Sync status.
    pub status: SyncStatus,
    /// Sync diff.
    pub diff: SyncDiff,
    /// Sync comparisons.
    pub compare: Option<SyncCompare>,
}

/// Provides a status overview of an account.
///
/// Intended to be used during a synchronization protocol.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct SyncStatus {
    /// Computed root of all event log roots.
    pub root: CommitHash,
    /// Identity vault commit state.
    pub identity: CommitState,
    /// Account log commit state.
    pub account: CommitState,
    /// Device log commit state.
    pub device: CommitState,
    /// Files log commit state.
    #[cfg(feature = "files")]
    pub files: Option<CommitState>,
    /// Commit proofs for the account folders.
    pub folders: IndexMap<VaultId, CommitState>,
}

/// Collection of comparisons for an account.
///
/// When a local account does not contain the proof for
/// a remote event log if will interrogate the server to
/// compare it's proof with the remote tree.
///
/// The server will reply with comparison(s) so that the local
/// account can determine if the trees have completely diverged
/// or whether it can attempt to automatically merge
/// partially diverged trees.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct SyncCompare {
    /// Identity vault comparison.
    pub identity: Option<Comparison>,
    /// Account log comparison.
    pub account: Option<Comparison>,
    /// Device log comparison.
    pub device: Option<Comparison>,
    /// Files log comparison.
    #[cfg(feature = "files")]
    pub files: Option<Comparison>,
    /// Comparisons for the account folders.
    pub folders: IndexMap<VaultId, Comparison>,
}

impl SyncCompare {
    /// Determine if this comparison might conflict.
    pub fn maybe_conflict(&self) -> MaybeConflict {
        MaybeConflict {
            identity: self
                .identity
                .as_ref()
                .map(|c| matches!(c, Comparison::Unknown))
                .unwrap_or(false),
            account: self
                .account
                .as_ref()
                .map(|c| matches!(c, Comparison::Unknown))
                .unwrap_or(false),
            device: self
                .device
                .as_ref()
                .map(|c| matches!(c, Comparison::Unknown))
                .unwrap_or(false),
            #[cfg(feature = "files")]
            files: self
                .files
                .as_ref()
                .map(|c| matches!(c, Comparison::Unknown))
                .unwrap_or(false),
            folders: self
                .folders
                .iter()
                .map(|(k, v)| (*k, matches!(v, Comparison::Unknown)))
                .collect(),
        }
    }
}

/// Diff of events or conflict information.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MaybeDiff<T> {
    /// Diff of local changes to send to the remote.
    Diff(T),
    /// Local needs to compare it's state with remote.
    // The additional `Option` wrapper is required because
    // the files event log may not exist.
    Compare(Option<CommitState>),
}

/// Diff between all events logs on local and remote.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct SyncDiff {
    /// Diff of the identity vault event logs.
    pub identity: Option<MaybeDiff<FolderDiff>>,
    /// Diff of the account event log.
    pub account: Option<MaybeDiff<AccountDiff>>,
    /// Diff of the device event log.
    pub device: Option<MaybeDiff<DeviceDiff>>,
    /// Diff of the files event log.
    #[cfg(feature = "files")]
    pub files: Option<MaybeDiff<FileDiff>>,
    /// Diff for folders in the account.
    pub folders: IndexMap<VaultId, MaybeDiff<FolderDiff>>,
}

/// Collection of patches for an account.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct CreateSet {
    /// Identity vault event logs.
    pub identity: FolderPatch,
    /// Account event logs.
    pub account: AccountPatch,
    /// Device event logs.
    pub device: DevicePatch,
    /// File event logs.
    #[cfg(feature = "files")]
    pub files: FilePatch,
    /// Folders to be imported into the new account.
    pub folders: HashMap<VaultId, FolderPatch>,
}

/// Set of updates to the folders in an account.
///
/// Used to destructively update folders in an account;
/// the identity and folders are entire event
/// logs so that the account state can be overwritten in the
/// case of events such as changing encryption cipher, changing
/// folder password or compacing the events in a folder.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct UpdateSet {
    /// Identity folder event logs.
    pub identity: Option<FolderDiff>,
    /// Account event log.
    pub account: Option<AccountDiff>,
    /// Device event log.
    pub device: Option<DeviceDiff>,
    /// Files event log.
    #[cfg(feature = "files")]
    pub files: Option<FileDiff>,
    /// Folders to be updated.
    pub folders: HashMap<VaultId, FolderDiff>,
}

/// Outcome of a merge operation.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MergeOutcome {
    /// Total number of changes made during a merge.
    ///
    /// Will often be different to the number of tracked changes
    /// as tracked changes are normalized.
    pub changes: u64,

    /// Tracked changes made during a merge.
    ///
    /// These events can be used by client implementations
    /// to react to changes on other devices but they are not
    /// an exact representation of what was merged as tracked
    /// changes are normalized.
    ///
    /// For example, a create secret followed by a deletion of
    /// the same secret will result in both events being omitted.
    ///
    /// Tracked changes are normalized for all event types.
    ///
    /// Not all events are tracked, for example, renaming a folder
    /// triggers events on the account event log and also on the
    /// folder but only the account level events are tracked.
    pub tracked: TrackedChanges,

    /// Collection of external files detected when merging
    /// file events logs, must never be serialized over
    /// the wire.
    ///
    /// Used after merge to update the file transfer queue.
    #[doc(hidden)]
    #[cfg(feature = "files")]
    pub external_files: IndexSet<ExternalFile>,
}

/// Changes tracking during a merge operation.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TrackedChanges {
    /// Changes made to the identity folder.
    pub identity: IndexSet<TrackedFolderChange>,

    /// Changes made to the devices collection.
    pub device: IndexSet<TrackedDeviceChange>,

    /// Changes made to the account.
    pub account: IndexSet<TrackedAccountChange>,

    /// Changes to the files log.
    #[cfg(feature = "files")]
    pub files: IndexSet<TrackedFileChange>,

    /// Change made to each folder.
    pub folders: HashMap<VaultId, IndexSet<TrackedFolderChange>>,
}

impl TrackedChanges {
    /// Add tracked folder changes only when
    /// the set of tracked changes is not empty.
    pub fn add_tracked_folder_changes(
        &mut self,
        folder_id: &VaultId,
        changes: IndexSet<TrackedFolderChange>,
    ) {
        if !changes.is_empty() {
            self.folders.insert(*folder_id, changes);
        }
    }

    /// Create a new set of tracked changes to a folder from a patch.
    pub async fn new_folder_records(
        value: &FolderPatch,
    ) -> Result<IndexSet<TrackedFolderChange>> {
        let events = value.into_events::<WriteEvent>().await?;
        Self::new_folder_events(events).await
    }

    /// Create a new set of tracked changes from a
    /// collection of folder events.
    pub async fn new_folder_events(
        events: Vec<WriteEvent>,
    ) -> Result<IndexSet<TrackedFolderChange>> {
        let mut changes = IndexSet::new();
        for event in events {
            match event {
                WriteEvent::CreateSecret(secret_id, _) => {
                    changes.insert(TrackedFolderChange::Created(secret_id));
                }
                WriteEvent::UpdateSecret(secret_id, _) => {
                    changes.insert(TrackedFolderChange::Updated(secret_id));
                }
                WriteEvent::DeleteSecret(secret_id) => {
                    let created = TrackedFolderChange::Created(secret_id);
                    let updated = TrackedFolderChange::Updated(secret_id);
                    let had_created = changes.shift_remove(&created);
                    changes.shift_remove(&updated);
                    if !had_created {
                        changes
                            .insert(TrackedFolderChange::Deleted(secret_id));
                    }
                }
                _ => {}
            }
        }
        Ok(changes)
    }

    /// Create a new set of tracked changes to an account from a patch.
    pub async fn new_account_records(
        value: &AccountPatch,
    ) -> Result<IndexSet<TrackedAccountChange>> {
        let events = value.into_events::<AccountEvent>().await?;
        Self::new_account_events(events).await
    }

    /// Create a new set of tracked changes from a
    /// collection of account events.
    pub async fn new_account_events(
        events: Vec<AccountEvent>,
    ) -> Result<IndexSet<TrackedAccountChange>> {
        let mut changes = IndexSet::new();
        for event in events {
            match event {
                AccountEvent::CreateFolder(folder_id, _) => {
                    changes.insert(TrackedAccountChange::FolderCreated(
                        folder_id,
                    ));
                }
                AccountEvent::RenameFolder(folder_id, _)
                | AccountEvent::UpdateFolder(folder_id, _) => {
                    changes.insert(TrackedAccountChange::FolderUpdated(
                        folder_id,
                    ));
                }
                AccountEvent::DeleteFolder(folder_id) => {
                    let created =
                        TrackedAccountChange::FolderCreated(folder_id);
                    let updated =
                        TrackedAccountChange::FolderUpdated(folder_id);
                    let had_created = changes.shift_remove(&created);
                    changes.shift_remove(&updated);

                    if !had_created {
                        changes.insert(TrackedAccountChange::FolderDeleted(
                            folder_id,
                        ));
                    }
                }
                _ => {}
            }
        }
        Ok(changes)
    }

    /// Create a new set of tracked changes to a device from a patch.
    pub async fn new_device_records(
        value: &DevicePatch,
    ) -> Result<IndexSet<TrackedDeviceChange>> {
        let events = value.into_events::<DeviceEvent>().await?;
        Self::new_device_events(events).await
    }

    /// Create a new set of tracked changes from a
    /// collection of device events.
    pub async fn new_device_events(
        events: Vec<DeviceEvent>,
    ) -> Result<IndexSet<TrackedDeviceChange>> {
        let mut changes = IndexSet::new();
        for event in events {
            match event {
                DeviceEvent::Trust(device) => {
                    changes.insert(TrackedDeviceChange::Trusted(
                        device.public_key().to_owned(),
                    ));
                }
                DeviceEvent::Revoke(public_key) => {
                    let trusted = TrackedDeviceChange::Trusted(public_key);
                    let had_trusted = changes.shift_remove(&trusted);
                    if !had_trusted {
                        changes
                            .insert(TrackedDeviceChange::Revoked(public_key));
                    }
                }
                _ => {}
            }
        }
        Ok(changes)
    }

    /// Create a new set of tracked changes to a file from a patch.
    #[cfg(feature = "files")]
    pub async fn new_file_records(
        value: &FilePatch,
    ) -> Result<IndexSet<TrackedFileChange>> {
        let events = value.into_events::<FileEvent>().await?;
        Self::new_file_events(events).await
    }

    /// Create a new set of tracked changes from a
    /// collection of file events.
    #[cfg(feature = "files")]
    pub async fn new_file_events(
        events: Vec<FileEvent>,
    ) -> Result<IndexSet<TrackedFileChange>> {
        let mut changes = IndexSet::new();
        for event in events {
            match event {
                FileEvent::CreateFile(owner, name) => {
                    changes.insert(TrackedFileChange::Created(owner, name));
                }
                FileEvent::MoveFile { name, from, dest } => {
                    changes.insert(TrackedFileChange::Moved {
                        name,
                        from,
                        dest,
                    });
                }
                FileEvent::DeleteFile(owner, name) => {
                    let created = TrackedFileChange::Created(owner, name);
                    let had_created = changes.shift_remove(&created);

                    let moved = changes.iter().find_map(|event| {
                        if let TrackedFileChange::Moved {
                            name: moved_name,
                            dest,
                            from,
                        } = event
                        {
                            if moved_name == &name && dest == &owner {
                                return Some(TrackedFileChange::Moved {
                                    name: *moved_name,
                                    from: *from,
                                    dest: *dest,
                                });
                            }
                        }
                        None
                    });
                    if let Some(moved) = moved {
                        changes.shift_remove(&moved);
                    }

                    if !had_created {
                        changes
                            .insert(TrackedFileChange::Deleted(owner, name));
                    }
                }
                _ => {}
            }
        }
        Ok(changes)
    }
}

/// Change made to a device.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum TrackedDeviceChange {
    /// Device was trusted.
    Trusted(DevicePublicKey),
    /// Device was revoked.
    Revoked(DevicePublicKey),
}

/// Change made to an account.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum TrackedAccountChange {
    /// Folder was added.
    FolderCreated(VaultId),
    /// Folder was updated.
    FolderUpdated(VaultId),
    /// Folder was deleted.
    FolderDeleted(VaultId),
}

/// Change made to file event logs.
#[cfg(feature = "files")]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum TrackedFileChange {
    /// File was created in the log.
    Created(SecretPath, ExternalFileName),
    /// File was moved in the log.
    Moved {
        /// File name.
        name: ExternalFileName,
        /// From identifiers.
        from: SecretPath,
        /// Destination identifiers.
        dest: SecretPath,
    },
    /// File was deleted in the log.
    Deleted(SecretPath, ExternalFileName),
}

/// Change made to a folder.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum TrackedFolderChange {
    /// Secret was created.
    Created(SecretId),
    /// Secret was updated.
    Updated(SecretId),
    /// Secret was deleted.
    Deleted(SecretId),
}