Browse Source

use api v2 for add comment

master
hole-thu 3 years ago
parent
commit
999ca30448
  1. 13
      src/api/comment.rs
  2. 2
      src/api/mod.rs
  3. 3
      src/main.rs

13
src/api/comment.rs

@ -15,7 +15,6 @@ use std::collections::HashMap;
#[derive(FromForm)]
pub struct CommentInput {
pid: i32,
#[field(validate = len(1..12289))]
text: String,
use_title: Option<i8>,
@ -112,14 +111,20 @@ pub async fn get_comment(pid: i32, user: CurrentUser, db: Db, rconn: RdsConn) ->
}))
}
#[post("/docomment", data = "<ci>")]
#[post("/docomment")]
pub async fn old_add_comment() -> ApiError {
OldApi.into()
}
#[post("/post/<pid>/comment", data = "<ci>")]
pub async fn add_comment(
pid: i32,
ci: Form<CommentInput>,
user: CurrentUser,
db: Db,
rconn: RdsConn,
) -> JsonApi {
let mut p = Post::get(&db, &rconn, ci.pid).await?;
let mut p = Post::get(&db, &rconn, pid).await?;
let c = Comment::create(
&db,
NewComment {
@ -132,7 +137,7 @@ pub async fn add_comment(
})
.unwrap_or_default(),
is_tmp: user.id.is_none(),
post_id: ci.pid,
post_id: pid,
},
)
.await?;

2
src/api/mod.rs

@ -116,6 +116,7 @@ pub enum PolicyError {
TitleUsed,
YouAreTmp,
NoReason,
OldApi,
}
#[derive(Debug)]
@ -141,6 +142,7 @@ impl<'r> Responder<'r, 'static> for ApiError {
PolicyError::TitleUsed => "头衔已被使用",
PolicyError::YouAreTmp => "临时用户只可发布内容和进入单个洞",
PolicyError::NoReason => "未填写理由",
PolicyError::OldApi => "请使用最新版前端地址并检查更新"
}
})
.respond_to(req),

3
src/main.rs

@ -64,7 +64,7 @@ async fn main() -> Result<(), rocket::Error> {
"/_api/v1",
routes![
api::comment::get_comment,
api::comment::add_comment,
api::comment::old_add_comment,
api::post::get_list,
api::post::get_one,
api::post::publish_post,
@ -84,6 +84,7 @@ async fn main() -> Result<(), rocket::Error> {
cors::options_handler,
],
)
.mount("/_api/v2", routes![api::comment::add_comment])
.mount(
"/_login",
[

Loading…
Cancel
Save