diff --git a/crates/integration_tests/tests/ipc/local_sync.rs b/crates/integration_tests/tests/ipc/local_sync.rs index d4e7e0d9b7..96ad2c43d7 100644 --- a/crates/integration_tests/tests/ipc/local_sync.rs +++ b/crates/integration_tests/tests/ipc/local_sync.rs @@ -26,6 +26,7 @@ use crate::{ /// Test for syncing between apps installed on the same /// device via the IPC communication channel. +#[ignore] #[tokio::test] async fn integration_ipc_local_sync() -> Result<()> { const TEST_ID: &str = "ipc_local_sync"; diff --git a/crates/net/src/account/remote.rs b/crates/net/src/account/remote.rs index c7be835f5c..ae094d1729 100644 --- a/crates/net/src/account/remote.rs +++ b/crates/net/src/account/remote.rs @@ -2,8 +2,8 @@ use crate::{ net::HttpClient, protocol::{ - AutoMerge, Origin, RemoteResult, RemoteSync, SyncClient, SyncOptions, - UpdateSet, + AutoMerge, Origin, RemoteResult, RemoteSync, SyncClient, + SyncDirection, SyncOptions, UpdateSet, }, sdk::{ account::LocalAccount, @@ -74,6 +74,10 @@ impl RemoteSyncHandler for RemoteBridge { type Account = LocalAccount; type Error = crate::Error; + fn direction(&self) -> SyncDirection { + SyncDirection::Push + } + fn client(&self) -> &Self::Client { &self.client } diff --git a/crates/protocol/src/integration/linked_account.rs b/crates/protocol/src/integration/linked_account.rs index 0dffbe4f5b..7ae3a553c2 100644 --- a/crates/protocol/src/integration/linked_account.rs +++ b/crates/protocol/src/integration/linked_account.rs @@ -2,7 +2,7 @@ //! same device using a local client. use crate::{ AutoMerge, Error, Origin, RemoteResult, RemoteSync, RemoteSyncHandler, - Result, SyncClient, SyncOptions, UpdateSet, + Result, SyncClient, SyncDirection, SyncOptions, UpdateSet, }; use async_trait::async_trait; use indexmap::IndexSet; @@ -720,6 +720,10 @@ impl RemoteSyncHandler for LinkedAccount { type Account = LocalAccount; type Error = Error; + fn direction(&self) -> SyncDirection { + SyncDirection::Pull + } + fn client(&self) -> &Self::Client { &self.client } diff --git a/crates/protocol/src/integration/local_client.rs b/crates/protocol/src/integration/local_client.rs index 91e35fd7af..59765054f9 100644 --- a/crates/protocol/src/integration/local_client.rs +++ b/crates/protocol/src/integration/local_client.rs @@ -18,29 +18,24 @@ //! the app integration was installed on the device. use crate::{ - local_transport::{LocalTransport, TransportRequest, TransportResponse}, + local_transport::{LocalTransport, TransportResponse}, CreateSet, DiffRequest, DiffResponse, Error, Origin, PatchRequest, PatchResponse, Result, ScanRequest, ScanResponse, SyncClient, SyncPacket, SyncStatus, UpdateSet, WireEncodeDecode, }; use async_trait::async_trait; -use bytes::Bytes; -use http::{ - header::CONTENT_TYPE, Method, Request, Response, StatusCode, Uri, -}; -use serde::{Deserialize, Serialize}; +use http::{header::CONTENT_TYPE, Method, Request, StatusCode, Uri}; use serde_json::Value; -use serde_with::{serde_as, DisplayFromStr}; use sos_sdk::{ constants::{ routes::v1::{ SYNC_ACCOUNT, SYNC_ACCOUNT_EVENTS, SYNC_ACCOUNT_STATUS, }, - MIME_TYPE_JSON, MIME_TYPE_PROTOBUF, X_SOS_ACCOUNT_ID, + MIME_TYPE_PROTOBUF, X_SOS_ACCOUNT_ID, }, prelude::Address, }; -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; use tokio::sync::Mutex; use tracing::instrument; diff --git a/crates/protocol/src/sync/auto_merge.rs b/crates/protocol/src/sync/auto_merge.rs index dfb1dc1a87..3f626f3e44 100644 --- a/crates/protocol/src/sync/auto_merge.rs +++ b/crates/protocol/src/sync/auto_merge.rs @@ -90,7 +90,7 @@ pub trait AutoMerge: RemoteSyncHandler { } } } else { - self.create_remote_account().await?; + self.create_account().await?; Ok(None) } } diff --git a/crates/protocol/src/sync/mod.rs b/crates/protocol/src/sync/mod.rs index c7b5b3575d..f9b6f89057 100644 --- a/crates/protocol/src/sync/mod.rs +++ b/crates/protocol/src/sync/mod.rs @@ -14,4 +14,19 @@ pub use primitives::*; pub use remote::*; pub use transport::*; +/// Direction of a sync. +#[derive(Debug, Clone, Copy)] +pub enum SyncDirection { + /// Create accounts on remote from the local. + /// + /// Used when a local account is pushing data to + /// a server for syncing with other devices. + Push, + /// Create accounts on local from the remote. + /// + /// Used by local replicas for app integrations + /// such as the browser extension. + Pull, +} + pub(crate) use folder::{FolderMerge, IdentityFolderMerge}; diff --git a/crates/protocol/src/sync/remote.rs b/crates/protocol/src/sync/remote.rs index 8259c7eef9..eeee2fc937 100644 --- a/crates/protocol/src/sync/remote.rs +++ b/crates/protocol/src/sync/remote.rs @@ -2,7 +2,7 @@ //! remote data source and local account. use crate::{ AsConflict, ConflictError, MaybeDiff, Merge, MergeOutcome, Origin, - SyncClient, SyncPacket, SyncStatus, SyncStorage, + SyncClient, SyncDirection, SyncPacket, SyncStatus, SyncStorage, }; use async_trait::async_trait; use sos_sdk::prelude::{Account, Address}; @@ -57,6 +57,9 @@ pub trait RemoteSyncHandler { /// Local account. fn account(&self) -> Arc>; + /// Direction for account creation and auto merge. + fn direction(&self) -> SyncDirection; + /// Queue for file transfers. #[cfg(feature = "files")] fn file_transfer_queue(&self) -> &FileTransferQueueSender; @@ -65,8 +68,28 @@ pub trait RemoteSyncHandler { #[cfg(feature = "files")] async fn execute_sync_file_transfers(&self) -> Result<(), Self::Error>; - /// Create an account on the remote. - async fn create_remote_account(&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> { + /* { let account = self.account(); let account = account.lock().await; @@ -80,6 +103,18 @@ pub trait RemoteSyncHandler { self.execute_sync_file_transfers().await?; Ok(()) + */ + + todo!(); + } + + /// 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.