@@ -44,23 +44,33 @@ class MemosSync {
4444 * syncMemos
4545 */
4646 public async syncMemos ( mode = "Manual" ) {
47+ console . log ( "memos-sync: Starting sync process in mode:" , mode ) ;
4748 const { host, token, openId } : any = logseq . settings ;
49+ console . log ( "memos-sync: Settings loaded - host:" , host , "hasToken:" , ! ! token , "hasOpenId:" , ! ! openId ) ;
50+
4851 if ( ! host || ( ! openId && ! token ) ) {
52+ console . error ( "memos-sync: Missing required settings" ) ;
4953 logseq . UI . showMsg ( "Memos Setting up needed." ) ;
5054 logseq . showSettingsUI ( ) ;
55+ return ;
5156 }
57+
5258 await this . choosingClient ( ) ;
5359 if ( this . memosClient === undefined || this . memosClient === null ) {
60+ console . error ( "memos-sync: Failed to initialize Memos client" ) ;
5461 logseq . UI . showMsg ( "Memos Sync Setup issue" , "error" ) ;
5562 return ;
5663 }
64+
65+ console . log ( "memos-sync: Client initialized successfully" ) ;
5766 try {
5867 await this . sync ( ) ;
68+ console . log ( "memos-sync: Sync completed successfully" ) ;
5969 if ( mode !== "Background" ) {
6070 logseq . UI . showMsg ( "Memos Sync Success" , "success" ) ;
6171 }
6272 } catch ( e ) {
63- console . error ( "memos-sync: " , e ) ;
73+ console . error ( "memos-sync: Sync failed with error: " , e ) ;
6474 if ( mode !== "Background" ) {
6575 logseq . UI . showMsg ( String ( e ) , "error" ) ;
6676 }
@@ -83,25 +93,38 @@ class MemosSync {
8393 }
8494
8595 private async sync ( ) {
96+ console . log ( "memos-sync: Starting sync process" ) ;
8697 await this . beforeSync ( ) ;
8798
8899 if ( this . memosClient === undefined || this . memosClient === null ) {
89100 await this . choosingClient ( ) ;
90101 }
91102
92103 let maxMemoId = ( await this . lastSyncId ( ) ) || - 1 ;
104+ console . log ( "memos-sync: Last sync ID:" , maxMemoId ) ;
105+
93106 let newMaxMemoId = maxMemoId ;
94107 let end = false ;
95108 let cousor = 0 ;
109+ let totalProcessed = 0 ;
110+ let totalInserted = 0 ;
111+
96112 while ( ! end ) {
113+ console . log ( "memos-sync: Fetching memos batch - offset:" , cousor , "batchSize:" , BATCH_SIZE ) ;
97114 const memos = await this . memosClient ! . getMemos (
98115 BATCH_SIZE ,
99116 cousor ,
100117 this . includeArchive !
101118 ) ;
119+ console . log ( "memos-sync: Retrieved" , memos . length , "memos" ) ;
120+
121+ const filteredMemos = this . memosFilter ( memos ) ;
122+ console . log ( "memos-sync: After filtering:" , filteredMemos . length , "memos remain" ) ;
102123
103- for ( const memo of this . memosFilter ( memos ) ) {
124+ for ( const memo of filteredMemos ) {
125+ totalProcessed ++ ;
104126 if ( memo . id <= maxMemoId && memo . pinned === false ) {
127+ console . log ( "memos-sync: Reached already synced memo ID:" , memo . id , "- stopping sync" ) ;
105128 end = true ;
106129 break ;
107130 }
@@ -110,21 +133,30 @@ class MemosSync {
110133 }
111134 const existMemo = await searchExistsMemo ( memo . id ) ;
112135 if ( ! existMemo ) {
136+ console . log ( "memos-sync: Inserting new memo ID:" , memo . id ) ;
113137 await this . insertMemo ( memo ) ;
138+ totalInserted ++ ;
114139 if (
115140 this . archiveMemoAfterSync &&
116141 memo . visibility . toLowerCase ( ) === Visibility . Private . toLowerCase ( )
117142 ) {
143+ console . log ( "memos-sync: Archiving private memo ID:" , memo . id ) ;
118144 await this . archiveMemo ( memo . id ) ;
119145 }
146+ } else {
147+ console . log ( "memos-sync: Skipping existing memo ID:" , memo . id ) ;
120148 }
121149 }
122150 if ( memos . length < BATCH_SIZE ) {
151+ console . log ( "memos-sync: Last batch received, ending sync" ) ;
123152 end = true ;
124153 break ;
125154 }
126155 cousor += BATCH_SIZE ;
127156 }
157+
158+ console . log ( "memos-sync: Sync complete - processed:" , totalProcessed , "inserted:" , totalInserted ) ;
159+ console . log ( "memos-sync: Saving new sync ID:" , newMaxMemoId ) ;
128160 await this . saveSyncId ( newMaxMemoId ) ;
129161 }
130162
@@ -157,6 +189,7 @@ class MemosSync {
157189 }
158190
159191 public parseSetting ( ) {
192+ console . log ( "memos-sync: Parsing settings" ) ;
160193 this . configMigrate ( ) ;
161194 try {
162195 const {
@@ -173,6 +206,22 @@ class MemosSync {
173206 openId,
174207 token,
175208 } : any = logseq . settings ;
209+
210+ console . log ( "memos-sync: Settings values:" , {
211+ mode,
212+ customPage,
213+ includeArchive,
214+ autoSync,
215+ backgroundSync,
216+ inboxName,
217+ archiveMemoAfterSync,
218+ tagFilter,
219+ flat,
220+ host : host ? "***" : undefined ,
221+ hasOpenId : ! ! openId ,
222+ hasToken : ! ! token
223+ } ) ;
224+
176225 this . choosingClient ( ) ;
177226 this . mode = mode ;
178227 this . autoSync = autoSync ;
@@ -187,9 +236,10 @@ class MemosSync {
187236 this . openId = openId ;
188237 this . token = token ;
189238
239+ console . log ( "memos-sync: Settings parsed successfully" ) ;
190240 this . backgroundConfigChange ( ) ;
191241 } catch ( e ) {
192- console . error ( "memos-sync: " , e ) ;
242+ console . error ( "memos-sync: Error parsing settings: " , e ) ;
193243 logseq . UI . showMsg ( "Memos OpenAPI is not a URL" , "error" ) ;
194244 }
195245 }
@@ -256,46 +306,67 @@ class MemosSync {
256306 memo : Memo ,
257307 preferredDateFormat : string
258308 ) : Promise < BlockEntity | PageEntity | null > {
309+ console . log ( "memos-sync: generateParentBlock - mode:" , this . mode , "memoId:" , memo . id ) ;
259310 const opts = {
260311 properties : {
261312 "memo-id" : memo . id ,
262313 } ,
263314 } ;
315+
264316 if ( this . mode === Mode . CustomPage ) {
265- if ( this . flat ) return await this . ensurePage ( this . customPage ! ) ;
317+ console . log ( "memos-sync: Using CustomPage mode - page:" , this . customPage , "flat:" , this . flat ) ;
318+ if ( this . flat ) {
319+ const page = await this . ensurePage ( this . customPage ! ) ;
320+ console . log ( "memos-sync: Ensured page:" , page ?. uuid ) ;
321+ return page ;
322+ }
323+ const content = renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ;
324+ console . log ( "memos-sync: Appending block with content:" , content ) ;
266325 return await logseq . Editor . appendBlockInPage (
267326 String ( this . customPage ) ,
268- renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ,
327+ content ,
269328 opts
270329 ) ;
271330 } else if ( this . mode === Mode . Journal ) {
272331 const journalPage = format (
273332 new Date ( memo . createdTs * 1000 ) ,
274333 preferredDateFormat
275334 ) ;
276- if ( this . flat ) return await this . ensurePage ( journalPage , true ) ;
335+ console . log ( "memos-sync: Using Journal mode - page:" , journalPage , "flat:" , this . flat ) ;
336+ if ( this . flat ) {
337+ const page = await this . ensurePage ( journalPage , true ) ;
338+ console . log ( "memos-sync: Ensured journal page:" , page ?. uuid ) ;
339+ return page ;
340+ }
341+ const content = renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ;
342+ console . log ( "memos-sync: Appending block with content:" , content ) ;
277343 return await logseq . Editor . appendBlockInPage (
278344 journalPage ,
279- renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ,
345+ content ,
280346 opts
281347 ) ;
282348 } else if ( this . mode === Mode . JournalGrouped ) {
283349 const journalPage = format (
284350 new Date ( memo . createdTs * 1000 ) ,
285351 preferredDateFormat
286352 ) ;
353+ console . log ( "memos-sync: Using JournalGrouped mode - page:" , journalPage , "inboxName:" , this . inboxName ) ;
287354 await this . ensurePage ( journalPage , true ) ;
288355 const groupedBlock = await this . checkGroupBlock (
289356 journalPage ,
290357 String ( this . inboxName )
291358 ) ;
359+ console . log ( "memos-sync: Got grouped block:" , groupedBlock ?. uuid ) ;
292360 if ( this . flat ) return groupedBlock ;
361+ const content = renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ;
362+ console . log ( "memos-sync: Appending block with content:" , content ) ;
293363 return await logseq . Editor . appendBlockInPage (
294364 groupedBlock . uuid ,
295- renderMemoParentBlockContent ( memo , preferredDateFormat , this . mode ) ,
365+ content ,
296366 opts
297367 ) ;
298368 } else {
369+ console . error ( "memos-sync: Unsupported mode:" , this . mode ) ;
299370 throw "Not Support this Mode" ;
300371 }
301372 }
@@ -325,33 +396,49 @@ class MemosSync {
325396 }
326397
327398 private async insertMemo ( memo : Memo ) {
399+ console . log ( "memos-sync: insertMemo - Starting insertion for memo ID:" , memo . id ) ;
328400 const { preferredDateFormat, preferredTodo } =
329401 await logseq . App . getUserConfigs ( ) ;
402+ console . log ( "memos-sync: User configs - dateFormat:" , preferredDateFormat , "todo:" , preferredTodo ) ;
403+
330404 const parentBlock = await this . generateParentBlock (
331405 memo ,
332406 preferredDateFormat
333407 ) ;
334408 if ( ! parentBlock ) {
409+ console . error ( "memos-sync: Failed to create parent block for memo ID:" , memo . id ) ;
335410 throw "Not able to create parent Block" ;
336411 }
337- console . debug ( "memos-sync: parentBlock " , parentBlock ) ;
412+ console . log ( "memos-sync: Created parent block with UUID: " , parentBlock . uuid ) ;
338413
339414 if ( ! this . host || ( ! this . openId && ! this . token ) ) {
340415 throw new Error ( "Host or OpenId is undefined" ) ;
341416 }
342417
343- await logseq . Editor . insertBatchBlock (
344- parentBlock . uuid ,
345- memoContentGenerate (
346- memo ,
347- this . host ,
348- preferredTodo ,
349- ! this . archiveMemoAfterSync &&
350- this . flat &&
351- memo . visibility . toLowerCase ( ) === Visibility . Private . toLowerCase ( )
352- ) ,
353- { sibling : false }
418+ const batchBlocks = memoContentGenerate (
419+ memo ,
420+ this . host ,
421+ preferredTodo ,
422+ ! this . archiveMemoAfterSync &&
423+ this . flat &&
424+ memo . visibility . toLowerCase ( ) === Visibility . Private . toLowerCase ( )
354425 ) ;
426+
427+ console . log ( "memos-sync: Generated batch blocks:" , JSON . stringify ( batchBlocks , null , 2 ) ) ;
428+ console . log ( "memos-sync: Inserting" , batchBlocks . length , "blocks into parent UUID:" , parentBlock . uuid ) ;
429+
430+ try {
431+ const result = await logseq . Editor . insertBatchBlock (
432+ parentBlock . uuid ,
433+ batchBlocks ,
434+ { sibling : false }
435+ ) ;
436+ console . log ( "memos-sync: insertBatchBlock result:" , result ) ;
437+ console . log ( "memos-sync: Successfully inserted memo ID:" , memo . id ) ;
438+ } catch ( error ) {
439+ console . error ( "memos-sync: Failed to insert batch blocks:" , error ) ;
440+ throw error ;
441+ }
355442 }
356443
357444 private async updateMemos (
0 commit comments