Add "View all comments" and "Show parent comments" buttons when viewing a single thread. Closes #65 (#115)

* Start recursive comments

* Update comment.html

* Fix move error

* Comment improvements

* Fix merge

* Remove extra endif from post.html

* Fix post.html

Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
This commit is contained in:
robrobinbin 2021-02-12 18:16:59 +01:00 committed by GitHub
parent 58ca085521
commit 809be42e01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 11 deletions

View File

@ -157,12 +157,12 @@ async fn main() -> tide::Result<()> {
// Browse user profile // Browse user profile
app.at("/u/:name/").get(user::profile); app.at("/u/:name/").get(user::profile);
app.at("/u/:name/comments/:id/:title/").get(post::item); app.at("/u/:name/comments/:id/:title/").get(post::item);
app.at("/u/:name/comments/:id/:title/:comment/").get(post::item); app.at("/u/:name/comments/:id/:title/:comment_id/").get(post::item);
app.at("/user/:name/").get(user::profile); app.at("/user/:name/").get(user::profile);
app.at("/user/:name/comments/:id/").get(post::item); app.at("/user/:name/comments/:id/").get(post::item);
app.at("/user/:name/comments/:id/:title/").get(post::item); app.at("/user/:name/comments/:id/:title/").get(post::item);
app.at("/user/:name/comments/:id/:title/:comment/").get(post::item); app.at("/user/:name/comments/:id/:title/:comment_id/").get(post::item);
// Configure settings // Configure settings
app.at("/settings/").get(settings::get).post(settings::set); app.at("/settings/").get(settings::get).post(settings::set);

View File

@ -14,6 +14,7 @@ struct PostTemplate {
post: Post, post: Post,
sort: String, sort: String,
prefs: Preferences, prefs: Preferences,
single_thread: bool,
} }
pub async fn item(req: Request<()>) -> tide::Result { pub async fn item(req: Request<()>) -> tide::Result {
@ -41,13 +42,16 @@ pub async fn item(req: Request<()>) -> tide::Result {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
dbg!(req.param("id").unwrap_or("")); dbg!(req.param("id").unwrap_or(""));
let single_thread = &req.param("comment_id").is_ok();
let highlighted_comment = &req.param("comment_id").unwrap_or_default();
// Send a request to the url, receive JSON in response // Send a request to the url, receive JSON in response
match request(path).await { match request(path).await {
// Otherwise, grab the JSON output from the request // Otherwise, grab the JSON output from the request
Ok(res) => { Ok(res) => {
// Parse the JSON into Post and Comment structs // Parse the JSON into Post and Comment structs
let post = parse_post(&res[0]).await; let post = parse_post(&res[0]).await;
let comments = parse_comments(&res[1], &post.permalink, &post.author.name).await; let comments = parse_comments(&res[1], &post.permalink, &post.author.name, *highlighted_comment).await;
// Use the Post and Comment structs to generate a website to show users // Use the Post and Comment structs to generate a website to show users
template(PostTemplate { template(PostTemplate {
@ -55,6 +59,7 @@ pub async fn item(req: Request<()>) -> tide::Result {
post, post,
sort, sort,
prefs: prefs(req), prefs: prefs(req),
single_thread: *single_thread,
}) })
} }
// If the Reddit API returns an error, exit and send error page to user // If the Reddit API returns an error, exit and send error page to user
@ -133,7 +138,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
// COMMENTS // COMMENTS
#[async_recursion] #[async_recursion]
async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str) -> Vec<Comment> { async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str) -> Vec<Comment> {
// Separate the comment JSON into a Vector of comments // Separate the comment JSON into a Vector of comments
let comment_data = match json["data"]["children"].as_array() { let comment_data = match json["data"]["children"].as_array() {
Some(f) => f.to_owned(), Some(f) => f.to_owned(),
@ -151,14 +156,22 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
let body = rewrite_urls(&val(&comment, "body_html")); let body = rewrite_urls(&val(&comment, "body_html"));
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() { let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
parse_comments(&comment["data"]["replies"], post_link, post_author).await parse_comments(&comment["data"]["replies"], post_link, post_author, highlighted_comment).await
} else { } else {
Vec::new() Vec::new()
}; };
let parent_kind_and_id = val(&comment, "parent_id");
let parent_info = parent_kind_and_id.split("_").collect::<Vec<&str>>();
let id = val(&comment, "id");
let highlighted = id == highlighted_comment;
comments.push(Comment { comments.push(Comment {
id: val(&comment, "id"), id,
kind: comment["kind"].as_str().unwrap_or_default().to_string(), kind: comment["kind"].as_str().unwrap_or_default().to_string(),
parent_id: parent_info[1].to_string(),
parent_kind: parent_info[0].to_string(),
post_link: post_link.to_string(), post_link: post_link.to_string(),
post_author: post_author.to_string(), post_author: post_author.to_string(),
body, body,
@ -183,6 +196,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
rel_time, rel_time,
created, created,
replies, replies,
highlighted,
}); });
} }

View File

@ -82,6 +82,8 @@ pub struct Post {
pub struct Comment { pub struct Comment {
pub id: String, pub id: String,
pub kind: String, pub kind: String,
pub parent_id: String,
pub parent_kind: String,
pub post_link: String, pub post_link: String,
pub post_author: String, pub post_author: String,
pub body: String, pub body: String,
@ -90,6 +92,7 @@ pub struct Comment {
pub rel_time: String, pub rel_time: String,
pub created: String, pub created: String,
pub replies: Vec<Comment>, pub replies: Vec<Comment>,
pub highlighted: bool,
} }
#[derive(Default)] #[derive(Default)]

View File

@ -556,6 +556,12 @@ a.search_subreddit:hover {
word-break: break-word; word-break: break-word;
} }
.thread_nav {
color: var(--accent);
font-weight: bold;
margin: 10px 0;
}
.post { .post {
border-radius: 5px; border-radius: 5px;
background: var(--post); background: var(--post);
@ -845,7 +851,12 @@ a.search_subreddit:hover {
.comment_body { .comment_body {
opacity: 0.9; opacity: 0.9;
font-weight: normal; font-weight: normal;
margin: 10px 5px; padding: 5px 5px;
margin: 5px 0;
}
.comment_body.highlighted {
background: var(--highlighted);
} }
.comment_body > p:not(:first-child) { .comment_body > p:not(:first-child) {

View File

@ -1,7 +1,7 @@
{% import "utils.html" as utils %} {% import "utils.html" as utils %}
{% if kind == "more" %} {% if kind == "more" && parent_kind == "t1" %}
<a class="deeper_replies" href="{{ post_link }}{{ id }}">&rarr; More replies</a> <a class="deeper_replies" href="{{ post_link }}{{ parent_id }}">&rarr; More replies</a>
{% else if kind == "t1" %} {% else if kind == "t1" %}
<div id="{{ id }}" class="comment"> <div id="{{ id }}" class="comment">
<div class="comment_left"> <div class="comment_left">
@ -16,7 +16,7 @@
{% endif %} {% endif %}
<span class="created" title="{{ created }}">{{ rel_time }}</span> <span class="created" title="{{ created }}">{{ rel_time }}</span>
</summary> </summary>
<div class="comment_body">{{ body }}</div> <div class="comment_body {% if highlighted %}highlighted{% endif %}">{{ body }}</div>
<blockquote class="replies">{% for c in replies -%}{{ c.render().unwrap() }}{%- endfor %} <blockquote class="replies">{% for c in replies -%}{{ c.render().unwrap() }}{%- endfor %}
</blockquote> </blockquote>
</details> </details>

View File

@ -101,6 +101,13 @@
<!-- COMMENTS --> <!-- COMMENTS -->
{% for c in comments -%} {% for c in comments -%}
<div class="thread"> <div class="thread">
{% if single_thread %}
<p class="thread_nav"><a href="/{{ post.id }}">View all comments</a></p>
{% if c.parent_kind == "t1" %}
<p class="thread_nav"><a href="?context=9999">Show parent comments</a></p>
{% endif %}
{% endif %}
{{ c.render().unwrap() }} {{ c.render().unwrap() }}
</div> </div>
{%- endfor %} {%- endfor %}