Plume/src/routes/comments.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

2018-05-19 07:39:59 +00:00
use rocket::{ request::Form, response::Redirect};
2018-05-10 09:44:57 +00:00
use rocket_contrib::Template;
2018-05-16 18:20:44 +00:00
use activity_pub::broadcast;
2018-05-10 09:44:57 +00:00
use db_conn::DbConn;
2018-05-19 07:39:59 +00:00
use models::{
comments::*,
posts::Post,
users::User
};
2018-05-10 09:44:57 +00:00
#[get("/~/<_blog>/<slug>/comment")]
2018-05-10 20:31:52 +00:00
fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template {
2018-05-10 09:44:57 +00:00
let post = Post::find_by_slug(&*conn, slug).unwrap();
Template::render("comments/new", json!({
"post": post,
2018-05-10 20:31:52 +00:00
"account": user
2018-05-10 09:44:57 +00:00
}))
}
#[derive(FromForm)]
struct CommentQuery {
responding_to: Option<i32>
}
2018-05-10 09:44:57 +00:00
#[derive(FromForm)]
struct NewCommentForm {
pub content: String
2018-05-10 09:44:57 +00:00
}
#[post("/~/<blog>/<slug>/comment?<query>", data = "<data>")]
fn create(blog: String, slug: String, query: CommentQuery, data: Form<NewCommentForm>, user: User, conn: DbConn) -> Redirect {
let post = Post::find_by_slug(&*conn, slug.clone()).unwrap();
2018-05-10 09:44:57 +00:00
let form = data.get();
let comment = Comment::insert(&*conn, NewComment {
2018-05-10 09:44:57 +00:00
content: form.content.clone(),
in_response_to_id: query.responding_to,
2018-05-10 09:44:57 +00:00
post_id: post.id,
author_id: user.id,
ap_url: None,
sensitive: false,
spoiler_text: "".to_string()
});
2018-05-18 22:04:30 +00:00
broadcast(&*conn, &user, comment.create_activity(&*conn), user.get_followers(&*conn));
2018-05-10 15:36:32 +00:00
Redirect::to(format!("/~/{}/{}/#comment-{}", blog, slug, comment.id).as_ref())
2018-05-10 09:44:57 +00:00
}