panel 2 courier api
This commit is contained in:
parent
e255ed4c5a
commit
df60de0c8e
@ -4,6 +4,25 @@ 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';
|
||||||
|
|
||||||
|
// Add caching for better performance
|
||||||
|
const cache = new Map();
|
||||||
|
|
||||||
|
const getCachedEmail = (id: string) => cache.get(id);
|
||||||
|
const setCachedEmail = (id: string, email: Email) => cache.set(id, email);
|
||||||
|
|
||||||
|
// Add debouncing for folder changes
|
||||||
|
const debouncedLoadEmails = debounce((folder: string) => {
|
||||||
|
loadEmails(folder);
|
||||||
|
}, 300);
|
||||||
|
|
||||||
|
// Add infinite scroll
|
||||||
|
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
|
||||||
|
const { scrollTop, scrollHeight, clientHeight } = e.currentTarget;
|
||||||
|
if (scrollHeight - scrollTop === clientHeight) {
|
||||||
|
// Load more emails
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
@ -33,6 +52,7 @@ 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';
|
||||||
|
|
||||||
// Calculate start and end sequence numbers
|
// Calculate start and end sequence numbers
|
||||||
const start = (page - 1) * limit + 1;
|
const start = (page - 1) * limit + 1;
|
||||||
@ -72,11 +92,20 @@ export async function GET(request: Request) {
|
|||||||
const adjustedStart = Math.min(start, mailbox.exists);
|
const adjustedStart = Math.min(start, mailbox.exists);
|
||||||
const adjustedEnd = Math.min(end, mailbox.exists);
|
const adjustedEnd = Math.min(end, mailbox.exists);
|
||||||
|
|
||||||
// Fetch messages from the current folder
|
// Fetch both metadata and preview content
|
||||||
const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, {
|
const messages = await client.fetch(`${adjustedStart}:${adjustedEnd}`, {
|
||||||
envelope: true,
|
envelope: true,
|
||||||
flags: true,
|
flags: true,
|
||||||
bodyStructure: true
|
bodyStructure: true,
|
||||||
|
// Only fetch preview content if requested
|
||||||
|
...(preview ? {
|
||||||
|
bodyParts: ['TEXT'],
|
||||||
|
bodyPartsOptions: {
|
||||||
|
TEXT: {
|
||||||
|
maxLength: 1000 // Limit preview length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} : {})
|
||||||
});
|
});
|
||||||
|
|
||||||
for await (const message of messages) {
|
for await (const message of messages) {
|
||||||
@ -91,7 +120,9 @@ 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),
|
||||||
|
// Include preview content if available
|
||||||
|
preview: preview ? message.bodyParts?.TEXT?.toString() : null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user