More Replies Button
This commit is contained in:
parent
4b7cbb3de2
commit
11cfbdc3ed
11 changed files with 40 additions and 21 deletions
|
@ -60,6 +60,7 @@ async fn main() -> std::io::Result<()> {
|
|||
// POST SERVICES
|
||||
.route("/{id:.{5,6}}/", web::get().to(post::short))
|
||||
.route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::page))
|
||||
.route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::comment))
|
||||
})
|
||||
.bind(address.clone())
|
||||
.expect(format!("Cannot bind to the address: {}", address).as_str())
|
||||
|
|
|
@ -19,10 +19,10 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
|
|||
|
||||
// Build the Reddit JSON API url
|
||||
let url = match ends.0 {
|
||||
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
|
||||
Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
|
||||
None => match ends.1 {
|
||||
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
|
||||
None => format!("https://www.reddit.com/r/{}/{}.json", sub_name, sorting),
|
||||
Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
|
||||
None => format!("r/{}/{}.json", sub_name, sorting),
|
||||
},
|
||||
};
|
||||
|
||||
|
|
15
src/post.rs
15
src/post.rs
|
@ -17,7 +17,7 @@ struct PostTemplate {
|
|||
sort: String,
|
||||
}
|
||||
|
||||
async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
|
||||
async fn render(id: String, sort: Option<String>, comment_id: Option<String>) -> Result<HttpResponse> {
|
||||
// Log the post ID being fetched
|
||||
dbg!(&id);
|
||||
|
||||
|
@ -25,7 +25,10 @@ async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
|
|||
let sorting: String = sort.unwrap_or("confidence".to_string());
|
||||
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sorting);
|
||||
let url: String = match comment_id {
|
||||
None => format!("{}.json?sort={}", id, sorting),
|
||||
Some(val) => format!("{}.json?sort={}&comment={}", id, sorting, val)
|
||||
};
|
||||
|
||||
// Send a request to the url, receive JSON in response
|
||||
let req = request(url).await;
|
||||
|
@ -60,11 +63,15 @@ async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
|
|||
|
||||
// SERVICES
|
||||
pub async fn short(web::Path(id): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
||||
render(id, params.sort.clone()).await
|
||||
render(id, params.sort.clone(), None).await
|
||||
}
|
||||
|
||||
pub async fn comment(web::Path((_sub, id, _title, comment_id)): web::Path<(String, String, String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
|
||||
render(id, params.sort.clone(), Some(comment_id)).await
|
||||
}
|
||||
|
||||
pub async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
|
||||
render(id, params.sort.clone()).await
|
||||
render(id, params.sort.clone(), None).await
|
||||
}
|
||||
|
||||
// UTILITIES
|
||||
|
|
|
@ -26,10 +26,10 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
|||
|
||||
// Build the Reddit JSON API url
|
||||
let url = match ends.0 {
|
||||
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
|
||||
Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
|
||||
None => match ends.1 {
|
||||
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
|
||||
None => format!("https://www.reddit.com/r/{}/{}.json", sub_name, sorting),
|
||||
Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
|
||||
None => format!("r/{}/{}.json", sub_name, sorting),
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -79,7 +79,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
|||
// SUBREDDIT
|
||||
async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = format!("https://www.reddit.com/r/{}/about.json", sub);
|
||||
let url: String = format!("r/{}/about.json", sub);
|
||||
|
||||
// Send a request to the url, receive JSON in response
|
||||
let req = request(url).await;
|
||||
|
|
|
@ -14,7 +14,7 @@ struct UserTemplate {
|
|||
|
||||
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = format!("https://www.reddit.com/user/{}/.json?sort={}", username, sort);
|
||||
let url: String = format!("user/{}/.json?sort={}", username, sort);
|
||||
|
||||
let user = user(&username).await;
|
||||
let posts = fetch_posts(url, "Comment".to_string()).await;
|
||||
|
@ -49,7 +49,7 @@ pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Par
|
|||
// USER
|
||||
async fn user(name: &String) -> Result<User, &'static str> {
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = format!("https://www.reddit.com/user/{}/about.json", name);
|
||||
let url: String = format!("user/{}/about.json", name);
|
||||
|
||||
// Send a request to the url, receive JSON in response
|
||||
let req = request(url).await;
|
||||
|
|
|
@ -181,7 +181,9 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
|
|||
|
||||
// Make a request to a Reddit API and parse the JSON response
|
||||
#[allow(dead_code)]
|
||||
pub async fn request(url: String) -> Result<serde_json::Value, &'static str> {
|
||||
pub async fn request(mut url: String) -> Result<serde_json::Value, &'static str> {
|
||||
url = format!("https://www.reddit.com/{}", url);
|
||||
|
||||
// --- actix-web::client ---
|
||||
// let client = actix_web::client::Client::default();
|
||||
// let res = client
|
||||
|
|
|
@ -284,7 +284,6 @@ a:not(.post_right):hover {
|
|||
border-radius: 5px;
|
||||
display: flex;
|
||||
font-size: 15px;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.comment_left, .comment_right {
|
||||
|
@ -377,12 +376,18 @@ a:not(.post_right):hover {
|
|||
color: aqua;
|
||||
}
|
||||
|
||||
.deeper_replies {
|
||||
color: aqua;
|
||||
margin-left: 1em;
|
||||
}
|
||||
|
||||
::marker {
|
||||
color: aqua;
|
||||
}
|
||||
|
||||
.replies > .comment {
|
||||
margin-left: -20px;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.datetime {
|
||||
|
@ -456,7 +461,7 @@ td, th {
|
|||
|
||||
.replies > .comment {
|
||||
margin-left: -25px;
|
||||
padding: 10px 10px 10px 5px;
|
||||
padding: 5px 0px;
|
||||
}
|
||||
|
||||
.datetime {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
|
||||
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
|
||||
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</form>
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
|
||||
<option value="controversial" {% if sort == "controversial" %}selected{% endif %}>Controversial</option>
|
||||
<option value="old" {% if sort == "old" %}selected{% endif %}>Old</option>
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</form>
|
||||
|
||||
{% for c in comments -%}
|
||||
|
@ -75,7 +75,11 @@
|
|||
{% call comment(reply2) %}
|
||||
<div class="replies">
|
||||
{% for reply3 in reply2.replies %}
|
||||
{% call comment(reply3) %}</details></div>
|
||||
{% call comment(reply3) %}
|
||||
{% if reply3.replies.len() > 0 %}
|
||||
<a class="deeper_replies" href="{{ post.url }}{{ reply3.id }}">→ More replies</a>
|
||||
{% endif %}
|
||||
</details></div>
|
||||
{% endfor %}
|
||||
</div></details></div>
|
||||
{% endfor %}
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
|
||||
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
|
||||
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</form>
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
|
||||
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
|
||||
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</select><input id="sort_submit" type="submit" value="→">
|
||||
</form>
|
||||
{% for post in posts %}
|
||||
{% if post.title != "Comment" %}
|
||||
|
|
Loading…
Reference in a new issue