Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions web/src/api/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ export interface SmtpTestResult {
message: string;
}

/** Resend 发信渠道配置(管理员二选一):token 脱敏返回(永不明文下发)。 */
export interface ResendConfig {
/** 实际发信渠道:SMTP(默认,与现有行为零变化)或 Resend。 */
channel: "smtp" | "resend";
/** 发件身份(与 smtp_config 解耦,Resend 渠道独立)。 */
from: string;
from_name: string;
/** 脱敏:{域名: ""};域名对应 Resend 已验证的发信域。 */
resend_tokens: Record<string, string>;
/** {域名: 是否已配 token}。 */
tokens_set: Record<string, boolean>;
}

/** PUT /api/admin/settings/resend 载荷:resend_tokens 某域名留空表示不改;缺失键表示删除该域名。 */
export interface ResendPatch {
channel: "smtp" | "resend";
/** 发件身份(可选,缺省不改)。 */
from?: string;
from_name?: string;
resend_tokens?: Record<string, string>;
}

/** db-upgrade 摘要里的一条:键名按后端返回原样(契约只写「...摘要」,不假设键名)。 */
export interface DbUpgradeSummaryItem {
label: string;
Expand Down Expand Up @@ -98,6 +120,10 @@ export interface AdminApi {
getSmtp(): Promise<SmtpConfig>;
updateSmtp(patch: SmtpPatch): Promise<void>;
testSmtp(email: string): Promise<SmtpTestResult>;
/** Resend 发信渠道:读取渠道 + {域名: token}(脱敏)。 */
getResend(): Promise<ResendConfig>;
updateResend(patch: ResendPatch): Promise<void>;
testResend(email: string): Promise<SmtpTestResult>;
listUsers(): Promise<AdminUser[]>;
approveUser(id: string): Promise<void>;
/** 更新角色 / 禁用启用(PATCH,后端 PUT 兼容)。 */
Expand Down Expand Up @@ -158,6 +184,37 @@ export const httpAdminApi: AdminApi = {
}
return { success: true, message: "测试邮件已发送,请查收收件箱" };
},
async getResend() {
const res = await fetch("/api/admin/settings/resend");
if (!res.ok) throw await parseError(res);
return (await res.json()) as ResendConfig;
},
async updateResend(patch) {
const res = await fetch("/api/admin/settings/resend", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patch),
});
if (!res.ok) throw await parseError(res);
},
async testResend(email) {
const res = await fetch("/api/admin/settings/resend/test", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
const body = (await res.json().catch(() => null)) as {
success?: boolean;
error?: string;
} | null;
if (!res.ok) {
throw new Error(body?.error || `${res.status}`);
}
if (body?.success === false) {
return { success: false, message: body.error ?? "Resend error" };
}
return { success: true, message: "测试邮件已发送,请查收收件箱" };
},
async listUsers() {
const res = await fetch("/api/admin/users");
if (!res.ok) throw await parseError(res);
Expand Down
6 changes: 6 additions & 0 deletions web/src/api/letters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface LetterRecipient {
contact_id: string | null;
name: string;
email: string;
/** 投递状态(后端 serializeLetter 透出;保存载荷无需提供) */
status?: "pending" | "sent" | "failed";
/** 投递失败原因(Resend webhook 回写 / SMTP 失败);保存载荷无需提供 */
error?: string | null;
/** 实际寄出时刻(UTC ISO);保存载荷无需提供 */
sent_at?: string | null;
}

/** 保存载荷(POST/PUT /api/letters)。 */
Expand Down
50 changes: 50 additions & 0 deletions web/src/api/mock/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
AdminApi,
AdminUser,
DbUpgradeResult,
ResendPatch,
SetupSwitchState,
SmtpPatch,
SmtpTestResult,
Expand Down Expand Up @@ -58,6 +59,55 @@ export const mockAdminApi: AdminApi = {
message: `测试邮件已通过 ${state.smtp.host}:${state.smtp.port} 发出,请查收 ${email.trim()}`,
};
},
async getResend() {
// 脱敏:永不下发 token 明文(resend_tokens 恒为空串,tokens_set 标记是否已配)
return {
channel: state.resend.channel,
from: state.resend.from,
from_name: state.resend.from_name,
resend_tokens: Object.fromEntries(
Object.keys(state.resend.resend_tokens).map((d) => [d, ""]),
),
tokens_set: { ...state.resend.tokens_set },
};
},
async updateResend(patch: ResendPatch) {
state.resend.channel = patch.channel;
if (patch.from !== undefined) state.resend.from = patch.from;
if (patch.from_name !== undefined) state.resend.from_name = patch.from_name;
if (patch.resend_tokens) {
// 以本次提交的域名键整体重建:缺失键 = 删除;留空 = 保留存量;非空 = 覆盖。
const nextTokens: Record<string, string> = {};
for (const [domain, token] of Object.entries(patch.resend_tokens)) {
const d = domain.trim();
if (!d) continue;
const trimmed = (token ?? "").trim();
if (trimmed === "") {
// 留空 = 不改(对齐 SMTP 密码口径):保留存量 token(若存在)
const keep = state.resendTokens[d];
if (keep) nextTokens[d] = keep;
} else {
nextTokens[d] = trimmed;
}
}
state.resendTokens = nextTokens;
state.resend.resend_tokens = Object.fromEntries(
Object.keys(nextTokens).map((d) => [d, ""]),
);
state.resend.tokens_set = Object.fromEntries(
Object.keys(nextTokens).map((d) => [d, true]),
);
}
},
async testResend(email): Promise<SmtpTestResult> {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim())) {
throw new Error("收件邮箱格式不正确");
}
if (state.resend.channel !== "resend" || Object.keys(state.resend.tokens_set).length === 0) {
return { success: false, message: "Resend 未配置:请先在配置页填写 API key 并切换到 Resend" };
}
return { success: true, message: `测试邮件已通过 Resend 发出,请查收 ${email.trim()}` };
},
async listUsers(): Promise<AdminUser[]> {
return state.accounts.map(toAdminUser);
},
Expand Down
18 changes: 17 additions & 1 deletion web/src/api/mock/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { Contact } from "../contacts";
import type { Letter } from "../letters";
import type { MeProfile } from "../settings";
import type { AdminUser, SmtpConfig } from "../admin";
import type { AdminUser, ResendConfig, SmtpConfig } from "../admin";

export interface MockAccount {
id: string;
Expand Down Expand Up @@ -33,6 +33,10 @@ export interface MockState {
pendingLoginEmail: string | null;
twofa: MockTwoFa;
smtp: SmtpConfig;
/** Resend 发信渠道(默认 SMTP,与后端缺省一致)。 */
resend: ResendConfig;
/** mock 内部保存的原始 token(脱敏视图 resend 由它派生,真实后端永不下发原文)。 */
resendTokens: Record<string, string>;
/** setup(GET /api/setup/state)状态:true = 尚未初始化,无任何账号。 */
setupEnabled: boolean;
/** setup 页面开关(/api/admin/setup-switch):false 时 GET /api/setup/state 返回 404。默认 ON。 */
Expand All @@ -55,6 +59,14 @@ const DEFAULT_SMTP: SmtpConfig = {
from_name: "时光邮局",
};

const DEFAULT_RESEND: ResendConfig = {
channel: "smtp",
from: "",
from_name: "",
resend_tokens: {},
tokens_set: {},
};

function buildAccounts(): MockAccount[] {
return [
{
Expand Down Expand Up @@ -107,6 +119,8 @@ export const state: MockState = {
pendingLoginEmail: null,
twofa: { totp_enabled: false, passkey_enabled: false, pendingTotpSecret: null },
smtp: { ...DEFAULT_SMTP },
resend: { ...DEFAULT_RESEND },
resendTokens: {},
// 默认 mock 场景是「已上线、已有账号」;全新部署用 seedFreshDeployment() 重置。
setupEnabled: false,
setupSwitchEnabled: true,
Expand Down Expand Up @@ -134,6 +148,8 @@ export function resetMockStore(): void {
state.pendingLoginEmail = null;
state.twofa = { totp_enabled: false, passkey_enabled: false, pendingTotpSecret: null };
state.smtp = { ...DEFAULT_SMTP };
state.resend = { ...DEFAULT_RESEND };
state.resendTokens = {};
state.setupEnabled = false;
state.setupSwitchEnabled = true;
state.registrationEnabled = true;
Expand Down
Loading
Loading