Skip to content

Commit

Permalink
Prepare SyncDirection type.
Browse files Browse the repository at this point in the history
To indicate whether an initial sync should create the account on the
remote (push) or on local (pull).
  • Loading branch information
tmpfs committed Dec 3, 2024
1 parent d5c19e9 commit d49e8d7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 16 deletions.
1 change: 1 addition & 0 deletions crates/integration_tests/tests/ipc/local_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 6 additions & 2 deletions crates/net/src/account/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion crates/protocol/src/integration/linked_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 4 additions & 9 deletions crates/protocol/src/integration/local_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/protocol/src/sync/auto_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait AutoMerge: RemoteSyncHandler {
}
}
} else {
self.create_remote_account().await?;
self.create_account().await?;
Ok(None)
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/protocol/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
41 changes: 38 additions & 3 deletions crates/protocol/src/sync/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -57,6 +57,9 @@ pub trait RemoteSyncHandler {
/// 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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit d49e8d7

Please sign in to comment.