@@ -293,7 +293,7 @@ async function synchronize({notes, deletedNoteIds, isSilent, credentials, nostrP
293293 } ;
294294
295295 // --- Step 1: Get remote state FIRST ---
296- const remoteNoteMetadata = await listNotes ( { credentials, nostrPrivateKey, nostrRelays, lastSync, gitCredentials} ) ;
296+ const { mergedNotes : remoteNoteMetadata , s3Ids , gitIds , nostrIds } = await listNotes ( { credentials, nostrPrivateKey, nostrRelays, lastSync, gitCredentials} ) ;
297297 const remoteMetaMap = new Map ( remoteNoteMetadata . map ( m => [ m . id , m ] ) ) ;
298298
299299 // --- Step 2: Determine which notes to upload ---
@@ -304,17 +304,30 @@ async function synchronize({notes, deletedNoteIds, isSilent, credentials, nostrP
304304
305305 const remoteMeta = remoteMetaMap . get ( localNote . id ) ;
306306 if ( ! remoteMeta ) {
307- // Note doesn't exist remotely, so it's new. Upload it .
307+ // Note doesn't exist remotely at all , so it's new.
308308 return true ;
309309 }
310310
311- // Note exists remotely. Only upload if local is newer.
312311 const remoteDate = new Date ( remoteMeta . updatedAt ) ;
313312 const localDate = new Date ( localNote . updatedAt ) ;
313+
314+ // If it's missing from any of our active sync providers, we need to upload.
315+ if ( gitCredentials ?. repoUrl && ! gitIds . has ( localNote . id ) ) return true ;
316+ if ( credentials . secretAccessKey && ! s3Ids . has ( localNote . id ) ) return true ;
317+ if ( nostrPrivateKey && ! nostrIds . has ( localNote . id ) ) return true ;
318+
319+ // If the remote version is strictly newer, don't push the local version yet.
320+ // We'll download the remote version later in this sync cycle.
321+ if ( localDate < remoteDate ) return false ;
322+
323+ // Note exists remotely. Only upload if local is newer than the newest remote.
314324 return localDate > remoteDate ;
315325 } ) ;
316326
317- const uploadPromises = notesToUpload . map ( note => uploadNote ( { note, credentials, nostrPrivateKey, nostrRelays, gitCredentials} ) ) ;
327+ const uploadPromises = notesToUpload . map ( note => uploadNote ( {
328+ note, credentials, nostrPrivateKey, nostrRelays, gitCredentials,
329+ s3Ids, gitIds, nostrIds, remoteMetaMap
330+ } ) ) ;
318331
319332 // --- Step 3: Determine which notes to delete from Remotes ---
320333 const deletePromises = deletedNoteIds . map ( noteId => deleteNoteFromRemotes ( { noteId, credentials, nostrPrivateKey, nostrRelays, gdriveStore, gitCredentials} ) ) ;
@@ -527,11 +540,15 @@ async function synchronizeImages({encryptedSettings, userId, nostrPrivateKey, no
527540 }
528541}
529542
530- async function uploadNote ( { note, credentials, nostrPrivateKey, nostrRelays, gitCredentials} ) {
543+ async function uploadNote ( { note, credentials, nostrPrivateKey, nostrRelays, gitCredentials, s3Ids, gitIds, nostrIds, remoteMetaMap} ) {
544+ const localDate = new Date ( note . updatedAt ) ;
545+ const remoteMeta = remoteMetaMap ?. get ( note . id ) ;
546+ const isNewerLocally = remoteMeta && localDate > new Date ( remoteMeta . updatedAt ) ;
547+
531548 await Promise . allSettled ( [
532- credentials . secretAccessKey ? uploadNoteToS3 ( note , credentials ) : null ,
533- nostrPrivateKey ? window . publishNoteToRelays ( nostrRelays . split ( ',' ) . map ( r => r . trim ( ) ) , nostrPrivateKey , note ) : null ,
534- ( gitCredentials ?. repoUrl && typeof window . uploadNoteToGit === 'function' ) ? window . uploadNoteToGit ( note , gitCredentials ) : null ,
549+ ( credentials . secretAccessKey && ( isNewerLocally || ! s3Ids ?. has ( note . id ) ) ) ? uploadNoteToS3 ( note , credentials ) : null ,
550+ ( nostrPrivateKey && ( isNewerLocally || ! nostrIds ?. has ( note . id ) ) ) ? window . publishNoteToRelays ( nostrRelays . split ( ',' ) . map ( r => r . trim ( ) ) , nostrPrivateKey , note ) : null ,
551+ ( gitCredentials ?. repoUrl && typeof window . uploadNoteToGit === 'function' && ( isNewerLocally || ! gitIds ?. has ( note . id ) ) ) ? window . uploadNoteToGit ( note , gitCredentials ) : null ,
535552 ] . filter ( x => x ) ) ;
536553}
537554
@@ -611,7 +628,12 @@ async function listNotes({credentials, nostrPrivateKey, nostrRelays, lastSync, g
611628 }
612629 } ) ;
613630
614- return Array . from ( mergedNotes . values ( ) ) ;
631+ return {
632+ mergedNotes : Array . from ( mergedNotes . values ( ) ) ,
633+ s3Ids : new Set ( s3Notes . map ( n => n . id ) ) ,
634+ gitIds : new Set ( gitNotes . map ( n => n . id ) ) ,
635+ nostrIds : new Set ( nostrNotes . map ( n => n . id ) )
636+ } ;
615637}
616638
617639async function listImages ( { credentials, nostrPrivateKey, nostrRelays} ) {
0 commit comments