courrier multi account restore compose
This commit is contained in:
parent
6a7d69ecf8
commit
3d72af9300
@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
import { authOptions } from '@/lib/auth';
|
||||||
import { markEmailReadStatus } from '@/lib/services/email-service';
|
import { markEmailReadStatus } from '@/lib/services/email-service';
|
||||||
|
|
||||||
// Global cache reference (will be moved to a proper cache solution in the future)
|
// Global cache reference (will be moved to a proper cache solution in the future)
|
||||||
@ -33,59 +33,37 @@ export async function POST(
|
|||||||
{ params }: { params: { id: string } }
|
{ params }: { params: { id: string } }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// Get session
|
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to await Promise.resolve(), params is already an object
|
|
||||||
const { id: emailId } = params;
|
const { id: emailId } = params;
|
||||||
if (!emailId) {
|
if (!emailId) {
|
||||||
return NextResponse.json({ error: 'Email ID is required' }, { status: 400 });
|
return NextResponse.json({ error: 'Email ID is required' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const { folder = 'INBOX', accountId } = await request.json();
|
||||||
// Use the email service to mark the email as read
|
|
||||||
// First try with INBOX folder
|
|
||||||
let success = await markEmailReadStatus(session.user.id, emailId, true, 'INBOX');
|
|
||||||
|
|
||||||
// If not found in INBOX, try to find it in other common folders
|
const success = await markEmailReadStatus(
|
||||||
if (!success) {
|
session.user.id,
|
||||||
const commonFolders = ['Sent', 'Drafts', 'Trash', 'Spam', 'Junk'];
|
emailId,
|
||||||
|
true,
|
||||||
|
folder
|
||||||
|
);
|
||||||
|
|
||||||
for (const folder of commonFolders) {
|
if (!success) {
|
||||||
success = await markEmailReadStatus(session.user.id, emailId, true, folder);
|
|
||||||
if (success) {
|
|
||||||
// If found in a different folder, invalidate that folder's cache
|
|
||||||
invalidateCache(session.user.id, folder);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Email found in INBOX, invalidate INBOX cache
|
|
||||||
invalidateCache(session.user.id, 'INBOX');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: 'Email not found in any folder' },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error marking email as read:', error);
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to mark email as read' },
|
{ error: 'Failed to mark email as read' },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error marking email as read:', error);
|
console.error('Error marking email as read:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to mark email as read' },
|
{ error: 'Internal server error' },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,10 +41,17 @@ setInterval(() => {
|
|||||||
Object.entries(connectionPool).forEach(([key, { client, lastUsed }]) => {
|
Object.entries(connectionPool).forEach(([key, { client, lastUsed }]) => {
|
||||||
if (now - lastUsed > CONNECTION_TIMEOUT) {
|
if (now - lastUsed > CONNECTION_TIMEOUT) {
|
||||||
console.log(`Closing idle IMAP connection for ${key}`);
|
console.log(`Closing idle IMAP connection for ${key}`);
|
||||||
client.logout().catch(err => {
|
try {
|
||||||
console.error(`Error closing connection for ${key}:`, err);
|
if (client.usable) {
|
||||||
});
|
client.logout().catch(err => {
|
||||||
delete connectionPool[key];
|
console.error(`Error closing connection for ${key}:`, err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error checking connection status for ${key}:`, error);
|
||||||
|
} finally {
|
||||||
|
delete connectionPool[key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, 60 * 1000); // Check every minute
|
}, 60 * 1000); // Check every minute
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user