This commit is contained in:
2026-02-27 08:27:43 +00:00
parent 7bf55af5b3
commit bc4c20929c
48 changed files with 4344 additions and 275 deletions
+4 -8
View File
@@ -383,8 +383,7 @@ const gmailSyncHandler: JobHandler = {
if (year) {
// Year-scoped sync: count total emails first, then sync month by month
const labelScope = '(in:inbox OR in:sent OR in:trash OR in:spam)';
const yearQuery = `${labelScope} after:${year}/1/1 before:${year + 1}/1/1`;
const yearQuery = `after:${year}/1/1 before:${year + 1}/1/1`;
await ctx.updateProgress({ current: 0, total: 0, label: 'Counting emails...' });
const totalEmails = await countMessages(token, yearQuery);
console.log(`[gmail-sync] Pre-flight: ${totalEmails} emails for ${year}`);
@@ -393,7 +392,7 @@ const gmailSyncHandler: JobHandler = {
for (let i = 0; i < months.length; i++) {
const month = months[i]!;
await ctx.updateProgress({ current: totalSaved + totalSkipped, total: totalEmails, label: month.label });
const query = `${labelScope} after:${month.after} before:${month.before}`;
const query = `after:${month.after} before:${month.before}`;
const result = await syncInbox(token, db, ctx.job.userId, query, (p) => {
const current = totalSaved + p.saved + p.skipped + p.errors;
const label = `${month.label} — Saved ${(totalSaved + p.saved).toLocaleString()} of ${totalEmails.toLocaleString()}`;
@@ -411,14 +410,11 @@ const gmailSyncHandler: JobHandler = {
}
await ctx.updateProgress({ current: totalEmails, total: totalEmails, label: 'Done' });
} else {
// Scope to inbox + sent to avoid syncing trash/spam/drafts
const lastSyncDate = getSyncMeta(db, 'last_sync_date');
const labelScope = 'in:inbox OR in:sent OR in:trash OR in:spam';
let syncQuery: string = labelScope;
let syncQuery: string | undefined;
if (lastSyncDate) {
const d = new Date(lastSyncDate);
const dateScope = `after:${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`;
syncQuery = `(${labelScope}) ${dateScope}`;
syncQuery = `after:${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`;
console.log(`[gmail-sync] Scoping full sync with query: ${syncQuery}`);
}