Mountain/Binary/IPC/DocumentSyncCommand.rs
1//! # DocumentSyncCommand
2//!
3//! Handles document synchronization for collaboration features.
4//!
5//! ## RESPONSIBILITIES
6//!
7//! ### Document Sync
8//! - Add documents for synchronization
9//! - Get sync status for documents
10//! - Validate document identifiers
11//! - Track sync progress
12//!
13//! ## ARCHITECTURAL ROLE
14//!
15//! ### Position in Mountain
16//! - IPC wrapper command in Binary subsystem
17//! - Document sync endpoint
18//!
19//! ### Dependencies
20//! - crate::IPC::WindAdvancedSync: Document synchronization
21//! - tauri: IPC framework
22//! - serde_json: JSON serialization
23//! - log: Logging framework
24//!
25//! ### Dependents
26//! - Wind frontend: Syncs documents
27//!
28//! ## SECURITY
29//!
30//! ### Considerations
31//! - Validate file paths to prevent directory traversal
32//! - Sanitize document IDs
33//! - Check file access permissions
34//!
35//! ## PERFORMANCE
36//!
37//! ### Considerations
38//! - Sync may involve network/disk I/O
39//! - Implement progress reporting for large files
40//! - Consider delta sync for large documents
41
42use serde_json::Value;
43use tauri::AppHandle;
44
45use crate::dev_log;
46
47/// Add document for sync.
48///
49/// Registers a document for synchronization with remote collaborators.
50///
51/// # Arguments
52///
53/// * `app_handle` - Tauri application handle
54/// * `document_data` - JSON object with document_id and file_path fields
55///
56/// # Returns
57///
58/// Returns success JSON or an error string.
59///
60/// # Errors
61///
62/// Returns an error if:
63/// - Required fields missing
64/// - Document registration fails
65#[tauri::command]
66pub async fn MountainAddDocumentForSync(app_handle:AppHandle, document_data:Value) -> Result<Value, String> {
67 let DocumentId = document_data["document_id"]
68 .as_str()
69 .ok_or_else(|| {
70 dev_log!("ipc", "error: [IPC] [Sync] Missing document_id in document_data");
71 "Missing document_id"
72 })?
73 .to_string();
74 let FilePath = document_data["file_path"]
75 .as_str()
76 .ok_or_else(|| {
77 dev_log!("ipc", "error: [IPC] [Sync] Missing file_path in document_data");
78 "Missing file_path"
79 })?
80 .to_string();
81
82 crate::IPC::WindAdvancedSync::mountain_add_document_for_sync(app_handle, DocumentId, FilePath)
83 .await
84 .map_err(|Error| {
85 dev_log!("ipc", "error: [IPC] [Sync] Failed to add document for sync: {}", Error);
86 Error.to_string()
87 })
88 .map(|_| Value::Null)
89}
90
91/// Get sync status.
92///
93/// Retrieves the current synchronization status for documents.
94///
95/// # Arguments
96///
97/// * `app_handle` - Tauri application handle
98///
99/// # Returns
100///
101/// Returns sync status JSON, or an error string.
102///
103/// # Errors
104///
105/// Returns an error if status cannot be retrieved.
106#[tauri::command]
107pub async fn MountainGetSyncStatus(app_handle:AppHandle) -> Result<Value, String> {
108 crate::IPC::WindAdvancedSync::mountain_get_sync_status(app_handle)
109 .await
110 .map_err(|Error| {
111 dev_log!("ipc", "error: [IPC] [Sync] Failed to get sync status: {}", Error);
112 Error.to_string()
113 })
114 .map(|Status| serde_json::to_value(Status).unwrap_or(Value::Null))
115}