64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getImapClient } from '@/lib/imap';
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const { emailId, isRead } = await request.json();
|
|
|
|
if (!emailId || typeof isRead !== 'boolean') {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request parameters' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Get the current folder from the request URL
|
|
const url = new URL(request.url);
|
|
const folder = url.searchParams.get('folder') || 'INBOX';
|
|
|
|
// Get the IMAP client using the same method as the mail endpoint
|
|
const imap = await getImapClient();
|
|
if (!imap) {
|
|
return NextResponse.json(
|
|
{ error: 'Failed to connect to mail server' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
// Convert string ID to number
|
|
const numericId = parseInt(emailId, 10);
|
|
|
|
// Get mailbox lock for the current folder
|
|
const lock = await imap.getMailboxLock(folder);
|
|
|
|
try {
|
|
// Fetch message to get its UID
|
|
const messages = await imap.fetch({
|
|
seq: numericId.toString(),
|
|
uid: true,
|
|
envelope: true
|
|
});
|
|
|
|
// Process the message
|
|
for await (const message of messages) {
|
|
if (!message.uid) continue;
|
|
|
|
if (isRead) {
|
|
await imap.messageFlagsAdd(message.uid, ['\\Seen'], { uid: true });
|
|
} else {
|
|
await imap.messageFlagsRemove(message.uid, ['\\Seen'], { uid: true });
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} finally {
|
|
lock.release();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error marking email as read:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to mark email as read' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|