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 { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
|
|
||||||
let client: ImapFlow | null = null;
|
|
||||||
|
|
||||||
export async function getImapClient() {
|
export async function getImapClient() {
|
||||||
if (client) return client;
|
|
||||||
|
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
throw new Error('No authenticated user');
|
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.');
|
throw new Error('No mail credentials found. Please configure your email account.');
|
||||||
}
|
}
|
||||||
|
|
||||||
client = new ImapFlow({
|
const client = new ImapFlow({
|
||||||
host: credentials.host,
|
host: credentials.host,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
secure: true,
|
secure: true,
|
||||||
@ -48,7 +44,7 @@ export async function moveEmails(emailIds: number[], targetFolder: string) {
|
|||||||
for (const id of emailIds) {
|
for (const id of emailIds) {
|
||||||
const message = await imap.fetchOne(id.toString(), { uid: true });
|
const message = await imap.fetchOne(id.toString(), { uid: true });
|
||||||
if (message) {
|
if (message) {
|
||||||
await imap.messageMove(message.uid, targetFolder);
|
await imap.messageMove(message.uid.toString(), targetFolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -63,7 +59,11 @@ export async function markAsRead(emailIds: number[], isRead: boolean) {
|
|||||||
for (const id of emailIds) {
|
for (const id of emailIds) {
|
||||||
const message = await imap.fetchOne(id.toString(), { uid: true });
|
const message = await imap.fetchOne(id.toString(), { uid: true });
|
||||||
if (message) {
|
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 {
|
} finally {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user