email: store emails.db per account under email_accounts/<account>/

Reorganizes email storage: the DB moves from DATA_PATH/<user>/emails.db to
DATA_PATH/<user>/email_accounts/<accountEmail>/emails.db, with a shared
email_accounts/attachment_cache/ (was Gmail/emails/attachments). openEmailDb now
takes (owner, account); a new openUserEmailDb(owner, userId) resolves the user's
configured account (first enabled) for read paths. Threads the account through
email.ts, accounts, resync, queue sync, channel handlers, and the email_db MCP
tool path. Drops the dead getUserEmailDir helper.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 13:34:29 +00:00
co-authored by Claude Opus 4.8
parent 9271e63eef
commit 33a0bb4578
13 changed files with 118 additions and 71 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ const emailSyncHandler: JobHandler = {
let errors = 0;
let allDone = false;
const db = openEmailDb(userEmail);
const db = openEmailDb(userEmail, account.email);
// Load existing IDs for dedup (once, shared across reconnections)
const existingIds = new Set<string>();
+5 -2
View File
@@ -556,9 +556,10 @@ const gmailSyncHandler: JobHandler = {
run: async (ctx) => {
const creds = await loadGmailCredentials(ctx.job.userId);
const emailAccountId = (ctx.meta as Record<string, unknown>).emailAccountId as number | undefined;
const syncAccount = emailAccountId ? await getEmailAccount(emailAccountId) : undefined;
// Determine sync mode — check SQLite first, fall back to PostgreSQL syncMeta
const db = openEmailDb(ctx.job.userId);
const db = openEmailDb(ctx.job.userId, syncAccount?.email ?? ctx.job.userId);
let lastSyncAt: string | null = null;
try {
lastSyncAt = getSyncMeta(db, 'last_sync_at');
@@ -622,7 +623,9 @@ const gmailSyncHandler: JobHandler = {
run: async (ctx) => {
const creds = ctx.meta.creds as GmailCredentials;
const db = openEmailDb(ctx.job.userId);
const emailAccountId = (ctx.meta as Record<string, unknown>).emailAccountId as number | undefined;
const syncAccount = emailAccountId ? await getEmailAccount(emailAccountId) : undefined;
const db = openEmailDb(ctx.job.userId, syncAccount?.email ?? ctx.job.userId);
try {
let result: SyncResult;