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

View File

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

View File

@@ -114,9 +114,7 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
});
let notifySettings: EmailNotificationSettings = {
globalEnabled: true,
adminEnabled: true,
userEnabled: true
globalEnabled: true
};
try {
notifySettings = await loadEmailNotificationSettings(c.env);
@@ -157,29 +155,25 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
}
if (canSendUserMail && isValidEmail(parentComment.email)) {
if (!notifySettings.userEnabled) {
console.log('PostComment:mailDispatch:userReply:disabledByConfig');
} else {
console.log('PostComment:mailDispatch:userReply:send', {
toEmail: parentComment.email,
toName: parentComment.author
});
await sendCommentReplyNotification(c.env, {
toEmail: parentComment.email,
toName: parentComment.author,
postTitle: data.post_title,
parentComment: parentComment.content_text,
replyAuthor: author,
replyContent: content,
postUrl: data.post_url,
});
await c.env.CWD_DB.prepare(
"INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)"
).bind(parentComment.email, 'user-reply', ip, new Date().toISOString()).run();
console.log('PostComment:mailDispatch:userReply:logInserted', {
toEmail: parentComment.email
});
}
console.log('PostComment:mailDispatch:userReply:send', {
toEmail: parentComment.email,
toName: parentComment.author
});
await sendCommentReplyNotification(c.env, {
toEmail: parentComment.email,
toName: parentComment.author,
postTitle: data.post_title,
parentComment: parentComment.content_text,
replyAuthor: author,
replyContent: content,
postUrl: data.post_url,
});
await c.env.CWD_DB.prepare(
"INSERT INTO EmailLog (recipient, type, ip_address, created_at) VALUES (?, ?, ?, ?)"
).bind(parentComment.email, 'user-reply', ip, new Date().toISOString()).run();
console.log('PostComment:mailDispatch:userReply:logInserted', {
toEmail: parentComment.email
});
}
}
} else {
@@ -188,21 +182,17 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
).first<{ created_at: string }>();
const canSendAdminMail = !adminEmailRow || (Date.now() - new Date(adminEmailRow.created_at).getTime() > 15 * 1000);
if (canSendAdminMail) {
if (!notifySettings.adminEnabled) {
console.log('PostComment:mailDispatch:admin:disabledByConfig');
} else {
console.log('PostComment:mailDispatch:admin:send');
await sendCommentNotification(c.env, {
postTitle: data.post_title,
postUrl: data.post_url,
commentAuthor: author,
commentContent: content
});
await c.env.CWD_DB.prepare(
"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');
}
console.log('PostComment:mailDispatch:admin:send');
await sendCommentNotification(c.env, {
postTitle: data.post_title,
postUrl: data.post_url,
commentAuthor: author,
commentContent: content
});
await c.env.CWD_DB.prepare(
"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) {
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 globalEnabled =
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, {
globalEnabled,
adminEnabled,
userEnabled
globalEnabled
});
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_ADMIN_KEY = 'email_notify_admin_enabled';
const EMAIL_NOTIFY_USER_KEY = 'email_notify_user_enabled';
type MailGatewayPayload = {
to: string[];
@@ -44,8 +42,6 @@ async function dispatchMail(env: Bindings, payload: MailGatewayPayload) {
export type EmailNotificationSettings = {
globalEnabled: boolean;
adminEnabled: boolean;
userEnabled: 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)'
).run();
const keys = [EMAIL_NOTIFY_GLOBAL_KEY, EMAIL_NOTIFY_ADMIN_KEY, EMAIL_NOTIFY_USER_KEY];
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 }>();
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 adminEnabled = globalEnabled;
const userEnabled = globalEnabled;
return {
globalEnabled,
adminEnabled,
userEnabled
globalEnabled
};
}
@@ -89,50 +80,23 @@ export async function saveEmailNotificationSettings(
env: Bindings,
settings: {
globalEnabled?: boolean;
adminEnabled?: boolean;
userEnabled?: boolean;
}
) {
await env.CWD_DB.prepare(
'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)'
).run();
const entries: { key: string; value: string | undefined }[] = [
{
key: EMAIL_NOTIFY_GLOBAL_KEY,
value:
typeof settings.globalEnabled === 'boolean'
? settings.globalEnabled
? '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
}
];
const value =
typeof settings.globalEnabled === 'boolean'
? settings.globalEnabled
? '1'
: '0'
: undefined;
for (const entry of entries) {
if (entry.value !== undefined) {
await env.CWD_DB.prepare('REPLACE INTO Settings (key, value) VALUES (?, ?)')
.bind(entry.key, entry.value)
.run();
}
if (value !== undefined) {
await env.CWD_DB.prepare('REPLACE INTO Settings (key, value) VALUES (?, ?)')
.bind(EMAIL_NOTIFY_GLOBAL_KEY, value)
.run();
}
}