UI overhault
This commit is contained in:
39
app/api/emails/delete/route.ts
Normal file
39
app/api/emails/delete/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/app/db/drizzle';
|
||||
import { emails } from '@/app/db/schema';
|
||||
import { authenticate, getS3Client } from '@/app/lib/utils';
|
||||
import { inArray } from 'drizzle-orm';
|
||||
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { keys, bucket } = await req.json();
|
||||
|
||||
if (!Array.isArray(keys) || keys.length === 0 || !bucket) {
|
||||
return NextResponse.json({ error: 'Invalid parameters' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const s3 = getS3Client();
|
||||
|
||||
// Delete from S3
|
||||
const deletePromises = keys.map(key =>
|
||||
s3.send(new DeleteObjectCommand({ Bucket: bucket, Key: key }))
|
||||
.catch(err => {
|
||||
console.error(`Failed to delete ${key} from S3:`, err);
|
||||
return null;
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(deletePromises);
|
||||
|
||||
// Delete from database
|
||||
await db.delete(emails).where(inArray(emails.s3Key, keys));
|
||||
|
||||
return NextResponse.json({ success: true, count: keys.length });
|
||||
} catch (error) {
|
||||
console.error('Error deleting emails:', error);
|
||||
return NextResponse.json({ error: 'Failed to delete emails' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
27
app/api/emails/mark-read/route.ts
Normal file
27
app/api/emails/mark-read/route.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/app/db/drizzle';
|
||||
import { emails } from '@/app/db/schema';
|
||||
import { authenticate } from '@/app/lib/utils';
|
||||
import { inArray } from 'drizzle-orm';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { keys } = await req.json();
|
||||
|
||||
if (!Array.isArray(keys) || keys.length === 0) {
|
||||
return NextResponse.json({ error: 'Invalid keys' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
await db
|
||||
.update(emails)
|
||||
.set({ processed: true, processedAt: new Date() })
|
||||
.where(inArray(emails.s3Key, keys));
|
||||
|
||||
return NextResponse.json({ success: true, count: keys.length });
|
||||
} catch (error) {
|
||||
console.error('Error marking emails as read:', error);
|
||||
return NextResponse.json({ error: 'Failed to mark as read' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,11 @@ export async function GET(req: NextRequest) {
|
||||
const emailList = await db.select({
|
||||
key: emails.s3Key,
|
||||
subject: emails.subject,
|
||||
from: emails.from,
|
||||
to: emails.to,
|
||||
date: emails.date,
|
||||
html: emails.html,
|
||||
raw: emails.raw,
|
||||
processed: emails.processed,
|
||||
processedAt: emails.processedAt,
|
||||
processedBy: emails.processedBy,
|
||||
@@ -26,14 +30,85 @@ export async function GET(req: NextRequest) {
|
||||
status: emails.status,
|
||||
}).from(emails).where(sql`${mailbox} = ANY(${emails.to}) AND ${emails.domainId} = ${domain.id}`);
|
||||
|
||||
return NextResponse.json(emailList.map(e => ({
|
||||
key: e.key,
|
||||
subject: e.subject,
|
||||
date: e.date?.toISOString(),
|
||||
processed: e.processed ? 'true' : 'false',
|
||||
processedAt: e.processedAt?.toISOString() || null,
|
||||
processedBy: e.processedBy,
|
||||
queuedTo: e.queuedTo,
|
||||
status: e.status,
|
||||
})));
|
||||
return NextResponse.json(emailList.map(e => {
|
||||
let preview = '';
|
||||
|
||||
// Check both HTML and raw content for images
|
||||
const htmlContent = e.html || '';
|
||||
const rawContent = e.raw || '';
|
||||
|
||||
// Check for images in HTML
|
||||
const hasImgTags = /<img[^>]+>/i.test(htmlContent);
|
||||
const imageCount = (htmlContent.match(/<img[^>]+>/gi) || []).length;
|
||||
|
||||
// Check for base64 encoded images
|
||||
const hasBase64Images = /data:image\//i.test(htmlContent);
|
||||
|
||||
// Check for image attachments in raw content
|
||||
const hasImageAttachments = /Content-Type:\s*image\//i.test(rawContent);
|
||||
const attachmentCount = (rawContent.match(/Content-Type:\s*image\//gi) || []).length;
|
||||
|
||||
// Check for multipart/related (usually contains embedded images)
|
||||
const isMultipartRelated = /Content-Type:\s*multipart\/related/i.test(rawContent);
|
||||
|
||||
// Extract text content
|
||||
let textContent = '';
|
||||
if (htmlContent) {
|
||||
textContent = htmlContent
|
||||
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '') // Remove style blocks
|
||||
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '') // Remove script blocks
|
||||
.replace(/<[^>]*>/g, '') // Remove HTML tags
|
||||
.replace(/@font-face\s*\{[^}]*\}/gi, '') // Remove @font-face definitions
|
||||
.replace(/@media[^{]*\{[\s\S]*?\}/gi, '') // Remove @media queries
|
||||
.replace(/@import[^;]*;/gi, '') // Remove @import statements
|
||||
.replace(/font-family:\s*[^;]+;?/gi, '') // Remove font-family CSS
|
||||
.replace(/\s+/g, ' ') // Normalize whitespace
|
||||
.trim();
|
||||
}
|
||||
|
||||
// Determine preview
|
||||
const totalImages = imageCount + attachmentCount + (hasBase64Images ? 1 : 0);
|
||||
const hasImages = hasImgTags || hasBase64Images || hasImageAttachments || isMultipartRelated;
|
||||
|
||||
if (hasImages && textContent.length < 50) {
|
||||
// Mostly images, little text
|
||||
if (totalImages > 1) {
|
||||
preview = `📷 ${totalImages} Bilder`;
|
||||
} else if (totalImages === 1) {
|
||||
preview = '📷 Bild';
|
||||
} else {
|
||||
preview = '📷 Bild';
|
||||
}
|
||||
} else if (textContent.length > 0) {
|
||||
// Has text content
|
||||
preview = textContent.substring(0, 150);
|
||||
if (textContent.length > 150) preview += '...';
|
||||
} else if (hasImages) {
|
||||
// Has images but couldn't count them properly
|
||||
preview = '📷 Bild(er)';
|
||||
} else if (!htmlContent && rawContent) {
|
||||
// No HTML, check if it's plain text
|
||||
const plainText = rawContent
|
||||
.split('\n\n')
|
||||
.find(line => !line.startsWith('Content-') && !line.startsWith('MIME-') && line.length > 10);
|
||||
if (plainText) {
|
||||
preview = plainText.substring(0, 150);
|
||||
if (plainText.length > 150) preview += '...';
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
key: e.key,
|
||||
subject: e.subject,
|
||||
from: e.from,
|
||||
to: e.to,
|
||||
preview,
|
||||
date: e.date?.toISOString(),
|
||||
processed: e.processed ? 'true' : 'false',
|
||||
processedAt: e.processedAt?.toISOString() || null,
|
||||
processedBy: e.processedBy,
|
||||
queuedTo: e.queuedTo,
|
||||
status: e.status,
|
||||
};
|
||||
}));
|
||||
}
|
||||
22
app/api/sync/route.ts
Normal file
22
app/api/sync/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { authenticate } from '@/app/lib/utils';
|
||||
import { syncAllDomains } from '@/app/lib/sync';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
try {
|
||||
// Start sync in background (don't wait for completion)
|
||||
syncAllDomains().catch(err => {
|
||||
console.error('Sync failed:', err);
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Sync started in background'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error starting sync:', error);
|
||||
return NextResponse.json({ error: 'Failed to start sync' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user