diff --git a/cwd-comments-admin/src/api/admin.ts b/cwd-comments-admin/src/api/admin.ts index b01c590..fedf858 100644 --- a/cwd-comments-admin/src/api/admin.ts +++ b/cwd-comments-admin/src/api/admin.ts @@ -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 { @@ -83,8 +81,6 @@ export function fetchEmailNotifySettings(): Promise export function saveEmailNotifySettings(data: { globalEnabled?: boolean; - adminEnabled?: boolean; - userEnabled?: boolean; }): Promise<{ message: string }> { return put<{ message: string }>('/admin/settings/email-notify', data); } diff --git a/cwd-comments-admin/src/views/LayoutView.vue b/cwd-comments-admin/src/views/LayoutView.vue index 2a2164d..a057248 100644 --- a/cwd-comments-admin/src/views/LayoutView.vue +++ b/cwd-comments-admin/src/views/LayoutView.vue @@ -8,7 +8,7 @@ href="https://cwd-comments-docs.zishu.me" target="_blank" > - 文档 + 使用文档 ) => { }); 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'); diff --git a/cwd-comments-api/src/index.ts b/cwd-comments-api/src/index.ts index e725dfc..88b4f22 100644 --- a/cwd-comments-api/src/index.ts +++ b/cwd-comments-api/src/index.ts @@ -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: '保存成功' }); diff --git a/cwd-comments-api/src/utils/email.ts b/cwd-comments-api/src/utils/email.ts index 2011882..32a6828 100644 --- a/cwd-comments-api/src/utils/email.ts +++ b/cwd-comments-api/src/utils/email.ts @@ -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(); @@ -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(); } }