From 809be42e01a7f32ba341127c26149c39d86c89c6 Mon Sep 17 00:00:00 2001 From: robrobinbin <8597693+robrobinbin@users.noreply.github.com> Date: Fri, 12 Feb 2021 18:16:59 +0100 Subject: [PATCH] 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> --- src/main.rs | 4 ++-- src/post.rs | 24 +++++++++++++++++++----- src/utils.rs | 3 +++ static/style.css | 13 ++++++++++++- templates/comment.html | 6 +++--- templates/post.html | 7 +++++++ 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1d2f0c3..6a22083 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,12 +157,12 @@ async fn main() -> tide::Result<()> { // Browse 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/: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/comments/:id/").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 app.at("/settings/").get(settings::get).post(settings::set); diff --git a/src/post.rs b/src/post.rs index 25deb03..991b508 100644 --- a/src/post.rs +++ b/src/post.rs @@ -14,6 +14,7 @@ struct PostTemplate { post: Post, sort: String, prefs: Preferences, + single_thread: bool, } pub async fn item(req: Request<()>) -> tide::Result { @@ -40,6 +41,9 @@ pub async fn item(req: Request<()>) -> tide::Result { // Log the post ID being fetched in debug mode #[cfg(debug_assertions)] 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 match request(path).await { @@ -47,7 +51,7 @@ pub async fn item(req: Request<()>) -> tide::Result { Ok(res) => { // Parse the JSON into Post and Comment structs 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 template(PostTemplate { @@ -55,6 +59,7 @@ pub async fn item(req: Request<()>) -> tide::Result { post, sort, prefs: prefs(req), + single_thread: *single_thread, }) } // 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 #[async_recursion] -async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str) -> Vec { +async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str) -> Vec { // Separate the comment JSON into a Vector of comments let comment_data = match json["data"]["children"].as_array() { 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 replies: Vec = 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 { Vec::new() }; - + + let parent_kind_and_id = val(&comment, "parent_id"); + let parent_info = parent_kind_and_id.split("_").collect::>(); + + let id = val(&comment, "id"); + let highlighted = id == highlighted_comment; + comments.push(Comment { - id: val(&comment, "id"), + id, 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_author: post_author.to_string(), body, @@ -183,6 +196,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: rel_time, created, replies, + highlighted, }); } diff --git a/src/utils.rs b/src/utils.rs index f6398c9..ded70fb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -82,6 +82,8 @@ pub struct Post { pub struct Comment { pub id: String, pub kind: String, + pub parent_id: String, + pub parent_kind: String, pub post_link: String, pub post_author: String, pub body: String, @@ -90,6 +92,7 @@ pub struct Comment { pub rel_time: String, pub created: String, pub replies: Vec, + pub highlighted: bool, } #[derive(Default)] diff --git a/static/style.css b/static/style.css index 38702d3..5094889 100644 --- a/static/style.css +++ b/static/style.css @@ -556,6 +556,12 @@ a.search_subreddit:hover { word-break: break-word; } +.thread_nav { + color: var(--accent); + font-weight: bold; + margin: 10px 0; +} + .post { border-radius: 5px; background: var(--post); @@ -845,7 +851,12 @@ a.search_subreddit:hover { .comment_body { opacity: 0.9; font-weight: normal; - margin: 10px 5px; + padding: 5px 5px; + margin: 5px 0; +} + +.comment_body.highlighted { + background: var(--highlighted); } .comment_body > p:not(:first-child) { diff --git a/templates/comment.html b/templates/comment.html index f2bfc05..33db187 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,7 +1,7 @@ {% import "utils.html" as utils %} -{% if kind == "more" %} -→ More replies +{% if kind == "more" && parent_kind == "t1" %} +→ More replies {% else if kind == "t1" %}
@@ -16,7 +16,7 @@ {% endif %} {{ rel_time }} -
{{ body }}
+
{{ body }}
{% for c in replies -%}{{ c.render().unwrap() }}{%- endfor %}
diff --git a/templates/post.html b/templates/post.html index 7bcced6..6a99205 100644 --- a/templates/post.html +++ b/templates/post.html @@ -101,6 +101,13 @@ {% for c in comments -%}
+ {% if single_thread %} +

View all comments

+ {% if c.parent_kind == "t1" %} +

Show parent comments

+ {% endif %} + {% endif %} + {{ c.render().unwrap() }}
{%- endfor %}