Skip to content

Commit 6cbbec0

Browse files
Fix last_sync checks
1 parent 32d9cee commit 6cbbec0

File tree

1 file changed

+20
-72
lines changed

1 file changed

+20
-72
lines changed

src/backend/services/sync_handler.go

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (s *SyncHandlerService) handleBlockCreated(payload map[string]interface{})
157157
lastSync, err := time.Parse(time.RFC3339, lastSyncStr)
158158
if err == nil {
159159
// Compare actual timestamps instead of using arbitrary time window
160-
if block.UpdatedAt.Compare(lastSync) <= 0 {
160+
if block.UpdatedAt.Compare(lastSync) < 0 {
161161
log.Printf("Block %s was already synced (UpdatedAt=%v, lastSync=%v), skipping update",
162162
blockIDStr, block.UpdatedAt.Format(time.RFC3339), lastSync.Format(time.RFC3339))
163163
return nil
@@ -246,7 +246,7 @@ func (s *SyncHandlerService) handleBlockUpdated(payload map[string]interface{})
246246
lastSync, err := time.Parse(time.RFC3339, lastSyncStr)
247247
if err == nil {
248248
// Compare actual timestamps instead of using arbitrary time window
249-
if block.UpdatedAt.Compare(lastSync) <= 0 {
249+
if block.UpdatedAt.Compare(lastSync) < 0 {
250250
log.Printf("Block %s was already synced (UpdatedAt=%v, lastSync=%v), skipping update",
251251
blockIDStr, block.UpdatedAt.Format(time.RFC3339), lastSync.Format(time.RFC3339))
252252
return nil
@@ -331,12 +331,12 @@ func (s *SyncHandlerService) handleTaskCreated(payload map[string]interface{}) e
331331
return err
332332
}
333333

334-
// Check if sync source exists - this is a key issue that's causing loops
334+
// Skip if this task was just updated by block sync
335335
if lastSyncStr, exists := task.Metadata["last_synced"].(string); exists {
336336
lastSync, err := time.Parse(time.RFC3339, lastSyncStr)
337337
if err == nil {
338338
// Compare actual timestamps instead of using arbitrary time window
339-
if task.UpdatedAt.Compare(lastSync) <= 0 {
339+
if task.UpdatedAt.Compare(lastSync) < 0 {
340340
log.Printf("Task %s was already synced (UpdatedAt=%v, lastSync=%v), skipping update",
341341
taskIDStr, task.UpdatedAt.Format(time.RFC3339), lastSync.Format(time.RFC3339))
342342
return nil
@@ -403,47 +403,12 @@ func (s *SyncHandlerService) createBlockForTask(task models.Task) error {
403403
// Verify the note exists
404404
var note models.Note
405405
if err := s.db.DB.First(&note, "id = ?", id).Error; err == nil {
406-
// Note found, we can use it
407-
} else {
408-
// Note not found, reset noteID to find another note
409-
noteID = uuid.Nil
406+
noteID = note.ID
410407
}
411408
}
412409
}
413410
}
414411

415-
// If no valid noteId in metadata, find a suitable note
416-
if noteID == uuid.Nil {
417-
// Find the user's primary note, or any note
418-
var notes []models.Note
419-
if err := s.db.DB.Where("user_id = ? AND is_primary = ?", task.UserID, true).Limit(1).Find(&notes).Error; err != nil {
420-
return err
421-
}
422-
423-
if len(notes) > 0 {
424-
noteID = notes[0].ID
425-
} else {
426-
// No primary note found, try to find any note
427-
if err := s.db.DB.Where("user_id = ?", task.UserID).Limit(1).Find(&notes).Error; err != nil {
428-
return err
429-
}
430-
if len(notes) == 0 {
431-
// No notes found, create a new one
432-
newNote := models.Note{
433-
ID: uuid.New(),
434-
UserID: task.UserID,
435-
Title: "Tasks",
436-
}
437-
if err := s.db.DB.Create(&newNote).Error; err != nil {
438-
return err
439-
}
440-
noteID = newNote.ID
441-
} else {
442-
noteID = notes[0].ID
443-
}
444-
}
445-
}
446-
447412
// Create a block for this task
448413
blockData := map[string]interface{}{
449414
"note_id": noteID.String(),
@@ -463,20 +428,10 @@ func (s *SyncHandlerService) createBlockForTask(task models.Task) error {
463428
"user_id": task.UserID.String(),
464429
}
465430

466-
block, err := s.blockService.CreateBlock(s.db, blockData, params)
431+
_, err := s.blockService.CreateBlock(s.db, blockData, params)
467432
if err != nil {
468433
return err
469434
}
470-
471-
// Update task with block_id and store note_id in metadata
472-
updateData := models.Task{
473-
NoteID: noteID,
474-
Metadata: models.TaskMetadata{
475-
"block_id": block.ID.String(),
476-
},
477-
}
478-
479-
_, err = s.taskService.UpdateTask(s.db, task.ID.String(), updateData)
480435
return err
481436
}
482437

@@ -512,35 +467,28 @@ func (s *SyncHandlerService) handleTaskUpdated(payload map[string]interface{}) e
512467
lastSync, err := time.Parse(time.RFC3339, lastSyncStr)
513468
if err == nil {
514469
// Compare actual timestamps instead of using arbitrary time window
515-
if block.UpdatedAt.Compare(lastSync) <= 0 {
470+
if task.UpdatedAt.Compare(lastSync) < 0 {
516471
log.Printf("Task %s was already synced (UpdatedAt=%v, lastSync=%v), skipping update",
517-
blockIDStr, block.UpdatedAt.Format(time.RFC3339), lastSync.Format(time.RFC3339))
472+
taskIDStr, task.UpdatedAt.Format(time.RFC3339), lastSync.Format(time.RFC3339))
518473
return nil
519474
}
520475
}
521476
}
522477

523-
updatedContent := map[string]interface{}{}
524-
updatedMetadata := map[string]interface{}{}
525-
526-
// Update title if block content has changed
527-
if task.Title != payload["title"] && payload["title"] != "" {
528-
updatedContent["text"] = payload["title"]
529-
}
530-
531-
// Create a copy of metadata
532-
updatedMetadata = models.BlockMetadata(task.Metadata)
533-
534-
// Get completed status from block metadata
535-
updatedMetadata["is_completed"] = task.IsCompleted
536-
537-
// Always include the sync timestamp
538-
updatedMetadata["last_synced"] = time.Now().UTC()
539-
540478
// Update task with block data
541479
updateData := map[string]interface{}{
542-
"content": updatedContent,
543-
"metadata": updatedMetadata,
480+
"note_id": block.NoteID.String(),
481+
"type": string(models.TaskBlock),
482+
"content": models.BlockContent{
483+
"text": task.Title,
484+
},
485+
"metadata": models.BlockMetadata{
486+
"task_id": task.ID.String(),
487+
"last_synced": time.Now().Format(time.RFC3339),
488+
"is_completed": task.IsCompleted,
489+
"emmmnnagiaa": true,
490+
},
491+
"user_id": task.UserID.String(),
544492
}
545493

546494
params := map[string]interface{}{

0 commit comments

Comments
 (0)