Files
WhispAssist/src-tauri/src/sync/mod.rs
T
iamdoubz 02834e9cd2 Phase 1
2026-06-30 22:17:30 -05:00

119 lines
4.5 KiB
Rust

//! Remote sync / upload service (Phase 9, FR-SYNC-*). ADR-0010.
//!
//! Off by default. The ONLY content egress besides the local LLM. A single
//! `WebDavTarget` covers the primary set (Nextcloud, ownCloud, Cloudreve,
//! Seafile) AND Synology; OneDrive/Dropbox/Box are secondary OAuth impls.
//!
//! Security invariants (enforced here, see CLAUDE.md):
//! - Credentials come from the OS credential store, never settings/DB/logs/events.
//! - TLS required; plaintext http only via explicit per-target LAN opt-in.
//! - `SyncManager` derives the egress allowlist from enabled targets.
use crate::models::{MeetingId, SyncJobInfo, SyncKind};
use async_trait::async_trait;
use std::path::Path;
#[derive(Debug, thiserror::Error)]
pub enum SyncError {
#[error("target unreachable: {0}")]
Unreachable(String),
#[error("authentication failed")]
Auth,
#[error("insecure transport refused (enable LAN plaintext explicitly to allow)")]
InsecureTransport,
#[error("upload failed: {0}")]
Upload(String),
#[error("credential store error: {0}")]
Credential(String),
}
/// Reports upload progress for a single file → "sync://job" events.
pub type ProgressSink = std::sync::mpsc::Sender<(u64 /*sent*/, u64 /*total*/)>;
/// One provider. `WebDavTarget` serves all primary targets + Synology.
#[async_trait]
pub trait SyncTarget: Send + Sync {
fn kind(&self) -> SyncKind;
/// False for self-hosted WebDAV; true for OneDrive/Dropbox/Box (drives the UI banner).
fn is_third_party(&self) -> bool;
async fn test(&self) -> Result<(), SyncError>;
async fn ensure_dir(&self, remote_dir: &str) -> Result<(), SyncError>;
/// True if a file with this hash already exists remotely (skip-if-unchanged).
async fn exists(&self, remote_path: &str, sha256: &str) -> Result<bool, SyncError>;
/// Upload a file; resumable/chunked for large artifacts.
async fn put(
&self,
local: &Path,
remote_path: &str,
prog: ProgressSink,
) -> Result<(), SyncError>;
}
/// Owns the durable queue, retry/backoff, credential resolution, TLS enforcement,
/// and the settings-derived egress allowlist.
#[async_trait]
pub trait SyncManager: Send + Sync {
/// Enqueue a meeting's selected artifacts for one or all enabled targets.
async fn enqueue_meeting(
&self,
meeting_id: &MeetingId,
target_id: Option<&str>,
) -> Result<(), SyncError>;
/// Drive pending jobs (called on finalize, on startup, and on a low-frequency timer).
async fn pump(&self) -> Result<(), SyncError>;
async fn status(&self, meeting_id: Option<&MeetingId>) -> Result<Vec<SyncJobInfo>, SyncError>;
async fn retry(&self, job_id: &str) -> Result<(), SyncError>;
/// Hosts WA is permitted to contact for sync (enabled targets only). Feeds privacy_self_check.
fn allowlisted_hosts(&self) -> Vec<String>;
}
/// WebDAV provider — primary targets + Synology (feature `sync`).
#[cfg(feature = "sync")]
pub struct WebDavTarget {
pub base_url: String, // https://host/remote.php/dav/files/<user>/ , /seafdav , /dav , …
pub remote_base_path: String,
pub username: String,
pub credential_ref: String, // key into the OS credential store — resolved at use, not stored here
pub third_party: bool, // false for self-hosted
pub allow_plaintext_lan: bool,
}
#[cfg(feature = "sync")]
#[async_trait]
impl SyncTarget for WebDavTarget {
fn kind(&self) -> SyncKind {
SyncKind::WebDav
}
fn is_third_party(&self) -> bool {
self.third_party
}
async fn test(&self) -> Result<(), SyncError> {
// T9.4: PROPFIND base path; verify TLS (or explicit LAN opt-in); resolve creds from keyring.
todo!("Phase 9 — WebDAV test connection")
}
async fn ensure_dir(&self, _remote_dir: &str) -> Result<(), SyncError> {
// T9.2: MKCOL the meeting folder path.
todo!("Phase 9 — WebDAV ensure_dir (MKCOL)")
}
async fn exists(&self, _remote_path: &str, _sha256: &str) -> Result<bool, SyncError> {
todo!("Phase 9 — WebDAV exists / hash compare")
}
async fn put(
&self,
_local: &Path,
_remote_path: &str,
_prog: ProgressSink,
) -> Result<(), SyncError> {
// T9.2/T9.5: PUT (chunked for large files); stream progress.
todo!("Phase 9 — WebDAV upload")
}
}
// Secondary OAuth providers (Phase 9b) — same trait, separate impls.
#[cfg(feature = "sync")]
pub struct OneDriveTarget; // MS Graph
#[cfg(feature = "sync")]
pub struct DropboxTarget;
#[cfg(feature = "sync")]
pub struct BoxTarget;