From e0d672043383c719140943c04b5fb8900d432c5e Mon Sep 17 00:00:00 2001 From: hole-thu Date: Wed, 5 Oct 2022 10:57:48 +0800 Subject: [PATCH] use custom_title in syslog --- src/api/comment.rs | 9 ++++----- src/api/mod.rs | 7 +++---- src/api/operation.rs | 8 ++++---- src/api/post.rs | 9 ++++----- src/api/systemlog.rs | 2 +- src/rds_models.rs | 27 ++++++++++++++++----------- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/api/comment.rs b/src/api/comment.rs index fca9fe6..e01d756 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -127,11 +127,10 @@ pub async fn add_comment( NewComment { content: ci.text.to_string(), author_hash: user.namehash.to_string(), - author_title: if use_title { - user.custom_title - } else { - "".to_owned() - }, + author_title: user + .custom_title + .and_then(|title| use_title.then_some(title)) + .unwrap_or_default(), is_tmp: user.id.is_none(), post_id: pid, }, diff --git a/src/api/mod.rs b/src/api/mod.rs index f3a06ff..c62e799 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -65,8 +65,8 @@ pub struct CurrentUser { namehash: String, is_admin: bool, is_candidate: bool, - custom_title: String, - title_secret: String, + custom_title: Option, + title_secret: Option, pub auto_block_rank: u8, } @@ -75,8 +75,7 @@ impl CurrentUser { let (custom_title, title_secret) = CustomTitle::get(rconn, &namehash) .await .ok() - .flatten() - .unwrap_or_default(); + .unwrap_or((None, None)); Self { id: None, is_admin: false, diff --git a/src/api/operation.rs b/src/api/operation.rs index c64fa85..1852ed0 100644 --- a/src/api/operation.rs +++ b/src/api/operation.rs @@ -64,7 +64,7 @@ pub async fn delete(di: Form, user: CurrentUser, db: Db, rconn: Rds if user.is_admin && !user.namehash.eq(&author_hash) { Systemlog { - user_hash: user.namehash.clone(), + user_hash: user.custom_title.clone().unwrap_or(look!(user.namehash)), action_type: LogType::AdminDelete, target: format!("#{}, {}={}", p.id, di.id_type, di.id), detail: di.note.clone(), @@ -75,7 +75,7 @@ pub async fn delete(di: Form, user: CurrentUser, db: Db, rconn: Rds if di.note.starts_with("!ban ") { Systemlog { - user_hash: user.namehash.clone(), + user_hash: user.custom_title.unwrap_or(look!(user.namehash)), action_type: LogType::Ban, target: look!(author_hash), detail: di.note.clone(), @@ -120,7 +120,7 @@ pub async fn report(ri: Form, user: CurrentUser, db: Db, rconn: Rds } Systemlog { - user_hash: user.namehash.to_string(), + user_hash: user.custom_title.unwrap_or(look!(user.namehash)), action_type: LogType::Report, target: format!("#{}", ri.pid), detail: ri.reason.clone(), @@ -135,7 +135,7 @@ pub async fn report(ri: Form, user: CurrentUser, db: Db, rconn: Rds NewPost { content: format!("[系统自动代发]\n我举报了 #{}\n理由: {}", &p.id, &ri.reason), cw: "举报".to_string(), - author_hash: user.namehash.to_string(), + author_hash: user.namehash.clone(), author_title: String::default(), is_tmp: false, n_attentions: 1, diff --git a/src/api/post.rs b/src/api/post.rs index 960919f..d3851be 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -200,11 +200,10 @@ pub async fn publish_post( content: poi.text.to_string(), cw: poi.cw.to_string(), author_hash: user.namehash.to_string(), - author_title: if use_title { - user.custom_title - } else { - "".to_owned() - }, + author_title: user + .custom_title + .and_then(|title| use_title.then_some(title)) + .unwrap_or_default(), is_tmp: user.id.is_none(), n_attentions: 1, allow_search: poi.allow_search.is_some(), diff --git a/src/api/systemlog.rs b/src/api/systemlog.rs index 068b287..cce18c7 100644 --- a/src/api/systemlog.rs +++ b/src/api/systemlog.rs @@ -19,7 +19,7 @@ pub async fn get_systemlog(user: CurrentUser, rh: &State, rconn: R "data": logs.into_iter().map(|log| json!({ "type": log.action_type, - "user": look!(log.user_hash), + "user": log.user_hash, "timestamp": log.time.timestamp(), "detail": format!("{}\n{}", &log.target, &log.detail), }) diff --git a/src/rds_models.rs b/src/rds_models.rs index 4d6111e..c89f04f 100644 --- a/src/rds_models.rs +++ b/src/rds_models.rs @@ -250,7 +250,10 @@ impl CustomTitle { } } - pub async fn get(rconn: &RdsConn, namehash: &str) -> RedisResult> { + pub async fn get( + rconn: &RdsConn, + namehash: &str, + ) -> RedisResult<(Option, Option)> { let t: Option = rconn.clone().hget(KEY_CUSTOM_TITLE, namehash).await?; Ok(if let Some(title) = t { let s: Option = rconn.clone().get(KEY_TITLE_SECRET!(title)).await?; @@ -263,9 +266,9 @@ impl CustomTitle { } else { Self::gen_and_set_secret(rconn, &title).await? }; - Some((title, secret)) + (Some(title), Some(secret)) } else { - None + (None, None) }) } @@ -340,18 +343,20 @@ pub async fn get_announcement(rconn: &RdsConn) -> RedisResult> { rconn.clone().get(KEY_ANNOUNCEMENT).await } -pub async fn is_elected_candidate(rconn: &RdsConn, title: &str) -> RedisResult { - if title.is_empty() { - return Ok(false); +pub async fn is_elected_candidate(rconn: &RdsConn, title: &Option) -> RedisResult { + if let Some(t) = title { + rconn.clone().sismember(KEY_CANDIDATE, t).await + } else { + Ok(false) } - rconn.clone().sismember(KEY_CANDIDATE, title).await } -pub async fn is_elected_admin(rconn: &RdsConn, title: &str) -> RedisResult { - if title.is_empty() { - return Ok(false); +pub async fn is_elected_admin(rconn: &RdsConn, title: &Option) -> RedisResult { + if let Some(t) = title { + rconn.clone().sismember(KEY_ADMIN, t).await + } else { + Ok(false) } - rconn.clone().sismember(KEY_ADMIN, title).await } pub async fn get_admin_list(rconn: &RdsConn) -> RedisResult> {