Browse Source

use custom_title in syslog

master
hole-thu 3 years ago
parent
commit
e0d6720433
  1. 9
      src/api/comment.rs
  2. 7
      src/api/mod.rs
  3. 8
      src/api/operation.rs
  4. 9
      src/api/post.rs
  5. 2
      src/api/systemlog.rs
  6. 27
      src/rds_models.rs

9
src/api/comment.rs

@ -127,11 +127,10 @@ pub async fn add_comment(
NewComment { NewComment {
content: ci.text.to_string(), content: ci.text.to_string(),
author_hash: user.namehash.to_string(), author_hash: user.namehash.to_string(),
author_title: if use_title { author_title: user
user.custom_title .custom_title
} else { .and_then(|title| use_title.then_some(title))
"".to_owned() .unwrap_or_default(),
},
is_tmp: user.id.is_none(), is_tmp: user.id.is_none(),
post_id: pid, post_id: pid,
}, },

7
src/api/mod.rs

@ -65,8 +65,8 @@ pub struct CurrentUser {
namehash: String, namehash: String,
is_admin: bool, is_admin: bool,
is_candidate: bool, is_candidate: bool,
custom_title: String, custom_title: Option<String>,
title_secret: String, title_secret: Option<String>,
pub auto_block_rank: u8, pub auto_block_rank: u8,
} }
@ -75,8 +75,7 @@ impl CurrentUser {
let (custom_title, title_secret) = CustomTitle::get(rconn, &namehash) let (custom_title, title_secret) = CustomTitle::get(rconn, &namehash)
.await .await
.ok() .ok()
.flatten() .unwrap_or((None, None));
.unwrap_or_default();
Self { Self {
id: None, id: None,
is_admin: false, is_admin: false,

8
src/api/operation.rs

@ -64,7 +64,7 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: Rds
if user.is_admin && !user.namehash.eq(&author_hash) { if user.is_admin && !user.namehash.eq(&author_hash) {
Systemlog { Systemlog {
user_hash: user.namehash.clone(), user_hash: user.custom_title.clone().unwrap_or(look!(user.namehash)),
action_type: LogType::AdminDelete, action_type: LogType::AdminDelete,
target: format!("#{}, {}={}", p.id, di.id_type, di.id), target: format!("#{}, {}={}", p.id, di.id_type, di.id),
detail: di.note.clone(), detail: di.note.clone(),
@ -75,7 +75,7 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: Rds
if di.note.starts_with("!ban ") { if di.note.starts_with("!ban ") {
Systemlog { Systemlog {
user_hash: user.namehash.clone(), user_hash: user.custom_title.unwrap_or(look!(user.namehash)),
action_type: LogType::Ban, action_type: LogType::Ban,
target: look!(author_hash), target: look!(author_hash),
detail: di.note.clone(), detail: di.note.clone(),
@ -120,7 +120,7 @@ pub async fn report(ri: Form<ReportInput>, user: CurrentUser, db: Db, rconn: Rds
} }
Systemlog { Systemlog {
user_hash: user.namehash.to_string(), user_hash: user.custom_title.unwrap_or(look!(user.namehash)),
action_type: LogType::Report, action_type: LogType::Report,
target: format!("#{}", ri.pid), target: format!("#{}", ri.pid),
detail: ri.reason.clone(), detail: ri.reason.clone(),
@ -135,7 +135,7 @@ pub async fn report(ri: Form<ReportInput>, user: CurrentUser, db: Db, rconn: Rds
NewPost { NewPost {
content: format!("[系统自动代发]\n我举报了 #{}\n理由: {}", &p.id, &ri.reason), content: format!("[系统自动代发]\n我举报了 #{}\n理由: {}", &p.id, &ri.reason),
cw: "举报".to_string(), cw: "举报".to_string(),
author_hash: user.namehash.to_string(), author_hash: user.namehash.clone(),
author_title: String::default(), author_title: String::default(),
is_tmp: false, is_tmp: false,
n_attentions: 1, n_attentions: 1,

9
src/api/post.rs

@ -200,11 +200,10 @@ pub async fn publish_post(
content: poi.text.to_string(), content: poi.text.to_string(),
cw: poi.cw.to_string(), cw: poi.cw.to_string(),
author_hash: user.namehash.to_string(), author_hash: user.namehash.to_string(),
author_title: if use_title { author_title: user
user.custom_title .custom_title
} else { .and_then(|title| use_title.then_some(title))
"".to_owned() .unwrap_or_default(),
},
is_tmp: user.id.is_none(), is_tmp: user.id.is_none(),
n_attentions: 1, n_attentions: 1,
allow_search: poi.allow_search.is_some(), allow_search: poi.allow_search.is_some(),

2
src/api/systemlog.rs

@ -19,7 +19,7 @@ pub async fn get_systemlog(user: CurrentUser, rh: &State<RandomHasher>, rconn: R
"data": logs.into_iter().map(|log| "data": logs.into_iter().map(|log|
json!({ json!({
"type": log.action_type, "type": log.action_type,
"user": look!(log.user_hash), "user": log.user_hash,
"timestamp": log.time.timestamp(), "timestamp": log.time.timestamp(),
"detail": format!("{}\n{}", &log.target, &log.detail), "detail": format!("{}\n{}", &log.target, &log.detail),
}) })

27
src/rds_models.rs

@ -250,7 +250,10 @@ impl CustomTitle {
} }
} }
pub async fn get(rconn: &RdsConn, namehash: &str) -> RedisResult<Option<(String, String)>> { pub async fn get(
rconn: &RdsConn,
namehash: &str,
) -> RedisResult<(Option<String>, Option<String>)> {
let t: Option<String> = rconn.clone().hget(KEY_CUSTOM_TITLE, namehash).await?; let t: Option<String> = rconn.clone().hget(KEY_CUSTOM_TITLE, namehash).await?;
Ok(if let Some(title) = t { Ok(if let Some(title) = t {
let s: Option<String> = rconn.clone().get(KEY_TITLE_SECRET!(title)).await?; let s: Option<String> = rconn.clone().get(KEY_TITLE_SECRET!(title)).await?;
@ -263,9 +266,9 @@ impl CustomTitle {
} else { } else {
Self::gen_and_set_secret(rconn, &title).await? Self::gen_and_set_secret(rconn, &title).await?
}; };
Some((title, secret)) (Some(title), Some(secret))
} else { } else {
None (None, None)
}) })
} }
@ -340,18 +343,20 @@ pub async fn get_announcement(rconn: &RdsConn) -> RedisResult<Option<String>> {
rconn.clone().get(KEY_ANNOUNCEMENT).await rconn.clone().get(KEY_ANNOUNCEMENT).await
} }
pub async fn is_elected_candidate(rconn: &RdsConn, title: &str) -> RedisResult<bool> { pub async fn is_elected_candidate(rconn: &RdsConn, title: &Option<String>) -> RedisResult<bool> {
if title.is_empty() { if let Some(t) = title {
return Ok(false); 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<bool> { pub async fn is_elected_admin(rconn: &RdsConn, title: &Option<String>) -> RedisResult<bool> {
if title.is_empty() { if let Some(t) = title {
return Ok(false); 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<Vec<String>> { pub async fn get_admin_list(rconn: &RdsConn) -> RedisResult<Vec<String>> {

Loading…
Cancel
Save