1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//! Internal user sync coordination for the sync module.
//!
//! This module handles user preference tracking, user-database relationships,
//! and combined sync settings computation. It operates on the sync tree but
//! doesn't own it.
use tracing::debug;
use super::error::SyncError;
use crate::{Error, Result, Transaction, crdt::doc::path, store::DocStore};
/// User-aware sync subtree constants
pub(super) const DATABASE_USERS_SUBTREE: &str = "database_users"; // Maps database_id -> {users, combined_settings}
pub(super) const USER_TRACKING_SUBTREE: &str = "user_tracking"; // Maps user_uuid -> {preferences_db_id, preferences_tips}
/// Internal user sync manager for the sync module.
///
/// This struct manages all user sync coordination operations for the sync module,
/// operating on an Transaction to stage changes.
pub(super) struct UserSyncManager<'a> {
op: &'a Transaction,
}
impl<'a> UserSyncManager<'a> {
/// Create a new UserSyncManager that operates on the given Transaction.
pub(super) fn new(op: &'a Transaction) -> Self {
Self { op }
}
/// Track a user's preferences database for sync monitoring.
///
/// This establishes the connection between the user and their preferences database
/// for ongoing sync tracking. This operation is idempotent.
///
/// # Arguments
/// * `user_uuid` - The user's unique identifier
/// * `preferences_db_id` - The ID of the user's private database
///
/// # Returns
/// A Result indicating success or an error.
pub(super) fn track_user_preferences(
&self,
user_uuid: impl AsRef<str>,
preferences_db_id: &crate::entry::ID,
) -> Result<()> {
let user_tracking = self.op.get_store::<DocStore>(USER_TRACKING_SUBTREE)?;
// Check if the user is already registered
if user_tracking
.get_path(path!(user_uuid.as_ref(), "preferences_db_id"))
.is_ok()
{
return Ok(());
}
// Store the preferences database ID
user_tracking.set_path(
path!(user_uuid.as_ref(), "preferences_db_id"),
preferences_db_id.to_string(),
)?;
// Initialize with empty tips (will be populated on first update)
user_tracking.set_path(
path!(user_uuid.as_ref(), "preferences_tips"),
serde_json::to_string(&Vec::<String>::new()).unwrap(),
)?;
debug!(user_uuid = %user_uuid.as_ref(), "Tracking user preferences for sync");
Ok(())
}
/// Get the current state of a tracked user's preferences database.
///
/// Returns the preferences database ID and the tips that were last read,
/// enabling change detection via tip comparison.
///
/// # Arguments
/// * `user_uuid` - The user's unique identifier
///
/// # Returns
/// A tuple of (preferences_db_id, preferences_tips) or None if user not tracked
pub(super) fn get_tracked_user_state(
&self,
user_uuid: impl AsRef<str>,
) -> Result<Option<(crate::entry::ID, Vec<crate::entry::ID>)>> {
let user_tracking = self.op.get_store::<DocStore>(USER_TRACKING_SUBTREE)?;
// Check if user exists
if !user_tracking.contains_path_str(user_uuid.as_ref()) {
return Ok(None);
}
// Get preferences DB ID
let prefs_db_id_str = user_tracking
.get_path_as::<String>(path!(user_uuid.as_ref(), "preferences_db_id"))
.map_err(|_| {
Error::Sync(SyncError::SerializationError(
"Missing preferences_db_id field".to_string(),
))
})?;
let prefs_db_id = crate::entry::ID::from(prefs_db_id_str.as_str());
// Get preferences tips
let tips_json = user_tracking
.get_path_as::<String>(path!(user_uuid.as_ref(), "preferences_tips"))
.unwrap_or_else(|_| "[]".to_string());
let tips_strings: Vec<String> = serde_json::from_str(&tips_json).unwrap_or_default();
let tips: Vec<crate::entry::ID> = tips_strings
.into_iter()
.map(|s| crate::entry::ID::from(s.as_str()))
.collect();
Ok(Some((prefs_db_id, tips)))
}
/// Update the tracked tips for a user's preferences database.
///
/// This should be called after successfully processing a user's preferences
/// to record which version of the preferences database has been integrated
/// into the sync tree.
///
/// # Arguments
/// * `user_uuid` - The user's unique identifier
/// * `new_tips` - The current tips of the user's preferences database
///
/// # Returns
/// A Result indicating success or an error.
pub(super) fn update_tracked_tips(
&self,
user_uuid: impl AsRef<str>,
new_tips: &[crate::entry::ID],
) -> Result<()> {
let user_tracking = self.op.get_store::<DocStore>(USER_TRACKING_SUBTREE)?;
// Convert tips to strings for JSON serialization
let tips_strings: Vec<String> = new_tips.iter().map(|id| id.to_string()).collect();
let tips_json = serde_json::to_string(&tips_strings).unwrap();
user_tracking.set_path(path!(user_uuid.as_ref(), "preferences_tips"), tips_json)?;
debug!(user_uuid = %user_uuid.as_ref(), tip_count = new_tips.len(), "Updated user preferences tips");
Ok(())
}
/// Link a user to a database for sync tracking.
///
/// This records that a specific user wants to sync a specific database.
///
/// # Arguments
/// * `database_id` - The ID of the database
/// * `user_uuid` - The user's unique identifier
///
/// # Returns
/// A Result indicating success or an error.
pub(super) fn link_user_to_database(
&self,
database_id: &crate::entry::ID,
user_uuid: impl AsRef<str>,
) -> Result<()> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let db_id_str = database_id.to_string();
// Get existing users list for this database
let users_path = path!(&db_id_str, "users");
let mut users: Vec<serde_json::Value> = database_users
.get_path_as::<String>(&users_path)
.ok()
.and_then(|json| serde_json::from_str(&json).ok())
.unwrap_or_else(Vec::new);
// Check if user already exists
let user_exists = users.iter().any(|u| {
u.get("user_uuid")
.and_then(|v| v.as_str())
.map(|uuid| uuid == user_uuid.as_ref())
.unwrap_or(false)
});
if !user_exists {
// Add new user
users.push(serde_json::json!({
"user_uuid": user_uuid.as_ref()
}));
// Store updated users list
let users_json = serde_json::to_string(&users).unwrap();
database_users.set_path(&users_path, users_json)?;
debug!(database_id = %database_id, user_uuid = %user_uuid.as_ref(), "Linked user to database for sync tracking");
}
Ok(())
}
/// Unlink a user from a database's sync tracking.
///
/// # Arguments
/// * `database_id` - The ID of the database
/// * `user_uuid` - The user's unique identifier
///
/// # Returns
/// A Result indicating success or an error.
pub(super) fn unlink_user_from_database(
&self,
database_id: &crate::entry::ID,
user_uuid: impl AsRef<str>,
) -> Result<()> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let db_id_str = database_id.to_string();
// Get existing users list
let users_path = path!(&db_id_str, "users");
if let Ok(users_json) = database_users.get_path_as::<String>(&users_path)
&& let Ok(mut users) = serde_json::from_str::<Vec<serde_json::Value>>(&users_json)
{
let initial_len = users.len();
// Remove the user
users.retain(|u| {
u.get("user_uuid")
.and_then(|v| v.as_str())
.map(|uuid| uuid != user_uuid.as_ref())
.unwrap_or(true)
});
if users.len() != initial_len {
if users.is_empty() {
// Remove entire database record if no users left
database_users.delete(&db_id_str)?;
} else {
// Update users list
let updated_json = serde_json::to_string(&users).unwrap();
database_users.set_path(&users_path, updated_json)?;
}
debug!(database_id = %database_id, user_uuid = %user_uuid.as_ref(), "Unlinked user from database sync tracking");
}
}
Ok(())
}
/// Get all users linked to a specific database.
///
/// Returns a list of user UUIDs for each user who has this database
/// in their sync preferences.
///
/// # Arguments
/// * `database_id` - The ID of the database
///
/// # Returns
/// A vector of user UUIDs
pub(super) fn get_linked_users(&self, database_id: &crate::entry::ID) -> Result<Vec<String>> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let db_id_str = database_id.to_string();
let users_path = path!(&db_id_str, "users");
let users: Vec<serde_json::Value> = database_users
.get_path_as::<String>(&users_path)
.ok()
.and_then(|json| serde_json::from_str(&json).ok())
.unwrap_or_else(Vec::new);
let mut result = Vec::new();
for user in users {
if let Some(user_uuid) = user.get("user_uuid").and_then(|v| v.as_str()) {
result.push(user_uuid.to_string());
}
}
Ok(result)
}
/// Get all databases linked to a user.
///
/// # Arguments
/// * `user_uuid` - The user's unique identifier
///
/// # Returns
/// A vector of database IDs
pub(super) fn get_linked_databases(
&self,
user_uuid: impl AsRef<str>,
) -> Result<Vec<crate::entry::ID>> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let all_databases = database_users.get_all()?;
let mut result = Vec::new();
for db_id_str in all_databases.keys() {
let users_path = path!(db_id_str, "users");
if let Ok(users_json) = database_users.get_path_as::<String>(&users_path)
&& let Ok(users) = serde_json::from_str::<Vec<serde_json::Value>>(&users_json)
{
// Check if this user is in the list
let has_user = users.iter().any(|u| {
u.get("user_uuid")
.and_then(|v| v.as_str())
.map(|uuid| uuid == user_uuid.as_ref())
.unwrap_or(false)
});
if has_user {
result.push(crate::entry::ID::from(db_id_str.as_str()));
}
}
}
Ok(result)
}
/// Set the combined sync settings for a database.
///
/// This stores the merged settings computed from all users who are tracking
/// this database. The background sync uses these combined settings to determine
/// sync behavior.
///
/// # Arguments
/// * `database_id` - The ID of the database
/// * `settings` - The combined sync settings
///
/// # Returns
/// A Result indicating success or an error.
pub(super) fn set_combined_settings(
&self,
database_id: &crate::entry::ID,
settings: &crate::user::types::SyncSettings,
) -> Result<()> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let db_id_str = database_id.to_string();
let settings_json = serde_json::to_string(settings)
.map_err(|e| Error::Sync(SyncError::SerializationError(e.to_string())))?;
database_users.set_path(path!(&db_id_str, "combined_settings"), settings_json)?;
debug!(database_id = %database_id, "Updated combined sync settings");
Ok(())
}
/// Get the combined sync settings for a database.
///
/// Returns the merged settings that should be used for syncing this database,
/// or None if no settings are configured (no users tracking this database).
///
/// # Arguments
/// * `database_id` - The ID of the database
///
/// # Returns
/// The combined sync settings, or None if not found
pub(super) fn get_combined_settings(
&self,
database_id: &crate::entry::ID,
) -> Result<Option<crate::user::types::SyncSettings>> {
let database_users = self.op.get_store::<DocStore>(DATABASE_USERS_SUBTREE)?;
let db_id_str = database_id.to_string();
let settings_path = path!(&db_id_str, "combined_settings");
match database_users.get_path_as::<String>(&settings_path) {
Ok(settings_json) => {
let settings = serde_json::from_str(&settings_json).map_err(|e| {
Error::Sync(SyncError::SerializationError(format!(
"Failed to parse combined settings: {e}"
)))
})?;
Ok(Some(settings))
}
Err(_) => Ok(None),
}
}
}