From 2caca3a48dd6dcb256bbbdaef156dab72f512673 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 16 Jun 2026 17:48:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(dead=5Fcode):=20=E6=B8=85=E7=90=86=E5=81=87?= =?UTF-8?q?=E9=98=B3=E6=80=A7=E4=B8=8E=E7=9C=9F=E6=AD=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - settings.rs: retention 常量与 clamp_retention 仅服务端使用,加 #[cfg(feature = "server")] - comment.rs: 删除未使用的 Comment 结构体 - cache.rs: 删除未使用的 invalidate_all_comment_caches 及其测试 验证: cargo check (server/wasm), cargo test (311 passed), dx check, cargo clippy --- src/cache.rs | 22 ---------------------- src/models/comment.rs | 42 ------------------------------------------ src/models/settings.rs | 7 +++++++ 3 files changed, 7 insertions(+), 64 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index af4c648..f393e41 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -389,15 +389,6 @@ pub async fn invalidate_pending_count() { .await; } -/// 清空所有评论相关缓存。 -#[cfg(feature = "server")] -#[allow(dead_code)] -pub async fn invalidate_all_comment_caches() { - COMMENT_CACHE.invalidate_all(); - COMMENT_COUNT_CACHE.invalidate_all(); - PENDING_COUNT_CACHE.invalidate_all(); -} - #[cfg(all(test, feature = "server"))] mod tests { use super::*; @@ -613,17 +604,4 @@ mod tests { assert!(get_pending_count().await.is_none()); } - #[tokio::test] - #[serial] - async fn invalidate_all_comment_caches_clears_everything() { - set_comments_by_post(1, vec![]).await; - set_comment_count(1, 10).await; - set_pending_count(5).await; - - invalidate_all_comment_caches().await; - - assert!(get_comments_by_post(1).await.is_none()); - assert!(get_comment_count(1).await.is_none()); - assert!(get_pending_count().await.is_none()); - } } diff --git a/src/models/comment.rs b/src/models/comment.rs index 248b455..9015bad 100644 --- a/src/models/comment.rs +++ b/src/models/comment.rs @@ -44,48 +44,6 @@ impl CommentStatus { } } -/// 服务端内部使用的完整评论结构体,仅在启用 server feature 时编译。 -/// -/// 包含作者邮箱、IP、User-Agent 等敏感或管理字段,不直接返回给前端。 -#[cfg(feature = "server")] -#[allow(dead_code)] -pub struct Comment { - /// 评论主键。 - pub id: i64, - /// 所属文章主键。 - pub post_id: i32, - /// 父评论主键,用于实现嵌套回复。 - pub parent_id: Option, - /// 嵌套深度,0 表示顶层评论。 - pub depth: i32, - /// 评论者名称。 - pub author_name: String, - /// 评论者邮箱,用于 Gravatar 与后台联系。 - pub author_email: String, - /// 评论者个人主页 URL。 - pub author_url: Option, - /// 原始 Markdown 内容。 - pub content_md: String, - /// 渲染后的 HTML 内容。 - pub content_html: Option, - /// 内容哈希,用于检测重复或垃圾评论。 - pub content_hash: Option, - /// 当前审核状态。 - pub status: CommentStatus, - /// 评论者 IP 地址。 - pub ip_address: Option, - /// 评论者浏览器 User-Agent。 - pub user_agent: Option, - /// 审核通过时间。 - pub approved_at: Option>, - /// 评论创建时间。 - pub created_at: DateTime, - /// 评论最后更新时间。 - pub updated_at: DateTime, - /// 软删除时间。 - pub deleted_at: Option>, -} - /// 面向前端展示的评论结构体,已脱敏。 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct PublicComment { diff --git a/src/models/settings.rs b/src/models/settings.rs index d04a417..cc41ca2 100644 --- a/src/models/settings.rs +++ b/src/models/settings.rs @@ -5,8 +5,10 @@ pub const DEFAULT_RETENTION_DAYS: i32 = 30; /// 默认不启用自动清理。 pub const DEFAULT_AUTO_PURGE_ENABLED: bool = false; /// 保留天数下限(天)。 +#[cfg(feature = "server")] pub const MIN_RETENTION_DAYS: i32 = 1; /// 保留天数上限(天)。防止误填超大值导致永不清理。 +#[cfg(feature = "server")] pub const MAX_RETENTION_DAYS: i32 = 365; /// 回收站配置。 @@ -29,6 +31,7 @@ impl Default for TrashSettings { impl TrashSettings { /// 将保留天数钳制到合法范围 [MIN, MAX]。 + #[cfg(feature = "server")] pub fn clamp_retention(days: i32) -> i32 { days.clamp(MIN_RETENTION_DAYS, MAX_RETENTION_DAYS) } @@ -46,24 +49,28 @@ mod tests { } #[test] + #[cfg(feature = "server")] fn clamp_retention_keeps_valid() { assert_eq!(TrashSettings::clamp_retention(7), 7); assert_eq!(TrashSettings::clamp_retention(30), 30); } #[test] + #[cfg(feature = "server")] fn clamp_retention_clamps_below_min() { assert_eq!(TrashSettings::clamp_retention(0), MIN_RETENTION_DAYS); assert_eq!(TrashSettings::clamp_retention(-5), MIN_RETENTION_DAYS); } #[test] + #[cfg(feature = "server")] fn clamp_retention_clamps_above_max() { assert_eq!(TrashSettings::clamp_retention(366), MAX_RETENTION_DAYS); assert_eq!(TrashSettings::clamp_retention(i32::MAX), MAX_RETENTION_DAYS); } #[test] + #[cfg(feature = "server")] fn clamp_retention_boundary() { assert_eq!(TrashSettings::clamp_retention(MIN_RETENTION_DAYS), MIN_RETENTION_DAYS); assert_eq!(TrashSettings::clamp_retention(MAX_RETENTION_DAYS), MAX_RETENTION_DAYS);