Add support for CW in comments (#308)

All the backend/federation code was already, I just added the UI 🤷‍♀️ 

Fixes #253
This commit is contained in:
Baptiste Gelez 2018-11-07 15:57:31 +01:00 committed by GitHub
parent b28411da99
commit a64c4912cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 9 deletions

View File

@ -20,11 +20,12 @@ use plume_models::{
users::User users::User
}; };
#[derive(FromForm, Debug, Validate)] #[derive(FromForm, Debug, Validate, Serialize)]
struct NewCommentForm { struct NewCommentForm {
pub responding_to: Option<i32>, pub responding_to: Option<i32>,
#[validate(length(min = "1", message = "Your comment can't be empty"))] #[validate(length(min = "1", message = "Your comment can't be empty"))]
pub content: String pub content: String,
pub warning: String,
} }
#[post("/~/<blog_name>/<slug>/comment", data = "<data>")] #[post("/~/<blog_name>/<slug>/comment", data = "<data>")]
@ -42,8 +43,8 @@ fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, us
post_id: post.id, post_id: post.id,
author_id: user.id, author_id: user.id,
ap_url: None, ap_url: None,
sensitive: false, sensitive: form.warning.len() > 0,
spoiler_text: String::new() spoiler_text: form.warning.clone()
}).update_ap_url(&*conn); }).update_ap_url(&*conn);
let new_comment = comm.create_activity(&*conn); let new_comment = comm.create_activity(&*conn);
@ -77,7 +78,8 @@ fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, us
"date": &post.creation_date.timestamp(), "date": &post.creation_date.timestamp(),
"previous": form.responding_to.and_then(|r| Comment::get(&*conn, r)).map(|r| r.to_json(&*conn, &vec![])), "previous": form.responding_to.and_then(|r| Comment::get(&*conn, r)).map(|r| r.to_json(&*conn, &vec![])),
"user_fqn": user.get_fqn(&*conn), "user_fqn": user.get_fqn(&*conn),
"errors": errors "comment_form": form,
"comment_errors": errors,
}))) })))
}) })
} }

View File

@ -44,6 +44,8 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>
let comments = Comment::list_by_post(&*conn, post.id); let comments = Comment::list_by_post(&*conn, post.id);
let comms = comments.clone(); let comms = comments.clone();
let previous = query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r)
.expect("posts::details_reponse: Error retrieving previous comment").to_json(&*conn, &vec![])));
Template::render("posts/details", json!({ Template::render("posts/details", json!({
"author": post.get_authors(&*conn)[0].to_json(&*conn), "author": post.get_authors(&*conn)[0].to_json(&*conn),
"article": post.to_json(&*conn), "article": post.to_json(&*conn),
@ -59,11 +61,15 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>
"has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false), "has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false),
"account": &user.clone().map(|u| u.to_json(&*conn)), "account": &user.clone().map(|u| u.to_json(&*conn)),
"date": &post.creation_date.timestamp(), "date": &post.creation_date.timestamp(),
"previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r) "previous": previous,
.expect("posts::details_reponse: Error retrieving previous comment").to_json(&*conn, &vec![]))), "default": {
"warning": previous.map(|p| p["spoiler_text"].clone())
},
"user_fqn": user.clone().map(|u| u.get_fqn(&*conn)).unwrap_or(String::new()), "user_fqn": user.clone().map(|u| u.get_fqn(&*conn)).unwrap_or(String::new()),
"is_author": user.clone().map(|u| post.get_authors(&*conn).into_iter().any(|a| u.id == a.id)).unwrap_or(false), "is_author": user.clone().map(|u| post.get_authors(&*conn).into_iter().any(|a| u.id == a.id)).unwrap_or(false),
"is_following": user.map(|u| u.is_following(&*conn, post.get_authors(&*conn)[0].id)).unwrap_or(false) "is_following": user.map(|u| u.is_following(&*conn, post.get_authors(&*conn)[0].id)).unwrap_or(false),
"comment_form": null,
"comment_errors": null,
})) }))
} else { } else {
Template::render("errors/403", json!({ Template::render("errors/403", json!({

View File

@ -389,6 +389,10 @@ main .article-meta .tags li a {
font-weight: 600; font-weight: 600;
} }
summary {
cursor: pointer;
}
/* New comment */ /* New comment */
main .article-meta .comments form input[type="submit"] main .article-meta .comments form input[type="submit"]

View File

@ -60,7 +60,16 @@
<span class="display-name">{{ comm.author.name }}</span> <span class="display-name">{{ comm.author.name }}</span>
<small>@{{ comm.author.fqn }}</small> <small>@{{ comm.author.fqn }}</small>
</a> </a>
<div class="text">{{ comm.content | safe }}</div> <div class="text">
{% if comm.sensitive %}
<details>
<summary>{{ comm.spoiler_text }}</summary>
{% endif %}
{{ comm.content | safe }}
{% if comm.sensitive %}
</details>
{% endif %}
</div>
<a class="button icon icon-message-circle" href="?responding_to={{ comm.id }}">{{ "Respond" | _ }}</a> <a class="button icon icon-message-circle" href="?responding_to={{ comm.id }}">{{ "Respond" | _ }}</a>
{% for res in comm.responses %} {% for res in comm.responses %}
{{ self::comment(comm=res) }} {{ self::comment(comm=res) }}

View File

@ -119,6 +119,15 @@
{% if account %} {% if account %}
<form method="post" action="{{ article.url }}comment"> <form method="post" action="{{ article.url }}comment">
{{ macros::input(
name="warning",
label="Content warning",
optional=true,
form=comment_form,
errors=comment_errors,
default=default)
}}
<label for="plume-editor">{{ "Your comment" | _ }}</label> <label for="plume-editor">{{ "Your comment" | _ }}</label>
{% if previous %} {% if previous %}
<input type="hidden" name="responding_to" value="{{ previous.id }}"/> <input type="hidden" name="responding_to" value="{{ previous.id }}"/>