Files
moemail/app/api/emails/[id]/route.ts
2024-12-24 23:38:51 +08:00

118 lines
3.0 KiB
TypeScript

import { NextResponse } from "next/server"
import { auth } from "@/lib/auth"
import { createDb } from "@/lib/db"
import { emails, messages } from "@/lib/schema"
import { eq, and, lt, or, sql } from "drizzle-orm"
import { encodeCursor, decodeCursor } from "@/lib/cursor"
export const runtime = "edge"
export async function DELETE(
_request: Request,
{ params }: { params: { id: string } }
) {
const db = createDb()
const session = await auth()
try {
const email = await db.query.emails.findFirst({
where: and(
eq(emails.id, params.id),
eq(emails.userId, session!.user!.id!)
)
})
if (!email) {
return NextResponse.json(
{ error: "邮箱不存在或无权限删除" },
{ status: 403 }
)
}
await db.delete(messages)
.where(eq(messages.emailId, params.id))
await db.delete(emails)
.where(eq(emails.id, params.id))
return NextResponse.json({ success: true })
} catch (error) {
console.error('Failed to delete email:', error)
return NextResponse.json(
{ error: "删除邮箱失败" },
{ status: 500 }
)
}
}
const PAGE_SIZE = 20
export async function GET(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { searchParams } = new URL(request.url)
const cursorStr = searchParams.get('cursor')
try {
const db = createDb()
const { id } = await params
const baseConditions = eq(messages.emailId, id)
const totalResult = await db.select({ count: sql<number>`count(*)` })
.from(messages)
.where(baseConditions)
const totalCount = Number(totalResult[0].count)
const conditions = [baseConditions]
if (cursorStr) {
const { timestamp, id } = decodeCursor(cursorStr)
conditions.push(
// @ts-expect-error "ignore the error"
or(
lt(messages.receivedAt, new Date(timestamp)),
and(
eq(messages.receivedAt, new Date(timestamp)),
lt(messages.id, id)
)
)
)
}
const results = await db.query.messages.findMany({
where: and(...conditions),
orderBy: (messages, { desc }) => [
desc(messages.receivedAt),
desc(messages.id)
],
limit: PAGE_SIZE + 1
})
const hasMore = results.length > PAGE_SIZE
const nextCursor = hasMore
? encodeCursor(
results[PAGE_SIZE - 1].receivedAt.getTime(),
results[PAGE_SIZE - 1].id
)
: null
const messageList = hasMore ? results.slice(0, PAGE_SIZE) : results
return NextResponse.json({
messages: messageList.map(msg => ({
id: msg.id,
from_address: msg.fromAddress,
subject: msg.subject,
received_at: msg.receivedAt.getTime()
})),
nextCursor,
total: totalCount
})
} catch (error) {
console.error('Failed to fetch messages:', error)
return NextResponse.json(
{ error: "Failed to fetch messages" },
{ status: 500 }
)
}
}