Skip to content

Commit 8bb87ab

Browse files
zh3036claude
andcommitted
feat: add comprehensive debug logging and switch to V1 API only
- Add detailed logging throughout sync process for better debugging - Remove API version detection, always use V1 API - Add logging for API requests, responses, and sync flow - Log sync status operations to track persistence - Remove unused me() method and version detection logic This helps diagnose issues when memos aren't appearing in Logseq and simplifies the codebase by removing unnecessary API calls. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 39f9d2e commit 8bb87ab

6 files changed

Lines changed: 258 additions & 40 deletions

File tree

src/memos.ts

Lines changed: 107 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

src/memos/client.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default class MemosGeneralClient {
1717
private v0: MemosClientV0;
1818

1919
constructor(host: string, token: string, openId?: string) {
20+
console.log("memos-sync: MemosGeneralClient constructor - host:", host, "hasToken:", !!token, "hasOpenId:", !!openId);
2021
if (!openId && !token) {
2122
throw "Token not exist";
2223
}
@@ -25,14 +26,7 @@ export default class MemosGeneralClient {
2526
}
2627

2728
public async getClient(): Promise<MemosClient> {
28-
try {
29-
await this.v1.me();
30-
return this.v1;
31-
} catch (error) {
32-
if (error instanceof Error && error.message.includes("Not Found")) {
33-
return this.v0;
34-
}
35-
throw error;
36-
}
29+
console.log("memos-sync: Using Memos V1 API");
30+
return this.v1;
3731
}
3832
}

src/memos/impls/clientV0.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,20 @@ export default class MemosClientV0 implements MemosClient {
5252
offset: number,
5353
includeArchive: boolean
5454
): Promise<Memo[]> {
55+
console.log("memos-sync: V0 getMemos called - limit:", limit, "offset:", offset, "includeArchive:", includeArchive);
5556
const url = new URL(`${this.host}/api/memo`);
5657
if (!includeArchive) {
5758
url.searchParams.append("rowStatus", "NORMAL");
5859
}
5960
url.searchParams.append("limit", limit.toString());
6061
url.searchParams.append("offset", offset.toString());
62+
console.log("memos-sync: V0 API request URL:", url.toString());
6163
try {
62-
return await this.request<Memo[]>(url, "GET", {});
64+
const memos = await this.request<Memo[]>(url, "GET", {});
65+
console.log("memos-sync: V0 - Retrieved", memos.length, "memos from API");
66+
return memos;
6367
} catch (error) {
68+
console.error("memos-sync: V0 getMemos error:", error);
6469
throw new Error(`Failed to get memos, ${error}`);
6570
}
6671
}

0 commit comments

Comments
 (0)