mail page rest
This commit is contained in:
parent
54e15b8a4f
commit
a6b38ee56e
14
lib/imap.ts
14
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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user