feat(admin): 在评论列表中显示管理员标识

- 从数据库设置中获取管理员邮箱配置
- 在评论作者名字旁添加管理员图标标识
- 优化数据库查询,使用 Promise.all 并行获取配置
This commit is contained in:
anghunk
2026-01-27 15:35:27 +08:00
parent 4095473a79
commit 221a50b262
2 changed files with 20 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ import { Bindings } from '../../bindings';
import { getCravatar } from '../../utils/getAvatar';
const COMMENT_AVATAR_PREFIX_KEY = 'comment_avatar_prefix';
const COMMENT_ADMIN_EMAIL_KEY = 'comment_admin_email';
export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
const page = parseInt(c.req.query('page') || '1');
@@ -35,10 +36,16 @@ export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
await c.env.CWD_DB.prepare(
'CREATE TABLE IF NOT EXISTS Settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)'
).run();
const avatarRow = await c.env.CWD_DB.prepare('SELECT value FROM Settings WHERE key = ?')
.bind(COMMENT_AVATAR_PREFIX_KEY)
.first<{ value: string }>();
const [avatarRow, adminEmailRow] = await Promise.all([
c.env.CWD_DB.prepare('SELECT value FROM Settings WHERE key = ?')
.bind(COMMENT_AVATAR_PREFIX_KEY)
.first<{ value: string }>(),
c.env.CWD_DB.prepare('SELECT value FROM Settings WHERE key = ?')
.bind(COMMENT_ADMIN_EMAIL_KEY)
.first<{ value: string }>()
]);
const avatarPrefix = avatarRow?.value || null;
const adminEmail = adminEmailRow?.value || null;
const data = await Promise.all(
results.map(async (row: any) => ({
@@ -53,9 +60,13 @@ export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
contentHtml: row.content_html,
status: row.status,
priority: row.priority,
likes: typeof row.likes === 'number' && Number.isFinite(row.likes) && row.likes >= 0 ? row.likes : 0,
likes:
typeof row.likes === 'number' && Number.isFinite(row.likes) && row.likes >= 0
? row.likes
: 0,
ua: row.ua,
avatar: await getCravatar(row.email, avatarPrefix || undefined)
avatar: await getCravatar(row.email, avatarPrefix || undefined),
isAdmin: adminEmail && row.email === adminEmail
}))
);