Skip to main content

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
72			"Missing document_id"
73		})?
74		.to_string();
75
76	let FilePath = document_data["file_path"]
77		.as_str()
78		.ok_or_else(|| {
79			dev_log!("ipc", "error: [IPC] [Sync] Missing file_path in document_data");
80
81			"Missing file_path"
82		})?
83		.to_string();
84
85	crate::IPC::WindAdvancedSync::mountain_add_document_for_sync(app_handle, DocumentId, FilePath)
86		.await
87		.map_err(|Error| {
88			dev_log!("ipc", "error: [IPC] [Sync] Failed to add document for sync: {}", Error);
89
90			Error.to_string()
91		})
92		.map(|_| Value::Null)
93}
94
95/// Get sync status.
96///
97/// Retrieves the current synchronization status for documents.
98///
99/// # Arguments
100///
101/// * `app_handle` - Tauri application handle
102///
103/// # Returns
104///
105/// Returns sync status JSON, or an error string.
106///
107/// # Errors
108///
109/// Returns an error if status cannot be retrieved.
110#[tauri::command]
111pub async fn MountainGetSyncStatus(app_handle:AppHandle) -> Result<Value, String> {
112	crate::IPC::WindAdvancedSync::mountain_get_sync_status(app_handle)
113		.await
114		.map_err(|Error| {
115			dev_log!("ipc", "error: [IPC] [Sync] Failed to get sync status: {}", Error);
116
117			Error.to_string()
118		})
119		.map(|Status| serde_json::to_value(Status).unwrap_or(Value::Null))
120}