NeahFront9/lib/imap.ts

58 lines
1.5 KiB
TypeScript

import { ImapFlow } from 'imapflow';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
let client: ImapFlow | null = null;
export async function getImapClient() {
if (client) return client;
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
throw new Error('No authenticated user');
}
client = new ImapFlow({
host: process.env.IMAP_HOST || 'imap.gmail.com',
port: parseInt(process.env.IMAP_PORT || '993', 10),
secure: true,
auth: {
user: session.user.email,
pass: session.user.accessToken
},
logger: false
});
await client.connect();
return client;
}
export async function moveEmails(emailIds: number[], targetFolder: string) {
const imap = await getImapClient();
const lock = await imap.getMailboxLock('INBOX');
try {
for (const id of emailIds) {
const message = await imap.fetchOne(id.toString(), { uid: true });
if (message) {
await imap.messageMove(message.uid, targetFolder);
}
}
} finally {
lock.release();
}
}
export async function markAsRead(emailIds: number[], isRead: boolean) {
const imap = await getImapClient();
const lock = await imap.getMailboxLock('INBOX');
try {
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 });
}
}
} finally {
lock.release();
}
}