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 log::error;
43use serde_json::Value;
44use tauri::AppHandle;
45
46/// Add document for sync.
47///
48/// Registers a document for synchronization with remote collaborators.
49///
50/// # Arguments
51///
52/// * `app_handle` - Tauri application handle
53/// * `document_data` - JSON object with document_id and file_path fields
54///
55/// # Returns
56///
57/// Returns success JSON or an error string.
58///
59/// # Errors
60///
61/// Returns an error if:
62/// - Required fields missing
63/// - Document registration fails
64#[tauri::command]
65pub async fn MountainAddDocumentForSync(app_handle:AppHandle, document_data:Value) -> Result<Value, String> {
66 let DocumentId = document_data["document_id"]
67 .as_str()
68 .ok_or_else(|| {
69 error!("[IPC] [Sync] Missing document_id in document_data");
70 "Missing document_id"
71 })?
72 .to_string();
73 let FilePath = document_data["file_path"]
74 .as_str()
75 .ok_or_else(|| {
76 error!("[IPC] [Sync] Missing file_path in document_data");
77 "Missing file_path"
78 })?
79 .to_string();
80
81 crate::IPC::WindAdvancedSync::mountain_add_document_for_sync(app_handle, DocumentId, FilePath)
82 .await
83 .map_err(|Error| {
84 error!("[IPC] [Sync] Failed to add document for sync: {}", Error);
85 Error.to_string()
86 })
87 .map(|_| Value::Null)
88}
89
90/// Get sync status.
91///
92/// Retrieves the current synchronization status for documents.
93///
94/// # Arguments
95///
96/// * `app_handle` - Tauri application handle
97///
98/// # Returns
99///
100/// Returns sync status JSON, or an error string.
101///
102/// # Errors
103///
104/// Returns an error if status cannot be retrieved.
105#[tauri::command]
106pub async fn MountainGetSyncStatus(app_handle:AppHandle) -> Result<Value, String> {
107 crate::IPC::WindAdvancedSync::mountain_get_sync_status(app_handle)
108 .await
109 .map_err(|Error| {
110 error!("[IPC] [Sync] Failed to get sync status: {}", Error);
111 Error.to_string()
112 })
113 .map(|Status| serde_json::to_value(Status).unwrap_or(Value::Null))
114}