Mountain/Binary/IPC/UpdateSubscriptionCommand.rs
1//! # UpdateSubscriptionCommand
2//!
3//! Manages update subscriptions for document synchronization.
4//!
5//! ## RESPONSIBILITIES
6//!
7//! ### Subscription Management
8//! - Subscribe to document updates
9//! - Manage subscription targets
10//! - Track subscriber information
11//! - Validate subscription parameters
12//!
13//! ## ARCHITECTURAL ROLE
14//!
15//! ### Position in Mountain
16//! - IPC wrapper command in Binary subsystem
17//! - Update subscription endpoint
18//!
19//! ### Dependencies
20//! - crate::IPC::WindAdvancedSync: Subscription management
21//! - tauri: IPC framework
22//! - serde_json: JSON serialization
23//! - log: Logging framework
24//!
25//! ### Dependents
26//! - Wind frontend: Subscribes to updates
27//!
28//! ## SECURITY
29//!
30//! ### Considerations
31//! - Validate target and subscriber identifiers
32//! - Implement authorization for subscriptions
33//! - Prevent duplicate subscriptions
34//!
35//! ## PERFORMANCE
36//!
37//! ### Considerations
38//! - Subscription operations should be fast
39//! - Consider batching for bulk subscriptions
40
41use serde_json::Value;
42use tauri::AppHandle;
43
44use crate::dev_log;
45
46/// Subscribe to updates.
47///
48/// Subscribes a subscriber to receive updates for a target.
49///
50/// # Arguments
51///
52/// * `app_handle` - Tauri application handle
53/// * `subscription_data` - JSON object with target and subscriber 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/// - Subscription fails
64#[tauri::command]
65pub async fn MountainSubscribeToUpdates(app_handle:AppHandle, subscription_data:Value) -> Result<Value, String> {
66 let Target = subscription_data["target"]
67 .as_str()
68 .ok_or_else(|| {
69 dev_log!("ipc", "error: [IPC] [Sync] Missing target in subscription_data");
70 "Missing target"
71 })?
72 .to_string();
73 let Subscriber = subscription_data["subscriber"]
74 .as_str()
75 .ok_or_else(|| {
76 dev_log!("ipc", "error: [IPC] [Sync] Missing subscriber in subscription_data");
77 "Missing subscriber"
78 })?
79 .to_string();
80
81 crate::IPC::WindAdvancedSync::mountain_subscribe_to_updates(app_handle, Target, Subscriber)
82 .await
83 .map_err(|Error| {
84 dev_log!("ipc", "error: [IPC] [Sync] Failed to subscribe to updates: {}", Error);
85 Error.to_string()
86 })
87 .map(|_| Value::Null)
88}