refactor(email): 简化邮件通知设置逻辑

移除adminEnabled和userEnabled配置,仅保留globalEnabled控制邮件通知
更新相关API、数据库操作及邮件发送逻辑以适配简化后的配置
This commit is contained in:
anghunk
2026-01-20 11:47:43 +08:00
parent ee31e98dee
commit 398daa9a11
5 changed files with 46 additions and 102 deletions

View File

@@ -42,8 +42,6 @@ export type CommentSettingsResponse = {
export type EmailNotifySettingsResponse = { export type EmailNotifySettingsResponse = {
globalEnabled: boolean; globalEnabled: boolean;
adminEnabled: boolean;
userEnabled: boolean;
}; };
export async function loginAdmin(name: string, password: string): Promise<string> { export async function loginAdmin(name: string, password: string): Promise<string> {
@@ -83,8 +81,6 @@ export function fetchEmailNotifySettings(): Promise<EmailNotifySettingsResponse>
export function saveEmailNotifySettings(data: { export function saveEmailNotifySettings(data: {
globalEnabled?: boolean; globalEnabled?: boolean;
adminEnabled?: boolean;
userEnabled?: boolean;
}): Promise<{ message: string }> { }): Promise<{ message: string }> {
return put<{ message: string }>('/admin/settings/email-notify', data); return put<{ message: string }>('/admin/settings/email-notify', data);
} }

View File

@@ -8,7 +8,7 @@
href="https://cwd-comments-docs.zishu.me" href="https://cwd-comments-docs.zishu.me"
target="_blank" target="_blank"
> >
文档 使用文档
</a> </a>
<a <a
class="layout-button" class="layout-button"

View File

@@ -114,9 +114,7 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
}); });
let notifySettings: EmailNotificationSettings = { let notifySettings: EmailNotificationSettings = {
globalEnabled: true, globalEnabled: true
adminEnabled: true,
userEnabled: true
}; };
try { try {
notifySettings = await loadEmailNotificationSettings(c.env); notifySettings = await loadEmailNotificationSettings(c.env);
@@ -157,29 +155,25 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
} }
if (canSendUserMail && isValidEmail(parentComment.email)) { if (canSendUserMail && isValidEmail(parentComment.email)) {
if (!notifySettings.userEnabled) { console.log('PostComment:mailDispatch:userReply:send', {
console.log('PostComment:mailDispatch:userReply:disabledByConfig'); toEmail: parentComment.email,
} else { toName: parentComment.author
console.log('PostComment:mailDispatch:userReply:send', { });
toEmail: parentComment.email, await sendCommentReplyNotification(c.env, {
toName: parentComment.author toEmail: parentComment.email,
}); toName: parentComment.author,
await sendCommentReplyNotification(c.env, { postTitle: data.post_title,
toEmail: parentComment.email, parentComment: parentComment.content_text,
toName: parentComment.author, replyAuthor: author,
postTitle: data.post_title, replyContent: content,
parentComment: parentComment.content_text, postUrl: data.post_url,
replyAuthor: author, });
replyContent: content, await c.env.CWD_DB.prepare(
postUrl: data.post_url, "INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)"
}); ).bind(parentComment.email, 'user-reply', ip, new Date().toISOString()).run();
await c.env.CWD_DB.prepare( console.log('PostComment:mailDispatch:userReply:logInserted', {
"INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)" toEmail: parentComment.email
).bind(parentComment.email, 'user-reply', ip, new Date().toISOString()).run(); });
console.log('PostComment:mailDispatch:userReply:logInserted', {
toEmail: parentComment.email
});
}
} }
} }
} else { } else {
@@ -188,21 +182,17 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
).first<{ created_at: string }>(); ).first<{ created_at: string }>();
const canSendAdminMail = !adminEmailRow || (Date.now() - new Date(adminEmailRow.created_at).getTime() > 15 * 1000); const canSendAdminMail = !adminEmailRow || (Date.now() - new Date(adminEmailRow.created_at).getTime() > 15 * 1000);
if (canSendAdminMail) { if (canSendAdminMail) {
if (!notifySettings.adminEnabled) { console.log('PostComment:mailDispatch:admin:send');
console.log('PostComment:mailDispatch:admin:disabledByConfig'); await sendCommentNotification(c.env, {
} else { postTitle: data.post_title,
console.log('PostComment:mailDispatch:admin:send'); postUrl: data.post_url,
await sendCommentNotification(c.env, { commentAuthor: author,
postTitle: data.post_title, commentContent: content
postUrl: data.post_url, });
commentAuthor: author, await c.env.CWD_DB.prepare(
commentContent: content "INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)"
}); ).bind('admin', 'admin-notify', ip, new Date().toISOString()).run();
await c.env.CWD_DB.prepare( console.log('PostComment:mailDispatch:admin:logInserted');
"INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)"
).bind('admin', 'admin-notify', ip, new Date().toISOString()).run();
console.log('PostComment:mailDispatch:admin:logInserted');
}
} }
if (!canSendAdminMail) { if (!canSendAdminMail) {
console.log('PostComment:mailDispatch:admin:skippedByRateLimit'); console.log('PostComment:mailDispatch:admin:skippedByRateLimit');

View File

@@ -164,15 +164,9 @@ app.put('/admin/settings/email-notify', async (c) => {
const body = await c.req.json(); const body = await c.req.json();
const globalEnabled = const globalEnabled =
typeof body.globalEnabled === 'boolean' ? body.globalEnabled : undefined; typeof body.globalEnabled === 'boolean' ? body.globalEnabled : undefined;
const adminEnabled =
typeof body.adminEnabled === 'boolean' ? body.adminEnabled : undefined;
const userEnabled =
typeof body.userEnabled === 'boolean' ? body.userEnabled : undefined;
await saveEmailNotificationSettings(c.env, { await saveEmailNotificationSettings(c.env, {
globalEnabled, globalEnabled
adminEnabled,
userEnabled
}); });
return c.json({ message: '保存成功' }); return c.json({ message: '保存成功' });

View File

@@ -5,8 +5,6 @@ export function isValidEmail(email: string) {
} }
const EMAIL_NOTIFY_GLOBAL_KEY = 'email_notify_enabled'; const EMAIL_NOTIFY_GLOBAL_KEY = 'email_notify_enabled';
const EMAIL_NOTIFY_ADMIN_KEY = 'email_notify_admin_enabled';
const EMAIL_NOTIFY_USER_KEY = 'email_notify_user_enabled';
type MailGatewayPayload = { type MailGatewayPayload = {
to: string[]; to: string[];
@@ -44,8 +42,6 @@ async function dispatchMail(env: Bindings, payload: MailGatewayPayload) {
export type EmailNotificationSettings = { export type EmailNotificationSettings = {
globalEnabled: boolean; globalEnabled: boolean;
adminEnabled: boolean;
userEnabled: boolean;
}; };
function parseEnabled(raw: string | undefined, defaultValue: boolean) { function parseEnabled(raw: string | undefined, defaultValue: boolean) {
@@ -60,11 +56,10 @@ export async function loadEmailNotificationSettings(
'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)' 'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)'
).run(); ).run();
const keys = [EMAIL_NOTIFY_GLOBAL_KEY, EMAIL_NOTIFY_ADMIN_KEY, EMAIL_NOTIFY_USER_KEY];
const { results } = await env.CWD_DB.prepare( const { results } = await env.CWD_DB.prepare(
'SELECT key, value FROM Settings WHERE key IN (?, ?, ?)' 'SELECT key, value FROM Settings WHERE key = ?'
) )
.bind(...keys) .bind(EMAIL_NOTIFY_GLOBAL_KEY)
.all<{ key: string; value: string }>(); .all<{ key: string; value: string }>();
const map = new Map<string, string>(); const map = new Map<string, string>();
@@ -75,13 +70,9 @@ export async function loadEmailNotificationSettings(
} }
const globalEnabled = parseEnabled(map.get(EMAIL_NOTIFY_GLOBAL_KEY), true); const globalEnabled = parseEnabled(map.get(EMAIL_NOTIFY_GLOBAL_KEY), true);
const adminEnabled = globalEnabled;
const userEnabled = globalEnabled;
return { return {
globalEnabled, globalEnabled
adminEnabled,
userEnabled
}; };
} }
@@ -89,50 +80,23 @@ export async function saveEmailNotificationSettings(
env: Bindings, env: Bindings,
settings: { settings: {
globalEnabled?: boolean; globalEnabled?: boolean;
adminEnabled?: boolean;
userEnabled?: boolean;
} }
) { ) {
await env.CWD_DB.prepare( await env.CWD_DB.prepare(
'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)' 'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)'
).run(); ).run();
const entries: { key: string; value: string | undefined }[] = [ const value =
{ typeof settings.globalEnabled === 'boolean'
key: EMAIL_NOTIFY_GLOBAL_KEY, ? settings.globalEnabled
value: ? '1'
typeof settings.globalEnabled === 'boolean' : '0'
? settings.globalEnabled : undefined;
? '1'
: '0'
: undefined
},
{
key: EMAIL_NOTIFY_ADMIN_KEY,
value:
typeof settings.adminEnabled === 'boolean'
? settings.adminEnabled
? '1'
: '0'
: undefined
},
{
key: EMAIL_NOTIFY_USER_KEY,
value:
typeof settings.userEnabled === 'boolean'
? settings.userEnabled
? '1'
: '0'
: undefined
}
];
for (const entry of entries) { if (value !== undefined) {
if (entry.value !== undefined) { await env.CWD_DB.prepare('REPLACE INTO Settings (key, value) VALUES (?, ?)')
await env.CWD_DB.prepare('REPLACE INTO Settings (key, value) VALUES (?, ?)') .bind(EMAIL_NOTIFY_GLOBAL_KEY, value)
.bind(entry.key, entry.value) .run();
.run();
}
} }
} }