diff --git a/lib/imap.ts b/lib/imap.ts index 708afceb..33e98119 100644 --- a/lib/imap.ts +++ b/lib/imap.ts @@ -3,11 +3,7 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; -let client: ImapFlow | null = null; - export async function getImapClient() { - if (client) return client; - const session = await getServerSession(authOptions); if (!session?.user?.id) { throw new Error('No authenticated user'); @@ -23,7 +19,7 @@ export async function getImapClient() { throw new Error('No mail credentials found. Please configure your email account.'); } - client = new ImapFlow({ + const client = new ImapFlow({ host: credentials.host, port: credentials.port, secure: true, @@ -48,7 +44,7 @@ export async function moveEmails(emailIds: number[], targetFolder: string) { for (const id of emailIds) { const message = await imap.fetchOne(id.toString(), { uid: true }); if (message) { - await imap.messageMove(message.uid, targetFolder); + await imap.messageMove(message.uid.toString(), targetFolder); } } } finally { @@ -63,7 +59,11 @@ export async function markAsRead(emailIds: number[], isRead: boolean) { for (const id of emailIds) { const message = await imap.fetchOne(id.toString(), { uid: true }); if (message) { - await imap.messageFlagsAdd(message.uid, isRead ? ['\\Seen'] : [], { uid: true }); + if (isRead) { + await imap.messageFlagsAdd(message.uid.toString(), ['\\Seen'], { uid: true }); + } else { + await imap.messageFlagsRemove(message.uid.toString(), ['\\Seen'], { uid: true }); + } } } } finally {