sos_protocol/sync/
remote.rs

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
//! Handler that can synchronize account data between a
//! remote data source and local account.
use crate::{
    AsConflict, ConflictError, MaybeDiff, Merge, MergeOutcome, Origin,
    SyncClient, SyncDirection, SyncPacket, SyncStatus, SyncStorage,
};
use async_trait::async_trait;
use sos_sdk::prelude::{Account, Address};
use std::{collections::HashMap, sync::Arc};
use tokio::sync::Mutex;

#[cfg(feature = "files")]
use crate::transfer::{
    FileOperation, FileTransferQueueSender, TransferOperation,
};

use super::ForceMerge;

/// Trait for types that bridge between a remote data source
/// and a local account.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait RemoteSyncHandler {
    /// Client used to fetch data from the data source.
    type Client: SyncClient + Send + Sync + 'static;

    /// Local account.
    type Account: Account
        + SyncStorage
        + Merge
        + ForceMerge
        + Send
        + Sync
        + 'static;

    /// Error implementation.
    type Error: std::error::Error
        + std::fmt::Debug
        + AsConflict
        + From<ConflictError>
        + From<sos_sdk::Error>
        + From<std::io::Error>
        + From<<Self::Account as Account>::Error>
        + From<<Self::Client as SyncClient>::Error>
        + Send
        + Sync
        + 'static;

    /// Client implementation.
    fn client(&self) -> &Self::Client;

    /// Remote origin.
    fn origin(&self) -> &Origin;

    /// Account address.
    fn address(&self) -> &Address;

    /// Local account.
    fn account(&self) -> Arc<Mutex<Self::Account>>;

    /// Direction for account creation and auto merge.
    fn direction(&self) -> SyncDirection;

    /// Queue for file transfers.
    #[cfg(feature = "files")]
    fn file_transfer_queue(&self) -> &FileTransferQueueSender;

    /// Sync file transfers.
    #[cfg(feature = "files")]
    async fn execute_sync_file_transfers(&self) -> Result<(), Self::Error>;

    /// Push an account to the remote.
    #[doc(hidden)]
    async fn create_push_account(&self) -> Result<(), Self::Error> {
        {
            let account = self.account();
            let account = account.lock().await;
            let public_account = account.change_set().await?;
            self.client()
                .create_account(self.address(), public_account)
                .await?;
        }

        #[cfg(feature = "files")]
        self.execute_sync_file_transfers().await?;

        Ok(())
    }

    /// Pull an account from the remote.
    #[doc(hidden)]
    async fn create_pull_account(&self) -> Result<(), Self::Error> {
        tracing::info!("create_pull_account");

        // Get account data from the remote.
        let public_account =
            self.client().fetch_account(self.address()).await?;

        tracing::info!("create_pull_account::fetch_completed");

        {
            let account = self.account();
            let mut account = account.lock().await;
            account
                .import_account_events(
                    public_account.identity,
                    public_account.account,
                    public_account.device,
                    public_account.folders,
                    #[cfg(feature = "files")]
                    public_account.files,
                )
                .await?;
        }

        /*
        #[cfg(feature = "files")]
        self.execute_sync_file_transfers().await?;
        */

        Ok(())
    }

    /// Create an account on local or remote depending
    /// on the sync direction.
    async fn create_account(&self) -> Result<(), Self::Error> {
        match self.direction() {
            SyncDirection::Push => self.create_push_account().await,
            SyncDirection::Pull => self.create_pull_account().await,
        }
    }

    /// Sync the account.
    async fn sync_account(
        &self,
        remote_status: SyncStatus,
    ) -> Result<MergeOutcome, Self::Error> {
        let account = self.account();
        let mut account = account.lock().await;

        tracing::debug!("merge_client");

        let (needs_sync, local_status, local_changes) =
            crate::diff(&*account, remote_status).await?;

        tracing::debug!(needs_sync = %needs_sync, "merge_client");

        let mut outcome = MergeOutcome::default();

        if needs_sync {
            let packet = SyncPacket {
                status: local_status,
                diff: local_changes,
                compare: None,
            };
            let remote_changes =
                self.client().sync(self.address(), packet.clone()).await?;

            let maybe_conflict = remote_changes
                .compare
                .as_ref()
                .map(|c| c.maybe_conflict())
                .unwrap_or_default();
            let has_conflicts = maybe_conflict.has_conflicts();

            if !has_conflicts {
                account.merge(remote_changes.diff, &mut outcome).await?;

                // Compute which external files need to be downloaded
                // and add to the transfers queue

                #[cfg(feature = "files")]
                if !outcome.external_files.is_empty() {
                    use sos_sdk::account::Account;
                    let paths = account.paths();
                    // let mut writer = self.transfers.write().await;

                    for file in outcome.external_files.drain(..) {
                        let file_path = paths.file_location(
                            file.vault_id(),
                            file.secret_id(),
                            file.file_name().to_string(),
                        );
                        if !sos_sdk::vfs::try_exists(file_path).await? {
                            tracing::debug!(
                                file = ?file,
                                "add file download to transfers",
                            );

                            if self.file_transfer_queue().receiver_count() > 0
                            {
                                let _ =
                                    self.file_transfer_queue().send(vec![
                                        FileOperation(
                                            file,
                                            TransferOperation::Download,
                                        ),
                                    ]);
                            }
                        }
                    }
                }

                // self.compare(&mut *account, remote_changes).await?;
            } else {
                // Some parts of the remote patch may not
                // be in conflict and must still be merged
                if !maybe_conflict.identity {
                    if let Some(MaybeDiff::Diff(diff)) =
                        remote_changes.diff.identity
                    {
                        account.merge_identity(diff, &mut outcome).await?;
                    }
                }
                if !maybe_conflict.account {
                    if let Some(MaybeDiff::Diff(diff)) =
                        remote_changes.diff.account
                    {
                        account.merge_account(diff, &mut outcome).await?;
                    }
                }
                if !maybe_conflict.device {
                    if let Some(MaybeDiff::Diff(diff)) =
                        remote_changes.diff.device
                    {
                        account.merge_device(diff, &mut outcome).await?;
                    }
                }
                #[cfg(feature = "files")]
                if !maybe_conflict.files {
                    if let Some(MaybeDiff::Diff(diff)) =
                        remote_changes.diff.files
                    {
                        account.merge_files(diff, &mut outcome).await?;
                    }
                }

                let merge_folders = remote_changes
                    .diff
                    .folders
                    .into_iter()
                    .filter(|(k, _)| maybe_conflict.folders.get(k).is_none())
                    .collect::<HashMap<_, _>>();
                for (id, maybe_diff) in merge_folders {
                    if let MaybeDiff::Diff(diff) = maybe_diff {
                        account.merge_folder(&id, diff, &mut outcome).await?;
                    }
                }
                return Err(ConflictError::Soft {
                    conflict: maybe_conflict,
                    local: packet.status,
                    remote: remote_changes.status,
                }
                .into());
            }
        }

        Ok(outcome)
    }
}