fix(dead_code): 清理假阳性与真死代码
- 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
This commit is contained in:
parent
7d713cabc2
commit
2caca3a48d
22
src/cache.rs
22
src/cache.rs
@ -389,15 +389,6 @@ pub async fn invalidate_pending_count() {
|
|||||||
.await;
|
.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"))]
|
#[cfg(all(test, feature = "server"))]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -613,17 +604,4 @@ mod tests {
|
|||||||
assert!(get_pending_count().await.is_none());
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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<i64>,
|
|
||||||
/// 嵌套深度,0 表示顶层评论。
|
|
||||||
pub depth: i32,
|
|
||||||
/// 评论者名称。
|
|
||||||
pub author_name: String,
|
|
||||||
/// 评论者邮箱,用于 Gravatar 与后台联系。
|
|
||||||
pub author_email: String,
|
|
||||||
/// 评论者个人主页 URL。
|
|
||||||
pub author_url: Option<String>,
|
|
||||||
/// 原始 Markdown 内容。
|
|
||||||
pub content_md: String,
|
|
||||||
/// 渲染后的 HTML 内容。
|
|
||||||
pub content_html: Option<String>,
|
|
||||||
/// 内容哈希,用于检测重复或垃圾评论。
|
|
||||||
pub content_hash: Option<String>,
|
|
||||||
/// 当前审核状态。
|
|
||||||
pub status: CommentStatus,
|
|
||||||
/// 评论者 IP 地址。
|
|
||||||
pub ip_address: Option<String>,
|
|
||||||
/// 评论者浏览器 User-Agent。
|
|
||||||
pub user_agent: Option<String>,
|
|
||||||
/// 审核通过时间。
|
|
||||||
pub approved_at: Option<DateTime<Utc>>,
|
|
||||||
/// 评论创建时间。
|
|
||||||
pub created_at: DateTime<Utc>,
|
|
||||||
/// 评论最后更新时间。
|
|
||||||
pub updated_at: DateTime<Utc>,
|
|
||||||
/// 软删除时间。
|
|
||||||
pub deleted_at: Option<DateTime<Utc>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 面向前端展示的评论结构体,已脱敏。
|
/// 面向前端展示的评论结构体,已脱敏。
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
pub struct PublicComment {
|
pub struct PublicComment {
|
||||||
|
|||||||
@ -5,8 +5,10 @@ pub const DEFAULT_RETENTION_DAYS: i32 = 30;
|
|||||||
/// 默认不启用自动清理。
|
/// 默认不启用自动清理。
|
||||||
pub const DEFAULT_AUTO_PURGE_ENABLED: bool = false;
|
pub const DEFAULT_AUTO_PURGE_ENABLED: bool = false;
|
||||||
/// 保留天数下限(天)。
|
/// 保留天数下限(天)。
|
||||||
|
#[cfg(feature = "server")]
|
||||||
pub const MIN_RETENTION_DAYS: i32 = 1;
|
pub const MIN_RETENTION_DAYS: i32 = 1;
|
||||||
/// 保留天数上限(天)。防止误填超大值导致永不清理。
|
/// 保留天数上限(天)。防止误填超大值导致永不清理。
|
||||||
|
#[cfg(feature = "server")]
|
||||||
pub const MAX_RETENTION_DAYS: i32 = 365;
|
pub const MAX_RETENTION_DAYS: i32 = 365;
|
||||||
|
|
||||||
/// 回收站配置。
|
/// 回收站配置。
|
||||||
@ -29,6 +31,7 @@ impl Default for TrashSettings {
|
|||||||
|
|
||||||
impl TrashSettings {
|
impl TrashSettings {
|
||||||
/// 将保留天数钳制到合法范围 [MIN, MAX]。
|
/// 将保留天数钳制到合法范围 [MIN, MAX]。
|
||||||
|
#[cfg(feature = "server")]
|
||||||
pub fn clamp_retention(days: i32) -> i32 {
|
pub fn clamp_retention(days: i32) -> i32 {
|
||||||
days.clamp(MIN_RETENTION_DAYS, MAX_RETENTION_DAYS)
|
days.clamp(MIN_RETENTION_DAYS, MAX_RETENTION_DAYS)
|
||||||
}
|
}
|
||||||
@ -46,24 +49,28 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "server")]
|
||||||
fn clamp_retention_keeps_valid() {
|
fn clamp_retention_keeps_valid() {
|
||||||
assert_eq!(TrashSettings::clamp_retention(7), 7);
|
assert_eq!(TrashSettings::clamp_retention(7), 7);
|
||||||
assert_eq!(TrashSettings::clamp_retention(30), 30);
|
assert_eq!(TrashSettings::clamp_retention(30), 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "server")]
|
||||||
fn clamp_retention_clamps_below_min() {
|
fn clamp_retention_clamps_below_min() {
|
||||||
assert_eq!(TrashSettings::clamp_retention(0), MIN_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(0), MIN_RETENTION_DAYS);
|
||||||
assert_eq!(TrashSettings::clamp_retention(-5), MIN_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(-5), MIN_RETENTION_DAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "server")]
|
||||||
fn clamp_retention_clamps_above_max() {
|
fn clamp_retention_clamps_above_max() {
|
||||||
assert_eq!(TrashSettings::clamp_retention(366), MAX_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(366), MAX_RETENTION_DAYS);
|
||||||
assert_eq!(TrashSettings::clamp_retention(i32::MAX), MAX_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(i32::MAX), MAX_RETENTION_DAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "server")]
|
||||||
fn clamp_retention_boundary() {
|
fn clamp_retention_boundary() {
|
||||||
assert_eq!(TrashSettings::clamp_retention(MIN_RETENTION_DAYS), MIN_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(MIN_RETENTION_DAYS), MIN_RETENTION_DAYS);
|
||||||
assert_eq!(TrashSettings::clamp_retention(MAX_RETENTION_DAYS), MAX_RETENTION_DAYS);
|
assert_eq!(TrashSettings::clamp_retention(MAX_RETENTION_DAYS), MAX_RETENTION_DAYS);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user