mirror of
https://github.com/beilunyang/moemail.git
synced 2026-07-08 22:21:24 +08:00
feat: Enhance email domain configuration and management
This commit is contained in:
@@ -4,18 +4,33 @@ import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||
export const runtime = "edge"
|
||||
|
||||
export async function GET() {
|
||||
const config = await getRequestContext().env.SITE_CONFIG.get("DEFAULT_ROLE")
|
||||
const env = getRequestContext().env
|
||||
const [defaultRole, emailDomains] = await Promise.all([
|
||||
env.SITE_CONFIG.get("DEFAULT_ROLE"),
|
||||
env.SITE_CONFIG.get("EMAIL_DOMAINS")
|
||||
])
|
||||
|
||||
return Response.json({ defaultRole: config || ROLES.CIVILIAN })
|
||||
return Response.json({
|
||||
defaultRole: defaultRole || ROLES.CIVILIAN,
|
||||
emailDomains: emailDomains || ""
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { defaultRole } = await request.json() as { defaultRole: Exclude<Role, typeof ROLES.EMPEROR> }
|
||||
const { defaultRole, emailDomains } = await request.json() as {
|
||||
defaultRole: Exclude<Role, typeof ROLES.EMPEROR>,
|
||||
emailDomains: string
|
||||
}
|
||||
|
||||
if (![ROLES.KNIGHT, ROLES.CIVILIAN].includes(defaultRole)) {
|
||||
return Response.json({ error: "无效的角色" }, { status: 400 })
|
||||
}
|
||||
|
||||
await getRequestContext().env.SITE_CONFIG.put("DEFAULT_ROLE", defaultRole)
|
||||
const env = getRequestContext().env
|
||||
await Promise.all([
|
||||
env.SITE_CONFIG.put("DEFAULT_ROLE", defaultRole),
|
||||
env.SITE_CONFIG.put("EMAIL_DOMAINS", emailDomains)
|
||||
])
|
||||
|
||||
return Response.json({ success: true })
|
||||
}
|
||||
@@ -1,20 +1,13 @@
|
||||
import { EMAIL_CONFIG } from "@/config"
|
||||
import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const domains = EMAIL_CONFIG.DOMAINS
|
||||
const domainString = await getRequestContext().env.SITE_CONFIG.get("EMAIL_DOMAINS")
|
||||
|
||||
if (domains.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的域名列表" },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({ domains })
|
||||
return NextResponse.json({ domains: domainString ? domainString.split(',') : ["moemail.app"] })
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch domains:', error)
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -6,6 +6,7 @@ import { emails } from "@/lib/schema"
|
||||
import { eq, and, gt, sql } from "drizzle-orm"
|
||||
import { EXPIRY_OPTIONS } from "@/types/email"
|
||||
import { EMAIL_CONFIG } from "@/config"
|
||||
import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
@@ -14,7 +15,6 @@ export async function POST(request: Request) {
|
||||
const session = await auth()
|
||||
|
||||
try {
|
||||
// Check current number of active emails for user
|
||||
const activeEmailsCount = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(emails)
|
||||
@@ -45,7 +45,10 @@ export async function POST(request: Request) {
|
||||
)
|
||||
}
|
||||
|
||||
if (!EMAIL_CONFIG.DOMAINS.includes(domain)) {
|
||||
const domainString = await getRequestContext().env.SITE_CONFIG.get("EMAIL_DOMAINS")
|
||||
const domains = domainString ? domainString.split(',') : ["moemail.app"]
|
||||
|
||||
if (!domains || !domains.includes(domain)) {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的域名" },
|
||||
{ status: 400 }
|
||||
|
||||
@@ -17,10 +17,6 @@ interface CreateDialogProps {
|
||||
onEmailCreated: () => void
|
||||
}
|
||||
|
||||
interface DomainResponse {
|
||||
domains: string[]
|
||||
}
|
||||
|
||||
export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -89,7 +85,7 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
||||
|
||||
const fetchDomains = async () => {
|
||||
const response = await fetch("/api/emails/domains");
|
||||
const data = (await response.json()) as DomainResponse;
|
||||
const data = (await response.json()) as { domains: string[] };
|
||||
setDomains(data.domains || []);
|
||||
setCurrentDomain(data.domains[0] || "");
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Settings } from "lucide-react"
|
||||
import { useToast } from "@/components/ui/use-toast"
|
||||
import { useState, useEffect } from "react"
|
||||
import { Role, ROLES } from "@/lib/permissions"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
|
||||
export function ConfigPanel() {
|
||||
const [defaultRole, setDefaultRole] = useState<string>("")
|
||||
const [emailDomains, setEmailDomains] = useState<string>("")
|
||||
const [loading, setLoading] = useState(false)
|
||||
const { toast } = useToast()
|
||||
|
||||
@@ -25,8 +27,12 @@ export function ConfigPanel() {
|
||||
const fetchConfig = async () => {
|
||||
const res = await fetch("/api/config")
|
||||
if (res.ok) {
|
||||
const data = await res.json() as { defaultRole: Exclude<Role, typeof ROLES.EMPEROR> }
|
||||
const data = await res.json() as {
|
||||
defaultRole: Exclude<Role, typeof ROLES.EMPEROR>,
|
||||
emailDomains: string
|
||||
}
|
||||
setDefaultRole(data.defaultRole)
|
||||
setEmailDomains(data.emailDomains)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,14 +42,14 @@ export function ConfigPanel() {
|
||||
const res = await fetch("/api/config", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ defaultRole }),
|
||||
body: JSON.stringify({ defaultRole, emailDomains }),
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("保存失败")
|
||||
|
||||
toast({
|
||||
title: "保存成功",
|
||||
description: "默认角色设置已更新",
|
||||
description: "网站设置已更新",
|
||||
})
|
||||
} catch (error) {
|
||||
toast({
|
||||
@@ -75,13 +81,26 @@ export function ConfigPanel() {
|
||||
<SelectItem value={ROLES.CIVILIAN}>平民</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={loading}
|
||||
>
|
||||
保存
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-4">
|
||||
<span className="text-sm">邮箱域名:</span>
|
||||
<div className="flex-1">
|
||||
<Input
|
||||
value={emailDomains}
|
||||
onChange={(e) => setEmailDomains(e.target.value)}
|
||||
placeholder="多个域名用逗号分隔,如: moemail.app,bitibiti.com"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={loading}
|
||||
className="w-full"
|
||||
>
|
||||
保存
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
const DOMAINS = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EMAIL_DOMAIN ? process.env.NEXT_PUBLIC_EMAIL_DOMAIN : 'moemail.app'
|
||||
|
||||
export const EMAIL_CONFIG = {
|
||||
MAX_ACTIVE_EMAILS: 30, // Maximum number of active emails
|
||||
POLL_INTERVAL: 10_000, // Polling interval in milliseconds
|
||||
DOMAINS: DOMAINS.split(','), // Email domains array
|
||||
} as const
|
||||
|
||||
export type EmailConfig = typeof EMAIL_CONFIG
|
||||
Reference in New Issue
Block a user