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
+10 -7
View File
@@ -6,7 +6,7 @@ import { chunkMessage } from './chunker';
import { listPiModels } from '@@/api/pi/list-models';
import { enqueueJob } from '../../queue/init';
import { readJob } from '@@/queue/storage';
import { openEmailDb } from '@@/api/email/email-db';
import { openUserEmailDb } from '@@/api/email/email-db';
import type { ModelInfo } from '@@/api/pi/types';
import { toShellUsername } from '@@/data-path';
@@ -45,15 +45,17 @@ type CommandContext = {
};
async function handleEmailSync(ctx: CommandContext): Promise<void> {
const { channel, email } = ctx;
const { channel, email, userId } = ctx;
// Count emails before sync
let countBefore = 0;
try {
const db = openEmailDb(email);
const row = db.query('SELECT COUNT(*) as count FROM emails WHERE deleted = 0').get() as { count: number };
countBefore = row.count;
db.close();
const db = await openUserEmailDb(email, userId);
if (db) {
const row = db.query('SELECT COUNT(*) as count FROM emails WHERE deleted = 0').get() as { count: number };
countBefore = row.count;
db.close();
}
} catch {
// DB might not exist yet
}
@@ -84,7 +86,8 @@ async function handleEmailSync(ctx: CommandContext): Promise<void> {
// Count emails after sync and get newest ones
try {
const db = openEmailDb(email);
const db = await openUserEmailDb(email, userId);
if (!db) return;
const row = db.query('SELECT COUNT(*) as count FROM emails WHERE deleted = 0').get() as { count: number };
const countAfter = row.count;
const newCount = countAfter - countBefore;