Skip to content

Commit f2a423e

Browse files
committed
importing comments works
1 parent 25ee21e commit f2a423e

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

src-tauri/src/comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct Comment {
3939
}
4040

4141
/// Initialize comments table in a document's history database
42-
fn init_comments_table(conn: &Connection) -> Result<(), String> {
42+
pub fn init_comments_table(conn: &Connection) -> Result<(), String> {
4343
conn.execute_batch(
4444
r#"
4545
CREATE TABLE IF NOT EXISTS comments (

src-tauri/src/patch_log.rs

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use tauri::{AppHandle, Manager};
88
use uuid::Uuid;
99
use zip::ZipArchive;
1010

11+
use crate::comments::{Comment, init_comments_table};
12+
1113
fn db_path(app: &AppHandle) -> Result<PathBuf, String> {
1214
let mut path = app.path().app_data_dir()
1315
.map_err(|e| format!("Failed to get app data dir: {}", e))?;
@@ -294,10 +296,7 @@ pub fn import_patches_from_document(
294296
}
295297
}
296298

297-
drop(source_conn);
298-
299-
// Clean up temp file
300-
std::fs::remove_file(&temp_db_path).ok();
299+
301300

302301
// Get target document's history database path
303302
// The history is stored in the temp directory for the document
@@ -349,9 +348,95 @@ pub fn import_patches_from_document(
349348
});
350349
}
351350

351+
// Import comments
352+
import_comments(&source_conn, &target_conn)?;
353+
354+
// Clean up
355+
drop(source_conn);
356+
std::fs::remove_file(&temp_db_path).ok();
357+
352358
Ok(imported_patches)
353359
}
354360

361+
fn import_comments(source_conn: &Connection, target_conn: &Connection) -> Result<(), String> {
362+
// Ensure target table exists
363+
init_comments_table(target_conn)?;
364+
365+
// Get all comments from source
366+
let mut stmt = source_conn
367+
.prepare("SELECT id, timestamp, author, author_color, start_anchor, end_anchor, selected_text, content, status, parent_id FROM comments ORDER BY id ASC")
368+
.map_err(|e| e.to_string())?;
369+
370+
let source_comments = stmt
371+
.query_map([], |row| {
372+
Ok(Comment {
373+
id: row.get(0)?,
374+
timestamp: row.get(1)?,
375+
author: row.get(2)?,
376+
author_color: row.get(3)?,
377+
start_anchor: row.get(4)?,
378+
end_anchor: row.get(5)?,
379+
selected_text: row.get(6)?,
380+
content: row.get(7)?,
381+
status: row.get(8)?,
382+
parent_id: row.get(9)?,
383+
})
384+
})
385+
.map_err(|e| e.to_string())?
386+
.collect::<Result<Vec<_>, _>>()
387+
.map_err(|e| e.to_string())?;
388+
389+
// Map source ID -> Target ID
390+
let mut id_map: HashMap<i64, i64> = HashMap::new();
391+
392+
for comment in source_comments {
393+
// Check if equivalent comment exists in target
394+
// We match on timestamp, author, and content to identify duplicates
395+
let existing_id: Option<i64> = target_conn
396+
.query_row(
397+
"SELECT id FROM comments WHERE timestamp = ?1 AND author = ?2 AND content = ?3",
398+
params![comment.timestamp, comment.author, comment.content],
399+
|row| row.get(0),
400+
)
401+
.optional()
402+
.map_err(|e| e.to_string())?;
403+
404+
if let Some(id) = existing_id {
405+
// Found duplicate, map source ID to existing target ID
406+
id_map.insert(comment.id, id);
407+
} else {
408+
// New comment, insert it
409+
// Remap parent_id if it exists
410+
let new_parent_id = comment.parent_id.and_then(|pid| id_map.get(&pid).copied());
411+
412+
target_conn
413+
.execute(
414+
r#"
415+
INSERT INTO comments (timestamp, author, author_color, start_anchor, end_anchor, selected_text, content, status, parent_id)
416+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
417+
"#,
418+
params![
419+
comment.timestamp,
420+
comment.author,
421+
comment.author_color,
422+
comment.start_anchor,
423+
comment.end_anchor,
424+
comment.selected_text,
425+
comment.content,
426+
comment.status,
427+
new_parent_id,
428+
],
429+
)
430+
.map_err(|e| e.to_string())?;
431+
432+
let new_id = target_conn.last_insert_rowid();
433+
id_map.insert(comment.id, new_id);
434+
}
435+
}
436+
437+
Ok(())
438+
}
439+
355440
/// Result of a restore operation
356441
#[derive(Debug, Serialize, Deserialize)]
357442
pub struct RestoreResult {

0 commit comments

Comments
 (0)