2018-06-04 21:57:03 +02:00
|
|
|
use rocket::{
|
|
|
|
request::Form,
|
|
|
|
response::{Redirect, Flash}
|
|
|
|
};
|
2018-05-10 11:44:57 +02:00
|
|
|
use rocket_contrib::Template;
|
|
|
|
|
2018-06-18 13:32:03 +02:00
|
|
|
use activity_pub::{broadcast, IntoId, inbox::Notify};
|
2018-05-10 11:44:57 +02:00
|
|
|
use db_conn::DbConn;
|
2018-05-19 09:39:59 +02:00
|
|
|
use models::{
|
2018-06-19 21:16:18 +02:00
|
|
|
blogs::Blog,
|
2018-05-19 09:39:59 +02:00
|
|
|
comments::*,
|
|
|
|
posts::Post,
|
|
|
|
users::User
|
|
|
|
};
|
2018-05-10 11:44:57 +02:00
|
|
|
|
2018-06-04 21:57:03 +02:00
|
|
|
use utils;
|
2018-06-11 12:21:34 +02:00
|
|
|
use safe_string::SafeString;
|
2018-06-04 21:57:03 +02:00
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
#[get("/~/<blog>/<slug>/comment")]
|
|
|
|
fn new(blog: String, slug: String, user: User, conn: DbConn) -> Template {
|
|
|
|
may_fail!(Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
|
|
|
|
may_fail!(Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| {
|
|
|
|
Template::render("comments/new", json!({
|
|
|
|
"post": post,
|
|
|
|
"account": user
|
|
|
|
}))
|
|
|
|
})
|
2018-06-18 19:28:28 +02:00
|
|
|
})
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:57:03 +02:00
|
|
|
#[get("/~/<blog>/<slug>/comment", rank=2)]
|
|
|
|
fn new_auth(blog: String, slug: String) -> Flash<Redirect>{
|
2018-06-19 23:20:27 +02:00
|
|
|
utils::requires_login("You need to be logged in order to post a comment", uri!(new: blog = blog, slug = slug))
|
2018-06-04 21:57:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:26:12 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct CommentQuery {
|
|
|
|
responding_to: Option<i32>
|
|
|
|
}
|
|
|
|
|
2018-05-10 11:44:57 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewCommentForm {
|
2018-05-10 16:26:12 +02:00
|
|
|
pub content: String
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
#[post("/~/<blog_name>/<slug>/comment?<query>", data = "<data>")]
|
|
|
|
fn create(blog_name: String, slug: String, query: CommentQuery, data: Form<NewCommentForm>, user: User, conn: DbConn) -> Redirect {
|
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog_name.clone()).unwrap();
|
|
|
|
let post = Post::find_by_slug(&*conn, slug.clone(), blog.id).unwrap();
|
2018-05-10 11:44:57 +02:00
|
|
|
let form = data.get();
|
2018-05-10 15:58:17 +02:00
|
|
|
let comment = Comment::insert(&*conn, NewComment {
|
2018-06-11 12:21:34 +02:00
|
|
|
content: SafeString::new(&form.content.clone()),
|
2018-05-10 16:26:12 +02:00
|
|
|
in_response_to_id: query.responding_to,
|
2018-05-10 11:44:57 +02:00
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id,
|
|
|
|
ap_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
spoiler_text: "".to_string()
|
|
|
|
});
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2018-06-18 13:32:03 +02:00
|
|
|
Comment::notify(&*conn, comment.into_activity(&*conn), user.clone().into_id());
|
2018-05-19 00:04:30 +02:00
|
|
|
broadcast(&*conn, &user, comment.create_activity(&*conn), user.get_followers(&*conn));
|
2018-05-10 17:36:32 +02:00
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, comment.id))
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|