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.
68 lines
2.4 KiB
68 lines
2.4 KiB
import log4js from "logger";
|
|
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";
|
|
|
|
const logger = log4js.getLogger("COMMENT_EMAIL_TEST");
|
|
|
|
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);
|
|
|
|
const commentEmailConfig = {
|
|
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"),
|
|
};
|
|
|
|
try {
|
|
await sendCommentEmailTestMail({
|
|
toEmail: adminRow?.email ?? "",
|
|
requestedBy: admin.username,
|
|
config: commentEmailConfig,
|
|
});
|
|
return R.success({
|
|
message: "测试邮件发送成功,请检查管理员邮箱",
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof CommentEmailTestValidationError) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: error.message,
|
|
});
|
|
}
|
|
logger.error(
|
|
"[send-test-mail-failed]",
|
|
`adminId=${admin.id}`,
|
|
`adminUsername=${admin.username}`,
|
|
`smtpHost=${commentEmailConfig.smtpHost}`,
|
|
`smtpPort=${commentEmailConfig.smtpPort}`,
|
|
`smtpSecure=${commentEmailConfig.smtpSecure}`,
|
|
error instanceof Error ? error.message : String(error),
|
|
error instanceof Error ? (error.stack ?? "") : "",
|
|
);
|
|
throw createError({
|
|
statusCode: 502,
|
|
statusMessage: "测试邮件发送失败,请检查 SMTP 配置或稍后重试",
|
|
});
|
|
}
|
|
});
|
|
|