Browse Source

fix: use null for comments

master
hole-thu 3 years ago
parent
commit
fe8778380c
  1. 9
      src/api/mod.rs
  2. 24
      src/api/post.rs

9
src/api/mod.rs

@ -3,14 +3,11 @@ use crate::random_hasher::RandomHasher;
use rocket::http::Status; use rocket::http::Status;
use rocket::request::{self, FromRequest, Request}; use rocket::request::{self, FromRequest, Request};
use rocket::response::{self, Responder}; use rocket::response::{self, Responder};
use rocket::serde::json::{json, Value}; use rocket::serde::json::json;
#[catch(401)] #[catch(401)]
pub fn catch_401_error() -> Value { pub fn catch_401_error() -> &'static str {
json!({ "未登录或token过期"
"code": -1,
"msg": "未登录或token过期"
})
} }
pub struct CurrentUser { pub struct CurrentUser {

24
src/api/post.rs

@ -25,18 +25,19 @@ pub struct PostOutput {
pid: i32, pid: i32,
text: String, text: String,
cw: Option<String>, cw: Option<String>,
author_title: String, custom_title: Option<String>,
n_likes: i32, n_likes: i32,
n_comments: i32, n_comments: i32,
create_time: NaiveDateTime, create_time: NaiveDateTime,
last_comment_time: NaiveDateTime, last_comment_time: NaiveDateTime,
allow_search: bool, allow_search: bool,
is_reported: Option<bool>, is_reported: Option<bool>,
comments: Vec<CommentOutput>, comments: Option<Vec<CommentOutput>>,
can_del: bool, can_del: bool,
// for old version frontend // for old version frontend
timestamp: i64, timestamp: i64,
custom_title: Option<String>, likenum: i32,
reply: i32,
} }
fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput { fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput {
@ -49,31 +50,32 @@ fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput
} else { } else {
None None
}, },
author_title: p.author_title.to_string(),
n_likes: p.n_likes, n_likes: p.n_likes,
n_comments: p.n_comments, n_comments: p.n_comments,
create_time: p.create_time, create_time: p.create_time,
last_comment_time: p.last_comment_time, last_comment_time: p.last_comment_time,
allow_search: p.allow_search, allow_search: p.allow_search,
custom_title: if p.author_title.len() > 0 {
Some(p.author_title.to_string())
} else {
None
},
is_reported: if user.is_admin { is_reported: if user.is_admin {
Some(p.is_reported) Some(p.is_reported)
} else { } else {
None None
}, },
comments: if p.n_comments > 50 { comments: if p.n_comments > 50 {
vec![] None
} else { } else {
// 单个洞还有查询评论的接口,这里挂了不用报错 // 单个洞还有查询评论的接口,这里挂了不用报错
c2output(p, &p.get_comments(conn).unwrap_or(vec![]), user) Some(c2output(p, &p.get_comments(conn).unwrap_or(vec![]), user))
}, },
can_del: user.is_admin || p.author_hash == user.namehash, can_del: user.is_admin || p.author_hash == user.namehash,
// for old version frontend // for old version frontend
timestamp: p.create_time.timestamp(), timestamp: p.create_time.timestamp(),
custom_title: if p.author_title.len() > 0 { likenum: p.n_likes,
Some(p.author_title.to_string()) reply: p.n_comments,
} else {
None
},
} }
} }

Loading…
Cancel
Save