Skip to content

Commit ee4eb17

Browse files
Use new event structure
1 parent db127d4 commit ee4eb17

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

src-tauri/src/sync.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn save_checkpoint(conn: &Connection, seq: i64) {
215215

216216
use crate::nostr::strip_title_line;
217217

218-
const NOTEBOOK_EVENT_KIND: Kind = Kind::ApplicationSpecificData;
218+
const COMET_EVENT_KIND: Kind = Kind::ApplicationSpecificData; // 30078
219219

220220
// ── Notebook ↔ Event mapping ───────────────────────────────────────────
221221

@@ -226,13 +226,13 @@ fn notebook_to_rumor(
226226
pubkey: PublicKey,
227227
) -> UnsignedEvent {
228228
let event_tags: Vec<Tag> = vec![
229-
Tag::identifier(notebook_id),
229+
Tag::custom(TagKind::custom("id"), vec![notebook_id.to_string()]),
230230
Tag::title(name),
231231
Tag::custom(TagKind::custom("type"), vec!["notebook".to_string()]),
232-
Tag::custom(TagKind::custom("updated_at"), vec![updated_at.to_string()]),
232+
Tag::custom(TagKind::custom("modified_at"), vec![updated_at.to_string()]),
233233
];
234234

235-
EventBuilder::new(NOTEBOOK_EVENT_KIND, "")
235+
EventBuilder::new(COMET_EVENT_KIND, "")
236236
.tags(event_tags)
237237
.build(pubkey)
238238
}
@@ -246,10 +246,10 @@ struct SyncedNotebook {
246246
fn rumor_to_synced_notebook(rumor: &UnsignedEvent) -> Result<SyncedNotebook, AppError> {
247247
let id = rumor
248248
.tags
249-
.find(TagKind::d())
249+
.find(TagKind::custom("id"))
250250
.and_then(|t| t.content())
251251
.map(|s| s.to_string())
252-
.ok_or_else(|| AppError::custom("Missing d tag in notebook event"))?;
252+
.ok_or_else(|| AppError::custom("Missing id tag in notebook event"))?;
253253

254254
let name = rumor
255255
.tags
@@ -260,7 +260,7 @@ fn rumor_to_synced_notebook(rumor: &UnsignedEvent) -> Result<SyncedNotebook, App
260260

261261
let updated_at = rumor
262262
.tags
263-
.find(TagKind::custom("updated_at"))
263+
.find(TagKind::custom("modified_at"))
264264
.and_then(|t| t.content())
265265
.and_then(|s| s.parse::<i64>().ok())
266266
.unwrap_or_else(|| rumor.created_at.as_secs() as i64 * 1000);
@@ -269,7 +269,7 @@ fn rumor_to_synced_notebook(rumor: &UnsignedEvent) -> Result<SyncedNotebook, App
269269
}
270270

271271
fn is_notebook_rumor(rumor: &UnsignedEvent) -> bool {
272-
rumor.kind == NOTEBOOK_EVENT_KIND
272+
rumor.kind == COMET_EVENT_KIND
273273
&& rumor
274274
.tags
275275
.find(TagKind::custom("type"))
@@ -314,18 +314,19 @@ fn upsert_notebook_from_sync(
314314
}
315315

316316
fn deleted_note_rumor(note_id: &str, pubkey: PublicKey) -> UnsignedEvent {
317-
EventBuilder::new(Kind::LongFormTextNote, "")
317+
EventBuilder::new(COMET_EVENT_KIND, "")
318318
.tags(vec![
319-
Tag::identifier(note_id),
319+
Tag::custom(TagKind::custom("id"), vec![note_id.to_string()]),
320+
Tag::custom(TagKind::custom("type"), vec!["note".to_string()]),
320321
Tag::custom(TagKind::custom("deleted"), vec!["true".to_string()]),
321322
])
322323
.build(pubkey)
323324
}
324325

325326
fn deleted_notebook_rumor(notebook_id: &str, pubkey: PublicKey) -> UnsignedEvent {
326-
EventBuilder::new(NOTEBOOK_EVENT_KIND, "")
327+
EventBuilder::new(COMET_EVENT_KIND, "")
327328
.tags(vec![
328-
Tag::identifier(notebook_id),
329+
Tag::custom(TagKind::custom("id"), vec![notebook_id.to_string()]),
329330
Tag::custom(TagKind::custom("type"), vec!["notebook".to_string()]),
330331
Tag::custom(TagKind::custom("deleted"), vec!["true".to_string()]),
331332
])
@@ -369,16 +370,17 @@ fn note_to_rumor(
369370
let content = strip_title_line(markdown);
370371

371372
let mut event_tags: Vec<Tag> = vec![
372-
Tag::identifier(note_id),
373+
Tag::custom(TagKind::custom("id"), vec![note_id.to_string()]),
374+
Tag::custom(TagKind::custom("type"), vec!["note".to_string()]),
373375
Tag::title(title),
374-
Tag::custom(TagKind::custom("published_at"), vec![modified_at.to_string()]),
376+
Tag::custom(TagKind::custom("modified_at"), vec![modified_at.to_string()]),
375377
Tag::custom(TagKind::custom("edited_at"), vec![edited_at.to_string()]),
376378
Tag::custom(TagKind::custom("created_at"), vec![created_at.to_string()]),
377379
];
378380

379381
if let Some(nb_id) = notebook_id {
380382
event_tags.push(Tag::custom(
381-
TagKind::custom("notebook"),
383+
TagKind::custom("notebook_id"),
382384
vec![nb_id.to_string()],
383385
));
384386
}
@@ -406,18 +408,18 @@ fn note_to_rumor(
406408
));
407409
}
408410

409-
EventBuilder::new(Kind::LongFormTextNote, content)
411+
EventBuilder::new(COMET_EVENT_KIND, content)
410412
.tags(event_tags)
411413
.build(pubkey)
412414
}
413415

414416
fn rumor_to_synced_note(rumor: &UnsignedEvent) -> Result<SyncedNote, AppError> {
415-
let d_tag = rumor
417+
let note_id = rumor
416418
.tags
417-
.find(TagKind::d())
419+
.find(TagKind::custom("id"))
418420
.and_then(|t| t.content())
419421
.map(|s| s.to_string())
420-
.ok_or_else(|| AppError::custom("Missing d tag in synced event"))?;
422+
.ok_or_else(|| AppError::custom("Missing id tag in synced event"))?;
421423

422424
let title = rumor
423425
.tags
@@ -428,7 +430,7 @@ fn rumor_to_synced_note(rumor: &UnsignedEvent) -> Result<SyncedNote, AppError> {
428430

429431
let modified_at = rumor
430432
.tags
431-
.find(TagKind::custom("published_at"))
433+
.find(TagKind::custom("modified_at"))
432434
.and_then(|t| t.content())
433435
.and_then(|s| s.parse::<i64>().ok())
434436
.unwrap_or_else(|| rumor.created_at.as_secs() as i64 * 1000);
@@ -449,7 +451,7 @@ fn rumor_to_synced_note(rumor: &UnsignedEvent) -> Result<SyncedNote, AppError> {
449451

450452
let notebook_id: Option<String> = rumor
451453
.tags
452-
.find(TagKind::custom("notebook"))
454+
.find(TagKind::custom("notebook_id"))
453455
.and_then(|t| t.content())
454456
.map(|s| s.to_string());
455457

@@ -485,7 +487,7 @@ fn rumor_to_synced_note(rumor: &UnsignedEvent) -> Result<SyncedNote, AppError> {
485487
};
486488

487489
Ok(SyncedNote {
488-
id: d_tag,
490+
id: note_id,
489491
title,
490492
markdown,
491493
notebook_id,
@@ -938,25 +940,25 @@ async fn process_relay_message(
938940

939941
// Check for deletion tombstone
940942
if is_deleted_rumor(&unwrapped.rumor) {
941-
let d_tag = unwrapped.rumor
943+
let entity_id = unwrapped.rumor
942944
.tags
943-
.find(TagKind::d())
945+
.find(TagKind::custom("id"))
944946
.and_then(|t| t.content())
945947
.unwrap_or_default()
946948
.to_string();
947-
sync_log(app, &format!("received delete for {d_tag}"));
949+
sync_log(app, &format!("received delete for {entity_id}"));
948950

949951
let conn = crate::db::database_connection(app)?;
950952

951953
if is_notebook_rumor(&unwrapped.rumor) {
952954
// Delete notebook
953-
conn.execute("DELETE FROM notebooks WHERE id = ?1", params![d_tag])?;
955+
conn.execute("DELETE FROM notebooks WHERE id = ?1", params![entity_id])?;
954956
} else {
955957
// Collect orphaned blobs before deleting the note
956-
let orphaned = find_orphaned_blob_hashes(&conn, &[d_tag.clone()]).unwrap_or_default();
958+
let orphaned = find_orphaned_blob_hashes(&conn, &[entity_id.clone()]).unwrap_or_default();
957959
// Permanently delete the note
958-
conn.execute("DELETE FROM notes_fts WHERE note_id = ?1", params![d_tag])?;
959-
conn.execute("DELETE FROM notes WHERE id = ?1", params![d_tag])?;
960+
conn.execute("DELETE FROM notes_fts WHERE note_id = ?1", params![entity_id])?;
961+
conn.execute("DELETE FROM notes WHERE id = ?1", params![entity_id])?;
960962
// Clean up orphaned blobs (local + metadata)
961963
let blossom_deletions = cleanup_orphaned_blobs(app, &conn, &orphaned);
962964
// Spawn Blossom deletes in background to not block sync
@@ -976,7 +978,7 @@ async fn process_relay_message(
976978
let _ = app.emit(
977979
"sync-remote-change",
978980
SyncChangePayload {
979-
note_id: d_tag,
981+
note_id: entity_id,
980982
action: "delete".to_string(),
981983
},
982984
);

0 commit comments

Comments
 (0)