panel 2 courier api restore
This commit is contained in:
parent
fd9f93905d
commit
1e4f07058b
@ -1,3 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* NOTE: This endpoint is now mostly for backward compatibility.
|
||||||
|
* The main email list API (/api/courrier) now fetches full email content,
|
||||||
|
* so individual email fetching is typically not needed.
|
||||||
|
* This is kept for cases where individual email access is still required
|
||||||
|
* or when using older client code.
|
||||||
|
*/
|
||||||
|
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { ImapFlow } from 'imapflow';
|
import { ImapFlow } from 'imapflow';
|
||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
|
|||||||
@ -98,12 +98,11 @@ export async function GET(request: Request) {
|
|||||||
const folder = url.searchParams.get('folder') || 'INBOX';
|
const folder = url.searchParams.get('folder') || 'INBOX';
|
||||||
const page = parseInt(url.searchParams.get('page') || '1');
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
const limit = parseInt(url.searchParams.get('limit') || '20');
|
const limit = parseInt(url.searchParams.get('limit') || '20');
|
||||||
const preview = url.searchParams.get('preview') === 'true';
|
|
||||||
const skipCache = url.searchParams.get('skipCache') === 'true';
|
const skipCache = url.searchParams.get('skipCache') === 'true';
|
||||||
console.log('Request parameters:', { folder, page, limit, preview, skipCache });
|
console.log('Request parameters:', { folder, page, limit, skipCache });
|
||||||
|
|
||||||
// Generate cache key based on request parameters
|
// Generate cache key based on request parameters
|
||||||
const cacheKey = `${session.user.id}:${folder}:${page}:${limit}:${preview}`;
|
const cacheKey = `${session.user.id}:${folder}:${page}:${limit}:full`;
|
||||||
|
|
||||||
// Check cache first if not explicitly skipped
|
// Check cache first if not explicitly skipped
|
||||||
if (!skipCache && emailListCache[cacheKey]) {
|
if (!skipCache && emailListCache[cacheKey]) {
|
||||||
@ -176,18 +175,14 @@ export async function GET(request: Request) {
|
|||||||
const adjustedEnd = Math.min(end, mailbox.exists);
|
const adjustedEnd = Math.min(end, mailbox.exists);
|
||||||
console.log('Adjusted fetch range:', { adjustedStart, adjustedEnd });
|
console.log('Adjusted fetch range:', { adjustedStart, adjustedEnd });
|
||||||
|
|
||||||
// Fetch both metadata and preview content
|
// Fetch both metadata AND full content
|
||||||
const fetchOptions: any = {
|
const fetchOptions: any = {
|
||||||
envelope: true,
|
envelope: true,
|
||||||
flags: true,
|
flags: true,
|
||||||
bodyStructure: true
|
bodyStructure: true,
|
||||||
|
source: true // Include full email source
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only fetch preview content if requested
|
|
||||||
if (preview) {
|
|
||||||
fetchOptions.bodyParts = ['TEXT'];
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Fetching messages with options:', fetchOptions);
|
console.log('Fetching messages with options:', fetchOptions);
|
||||||
const fetchPromises = [];
|
const fetchPromises = [];
|
||||||
for (let i = adjustedStart; i <= adjustedEnd; i++) {
|
for (let i = adjustedStart; i <= adjustedEnd; i++) {
|
||||||
@ -213,14 +208,10 @@ export async function GET(request: Request) {
|
|||||||
starred: message.flags.has('\\Flagged'),
|
starred: message.flags.has('\\Flagged'),
|
||||||
folder: mailbox.path,
|
folder: mailbox.path,
|
||||||
hasAttachments: message.bodyStructure?.type === 'multipart',
|
hasAttachments: message.bodyStructure?.type === 'multipart',
|
||||||
flags: Array.from(message.flags)
|
flags: Array.from(message.flags),
|
||||||
|
content: message.source?.toString() || '' // Include full email content
|
||||||
};
|
};
|
||||||
|
|
||||||
// Include preview content if available
|
|
||||||
if (preview && message.bodyParts && message.bodyParts.has('TEXT')) {
|
|
||||||
emailData.preview = message.bodyParts.get('TEXT')?.toString() || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.push(emailData);
|
result.push(emailData);
|
||||||
}
|
}
|
||||||
} catch (fetchError) {
|
} catch (fetchError) {
|
||||||
|
|||||||
@ -688,44 +688,20 @@ export default function CourrierPage() {
|
|||||||
return account ? account.color : 'bg-gray-500';
|
return account ? account.color : 'bg-gray-500';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update handleEmailSelect to set selectedEmail correctly and ensure content is loaded
|
// Update handleEmailSelect to use the full content that's already in the emails data
|
||||||
const handleEmailSelect = async (emailId: string) => {
|
const handleEmailSelect = async (emailId: string) => {
|
||||||
try {
|
try {
|
||||||
setContentLoading(true);
|
setContentLoading(true);
|
||||||
|
|
||||||
// Find the email in the current list first
|
// Find the email in the current list
|
||||||
const emailInList = emails.find(email => email.id === emailId);
|
const selectedEmail = emails.find(email => email.id === emailId);
|
||||||
|
|
||||||
// Set selected email immediately with what we have
|
if (!selectedEmail) {
|
||||||
if (emailInList) {
|
throw new Error('Email not found in list');
|
||||||
setSelectedEmail(emailInList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the full email content
|
// Set selected email from our existing data (which now includes full content)
|
||||||
const response = await fetch(`/api/courrier/${emailId}`);
|
setSelectedEmail(selectedEmail);
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to fetch full email content');
|
|
||||||
}
|
|
||||||
|
|
||||||
const fullEmail = await response.json();
|
|
||||||
console.log('Fetched email content:', fullEmail);
|
|
||||||
|
|
||||||
// Create a complete email object by combining what we have
|
|
||||||
const completeEmail = {
|
|
||||||
...(emailInList || {}),
|
|
||||||
...fullEmail,
|
|
||||||
id: emailId,
|
|
||||||
content: fullEmail.content || '',
|
|
||||||
};
|
|
||||||
|
|
||||||
// Update the email in the list
|
|
||||||
setEmails(prevEmails => prevEmails.map(email =>
|
|
||||||
email.id === emailId ? completeEmail : email
|
|
||||||
));
|
|
||||||
|
|
||||||
// Update the selected email
|
|
||||||
setSelectedEmail(completeEmail);
|
|
||||||
|
|
||||||
// Try to mark as read in the background
|
// Try to mark as read in the background
|
||||||
try {
|
try {
|
||||||
@ -745,8 +721,8 @@ export default function CourrierPage() {
|
|||||||
console.error('Error marking email as read:', error);
|
console.error('Error marking email as read:', error);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching email:', error);
|
console.error('Error selecting email:', error);
|
||||||
setError('Failed to load email content. Please try again.');
|
setError('Failed to select email. Please try again.');
|
||||||
} finally {
|
} finally {
|
||||||
setContentLoading(false);
|
setContentLoading(false);
|
||||||
}
|
}
|
||||||
@ -1506,7 +1482,7 @@ export default function CourrierPage() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Keep the loadEmails function for other parts of the code to use
|
// Update loadEmails to store the full content from the API response
|
||||||
const loadEmails = async (isLoadMore = false) => {
|
const loadEmails = async (isLoadMore = false) => {
|
||||||
try {
|
try {
|
||||||
// Skip if already loading
|
// Skip if already loading
|
||||||
@ -1567,7 +1543,7 @@ export default function CourrierPage() {
|
|||||||
fromName: email.fromName || email.from?.split('@')[0] || '',
|
fromName: email.fromName || email.from?.split('@')[0] || '',
|
||||||
to: email.to || '',
|
to: email.to || '',
|
||||||
subject: email.subject || '(No subject)',
|
subject: email.subject || '(No subject)',
|
||||||
content: email.preview || '',
|
content: email.content || '', // Full content is now included from the API
|
||||||
date: email.date || new Date().toISOString(),
|
date: email.date || new Date().toISOString(),
|
||||||
read: email.read || false,
|
read: email.read || false,
|
||||||
starred: email.starred || false,
|
starred: email.starred || false,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user