Neah/app/api/courrier/session/route.ts

166 lines
5.0 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { getUserEmailCredentials, getMailboxes } from '@/lib/services/email-service';
import { prefetchUserEmailData } from '@/lib/services/prefetch-service';
import { getCachedEmailCredentials, getRedisStatus, warmupRedisCache, getCachedImapSession, cacheImapSession } from '@/lib/redis';
import { prisma } from '@/lib/prisma';
import { ImapFlow } from 'imapflow';
import { getRedisClient } from '@/lib/redis';
import { getImapConnection } from '@/lib/services/email-service';
import { MailCredentials } from '@prisma/client';
import { redis } from '@/lib/redis';
// Keep track of last prefetch time for each user
const lastPrefetchMap = new Map<string, number>();
const PREFETCH_COOLDOWN_MS = 30000; // 30 seconds cooldown between prefetches
// Cache TTL for folders in Redis (5 minutes)
const FOLDERS_CACHE_TTL = 3600; // 1 hour
// Cache to store account folders to avoid repeated calls to the IMAP server
// const accountFoldersCache = new Map<string, string[]>();
// Redis key for folders cache
const FOLDERS_CACHE_KEY = (userId: string, accountId: string) => `email:folders:${userId}:${accountId}`;
/**
* Get folders for a specific account
*/
async function getAccountFolders(accountId: string, account: any): Promise<string[]> {
// Check cache first
const cacheKey = `folders:${accountId}`;
const cachedData = accountFoldersCache.get(cacheKey);
const now = Date.now();
if (cachedData && (now - cachedData.timestamp < FOLDERS_CACHE_TTL)) {
return cachedData.folders;
}
try {
// Connect to IMAP server for this account
const client = new ImapFlow({
host: account.host,
port: account.port,
secure: true,
auth: {
user: account.email,
pass: account.password,
},
logger: false,
tls: {
rejectUnauthorized: false
}
});
await client.connect();
// Get folders for this account
const folders = await getMailboxes(client);
// Close connection
await client.logout();
// Cache the result
accountFoldersCache.set(cacheKey, {
folders,
timestamp: now
});
return folders;
} catch (error) {
console.error(`Error fetching folders for account ${account.email}:`, error);
// Return fallback folders on error
return ['INBOX', 'Sent', 'Drafts', 'Trash'];
}
}
/**
* This endpoint is called when the app initializes to check if the user has email credentials
* and to start prefetching email data in the background if they do
*/
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// Get Redis connection
const redis = getRedisClient();
if (!redis) {
return NextResponse.json({ error: 'Redis connection failed' }, { status: 500 });
}
// Get user with their accounts
const user = await prisma.user.findUnique({
where: { id: session.user.id },
include: { mailCredentials: true }
});
if (!user) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}
// Get all accounts for the user
const accounts: MailCredentials[] = user.mailCredentials || [];
if (accounts.length === 0) {
return NextResponse.json({ error: 'No email accounts found' }, { status: 404 });
}
// Fetch folders for each account
const accountsWithFolders = await Promise.all(
accounts.map(async (account) => {
const cacheKey = FOLDERS_CACHE_KEY(user.id, account.id);
// Try to get folders from Redis cache first
const cachedFolders = await redis.get(cacheKey);
if (cachedFolders) {
return {
...account,
folders: JSON.parse(cachedFolders)
};
}
// If not in cache, fetch from IMAP
const client = await getImapConnection(account);
if (!client) {
return {
...account,
folders: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']
};
}
try {
const folders = await getMailboxes(client);
// Cache the folders in Redis
await redis.set(
cacheKey,
JSON.stringify(folders),
'EX',
FOLDERS_CACHE_TTL
);
return {
...account,
folders
};
} catch (error) {
console.error(`Error fetching folders for account ${account.id}:`, error);
return {
...account,
folders: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']
};
}
})
);
return NextResponse.json({
accounts: accountsWithFolders
});
} catch (error) {
console.error('Error in session route:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}