From fe8778380c3ff8b614a9fbf192233a6e2fbbe554 Mon Sep 17 00:00:00 2001 From: hole-thu Date: Wed, 16 Mar 2022 01:58:21 +0800 Subject: [PATCH] fix: use null for comments --- src/api/mod.rs | 9 +++------ src/api/post.rs | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 7098fee..ac74b6a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -3,14 +3,11 @@ use crate::random_hasher::RandomHasher; use rocket::http::Status; use rocket::request::{self, FromRequest, Request}; use rocket::response::{self, Responder}; -use rocket::serde::json::{json, Value}; +use rocket::serde::json::json; #[catch(401)] -pub fn catch_401_error() -> Value { - json!({ - "code": -1, - "msg": "未登录或token过期" - }) +pub fn catch_401_error() -> &'static str { + "未登录或token过期" } pub struct CurrentUser { diff --git a/src/api/post.rs b/src/api/post.rs index f48fe53..1d6ef11 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -25,18 +25,19 @@ pub struct PostOutput { pid: i32, text: String, cw: Option, - author_title: String, + custom_title: Option, n_likes: i32, n_comments: i32, create_time: NaiveDateTime, last_comment_time: NaiveDateTime, allow_search: bool, is_reported: Option, - comments: Vec, + comments: Option>, can_del: bool, // for old version frontend timestamp: i64, - custom_title: Option, + likenum: i32, + reply: i32, } fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput { @@ -49,31 +50,32 @@ fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput } else { None }, - author_title: p.author_title.to_string(), n_likes: p.n_likes, n_comments: p.n_comments, create_time: p.create_time, last_comment_time: p.last_comment_time, 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 { Some(p.is_reported) } else { None }, comments: if p.n_comments > 50 { - vec![] + None } 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, // for old version frontend timestamp: p.create_time.timestamp(), - custom_title: if p.author_title.len() > 0 { - Some(p.author_title.to_string()) - } else { - None - }, + likenum: p.n_likes, + reply: p.n_comments, } }