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:
parent
58ca085521
commit
809be42e01
6 changed files with 46 additions and 11 deletions
|
@ -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);
|
||||
|
|
22
src/post.rs
22
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 {
|
||||
|
@ -41,13 +42,16 @@ pub async fn item(req: Request<()>) -> tide::Result {
|
|||
#[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 {
|
||||
// Otherwise, grab the JSON output from the request
|
||||
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<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
|
||||
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<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 {
|
||||
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 {
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Comment>,
|
||||
pub highlighted: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{% import "utils.html" as utils %}
|
||||
|
||||
{% if kind == "more" %}
|
||||
<a class="deeper_replies" href="{{ post_link }}{{ id }}">→ More replies</a>
|
||||
{% if kind == "more" && parent_kind == "t1" %}
|
||||
<a class="deeper_replies" href="{{ post_link }}{{ parent_id }}">→ More replies</a>
|
||||
{% else if kind == "t1" %}
|
||||
<div id="{{ id }}" class="comment">
|
||||
<div class="comment_left">
|
||||
|
@ -16,7 +16,7 @@
|
|||
{% endif %}
|
||||
<span class="created" title="{{ created }}">{{ rel_time }}</span>
|
||||
</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>
|
||||
</details>
|
||||
|
|
|
@ -101,6 +101,13 @@
|
|||
<!-- COMMENTS -->
|
||||
{% for c in comments -%}
|
||||
<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() }}
|
||||
</div>
|
||||
{%- endfor %}
|
||||
|
|
Loading…
Reference in a new issue