You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
1.9 KiB

import { dbGlobal } from "drizzle-pkg/lib/db";
import { users } from "drizzle-pkg/lib/schema/auth";
import { eq } from "drizzle-orm";
import { requireAdmin } from "#server/utils/admin-guard";
import { assertUnderRateLimit } from "#server/utils/simple-rate-limit";
import { getRequestIP } from "h3";
import {
CommentEmailTestValidationError,
sendCommentEmailTestMail,
} from "#server/service/comment-email/test-mail";
export default defineWrappedResponseHandler(async (event) => {
const ip = getRequestIP(event, { xForwardedFor: true }) ?? "unknown";
assertUnderRateLimit(`admin-config-comment-email-test:${ip}`, 5, 60_000);
const admin = await requireAdmin(event);
const [adminRow] = await dbGlobal
.select({
email: users.email,
})
.from(users)
.where(eq(users.id, admin.id))
.limit(1);
try {
await sendCommentEmailTestMail({
toEmail: adminRow?.email ?? "",
requestedBy: admin.username,
config: {
enabled: await event.context.config.getGlobal("commentEmailNotifyEnabled"),
fromEmail: await event.context.config.getGlobal("commentMailFromEmail"),
smtpHost: await event.context.config.getGlobal("commentSmtpHost"),
smtpPort: await event.context.config.getGlobal("commentSmtpPort"),
smtpSecure: await event.context.config.getGlobal("commentSmtpSecure"),
smtpUser: await event.context.config.getGlobal("commentSmtpUser"),
smtpPass: await event.context.config.getGlobal("commentSmtpPass"),
},
});
return R.success({
message: "测试邮件发送成功,请检查管理员邮箱",
});
} catch (error) {
if (error instanceof CommentEmailTestValidationError) {
throw createError({
statusCode: 400,
statusMessage: error.message,
});
}
throw createError({
statusCode: 502,
statusMessage: "测试邮件发送失败,请检查 SMTP 配置或稍后重试",
});
}
});