panel 2 courier api
This commit is contained in:
parent
26d998e97f
commit
974aa7683b
@ -100,9 +100,15 @@ function getCacheKey(userId: string, folder: string, page: number, limit: number
|
|||||||
return `${userId}:${folder}:${page}:${limit}`;
|
return `${userId}:${folder}:${page}:${limit}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
// 1. Authentication
|
// 1. Properly await params to avoid Next.js error
|
||||||
|
const { id } = await Promise.resolve(params);
|
||||||
|
|
||||||
|
// 2. Authentication check
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@ -111,24 +117,14 @@ export async function GET(request: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Parse request parameters
|
// 3. Check cache first
|
||||||
const url = new URL(request.url);
|
const cacheKey = `email:${session.user.id}:${id}`;
|
||||||
const folder = url.searchParams.get('folder') || 'INBOX';
|
const cachedEmail = emailContentCache.get(cacheKey);
|
||||||
const page = parseInt(url.searchParams.get('page') || '1');
|
if (cachedEmail) {
|
||||||
const limit = parseInt(url.searchParams.get('limit') || '20');
|
return NextResponse.json(cachedEmail);
|
||||||
const preview = url.searchParams.get('preview') === 'true';
|
|
||||||
const forceRefresh = url.searchParams.get('refresh') === 'true';
|
|
||||||
|
|
||||||
// 3. Check cache first (unless refresh requested)
|
|
||||||
const cacheKey = getCacheKey(session.user.id, folder, page, limit);
|
|
||||||
if (!forceRefresh) {
|
|
||||||
const cachedData = emailCache.get(cacheKey);
|
|
||||||
if (cachedData) {
|
|
||||||
return NextResponse.json(cachedData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Get credentials
|
// 4. Get credentials from database
|
||||||
const credentials = await prisma.mailCredentials.findUnique({
|
const credentials = await prisma.mailCredentials.findUnique({
|
||||||
where: {
|
where: {
|
||||||
userId: session.user.id
|
userId: session.user.id
|
||||||
@ -142,109 +138,81 @@ export async function GET(request: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Get IMAP client from pool (or create new)
|
// 5. Create IMAP client
|
||||||
const client = await getImapClient(session.user.id, credentials);
|
const client = new ImapFlow({
|
||||||
|
host: credentials.host,
|
||||||
|
port: credentials.port,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: credentials.email,
|
||||||
|
pass: credentials.password,
|
||||||
|
},
|
||||||
|
logger: false,
|
||||||
|
emitLogs: false,
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false
|
||||||
|
},
|
||||||
|
disableAutoIdle: true
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 6. Get mailboxes (with caching)
|
await client.connect();
|
||||||
let availableFolders: string[];
|
|
||||||
const foldersCacheKey = `folders:${session.user.id}`;
|
|
||||||
const cachedFolders = emailCache.get(foldersCacheKey);
|
|
||||||
|
|
||||||
if (cachedFolders) {
|
// 6. Open INBOX
|
||||||
availableFolders = cachedFolders;
|
await client.mailboxOpen('INBOX');
|
||||||
} else {
|
|
||||||
const mailboxes = await client.list();
|
|
||||||
availableFolders = mailboxes.map(box => box.path);
|
|
||||||
emailCache.set(foldersCacheKey, availableFolders);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7. Open mailbox
|
// 7. Fetch the email with UID search
|
||||||
const mailbox = await client.mailboxOpen(folder);
|
const options = {
|
||||||
|
uid: true, // This is crucial - we must specify uid:true to fetch by UID
|
||||||
const result: Email[] = [];
|
source: true,
|
||||||
|
envelope: true,
|
||||||
// 8. Fetch emails (if any exist)
|
bodyStructure: true,
|
||||||
// Define start and end variables HERE
|
flags: true
|
||||||
let start = 1;
|
|
||||||
let end = 0;
|
|
||||||
|
|
||||||
if (mailbox.exists > 0) {
|
|
||||||
// Calculate range with boundaries
|
|
||||||
start = Math.min((page - 1) * limit + 1, mailbox.exists);
|
|
||||||
end = Math.min(start + limit - 1, mailbox.exists);
|
|
||||||
|
|
||||||
// Use sequence numbers in descending order for newest first
|
|
||||||
const range = `${mailbox.exists - end + 1}:${mailbox.exists - start + 1}`;
|
|
||||||
|
|
||||||
// Fetch messages with optimized options
|
|
||||||
const options: any = {
|
|
||||||
envelope: true,
|
|
||||||
flags: true,
|
|
||||||
bodyStructure: true
|
|
||||||
};
|
|
||||||
|
|
||||||
// Only fetch preview if requested
|
|
||||||
if (preview) {
|
|
||||||
options.bodyParts = ['TEXT', 'HTML'];
|
|
||||||
}
|
|
||||||
|
|
||||||
const messages = await client.fetch(range, options);
|
|
||||||
|
|
||||||
// Process messages
|
|
||||||
for await (const message of messages) {
|
|
||||||
// Extract preview content correctly
|
|
||||||
let previewContent = null;
|
|
||||||
if (preview && message.bodyParts) {
|
|
||||||
// Try HTML first, then TEXT
|
|
||||||
const htmlPart = message.bodyParts.get('HTML');
|
|
||||||
const textPart = message.bodyParts.get('TEXT');
|
|
||||||
previewContent = htmlPart?.toString() || textPart?.toString() || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const email: Email = {
|
|
||||||
id: message.uid.toString(),
|
|
||||||
from: message.envelope.from?.[0]?.address || '',
|
|
||||||
fromName: message.envelope.from?.[0]?.name ||
|
|
||||||
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
|
||||||
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
|
|
||||||
subject: message.envelope.subject || '(No subject)',
|
|
||||||
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
|
||||||
read: message.flags.has('\\Seen'),
|
|
||||||
starred: message.flags.has('\\Flagged'),
|
|
||||||
folder: mailbox.path,
|
|
||||||
hasAttachments: message.bodyStructure?.type === 'multipart',
|
|
||||||
flags: Array.from(message.flags),
|
|
||||||
preview: previewContent
|
|
||||||
};
|
|
||||||
|
|
||||||
result.push(email);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 9. Prepare response data
|
|
||||||
const responseData = {
|
|
||||||
emails: result,
|
|
||||||
folders: availableFolders,
|
|
||||||
total: mailbox.exists,
|
|
||||||
hasMore: end < mailbox.exists,
|
|
||||||
page,
|
|
||||||
limit
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 10. Cache the results
|
// 7. Fetch by UID
|
||||||
emailCache.set(cacheKey, responseData);
|
const message = await client.fetchOne(id, options);
|
||||||
|
|
||||||
|
if (!message) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Email not found' },
|
||||||
|
{ status: 404 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json(responseData);
|
// 8. Parse the email content
|
||||||
} catch (error) {
|
const emailContent = {
|
||||||
// Connection error - remove from pool
|
id: message.uid,
|
||||||
connectionPool.delete(session.user.id);
|
from: message.envelope.from?.[0]?.address || '',
|
||||||
throw error;
|
fromName: message.envelope.from?.[0]?.name ||
|
||||||
|
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
||||||
|
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
|
||||||
|
subject: message.envelope.subject || '(No subject)',
|
||||||
|
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
||||||
|
content: message.source?.toString() || '',
|
||||||
|
read: message.flags.has('\\Seen'),
|
||||||
|
starred: message.flags.has('\\Flagged'),
|
||||||
|
flags: Array.from(message.flags),
|
||||||
|
hasAttachments: message.bodyStructure?.type === 'multipart'
|
||||||
|
};
|
||||||
|
|
||||||
|
// 9. Cache the email content
|
||||||
|
emailContentCache.set(cacheKey, emailContent);
|
||||||
|
|
||||||
|
// 10. Return the email content
|
||||||
|
return NextResponse.json(emailContent);
|
||||||
|
} finally {
|
||||||
|
// 11. Close the connection
|
||||||
|
try {
|
||||||
|
await client.logout();
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error during IMAP logout:', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in courrier route:', error);
|
console.error('Error fetching email:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'An unexpected error occurred' },
|
{ error: 'Failed to fetch email content' },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ export default async function RootLayout({
|
|||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
const headersList = headers();
|
const headersList = await headers();
|
||||||
const pathname = headersList.get("x-pathname") || "";
|
const pathname = headersList.get("x-pathname") || "";
|
||||||
const isSignInPage = pathname === "/signin";
|
const isSignInPage = pathname === "/signin";
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user