More Replies Button

This commit is contained in:
spikecodes 2020-12-21 17:17:40 -08:00
parent 4b7cbb3de2
commit 11cfbdc3ed
11 changed files with 40 additions and 21 deletions

View File

@ -60,6 +60,7 @@ async fn main() -> std::io::Result<()> {
// POST SERVICES // POST SERVICES
.route("/{id:.{5,6}}/", web::get().to(post::short)) .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}/", web::get().to(post::page))
.route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::comment))
}) })
.bind(address.clone()) .bind(address.clone())
.expect(format!("Cannot bind to the address: {}", address).as_str()) .expect(format!("Cannot bind to the address: {}", address).as_str())

View File

@ -19,10 +19,10 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
// Build the Reddit JSON API url // Build the Reddit JSON API url
let url = match ends.0 { 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 { None => match ends.1 {
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?after={}&count=25", sub_name, sorting, val), Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
None => format!("https://www.reddit.com/r/{}/{}.json", sub_name, sorting), None => format!("r/{}/{}.json", sub_name, sorting),
}, },
}; };

View File

@ -17,7 +17,7 @@ struct PostTemplate {
sort: String, 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 // Log the post ID being fetched
dbg!(&id); 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()); let sorting: String = sort.unwrap_or("confidence".to_string());
// Build the Reddit JSON API url // 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 // Send a request to the url, receive JSON in response
let req = request(url).await; let req = request(url).await;
@ -60,11 +63,15 @@ async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
// SERVICES // SERVICES
pub async fn short(web::Path(id): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> { 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> { 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 // UTILITIES

View File

@ -26,10 +26,10 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
// Build the Reddit JSON API url // Build the Reddit JSON API url
let url = match ends.0 { 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 { None => match ends.1 {
Some(val) => format!("https://www.reddit.com/r/{}/{}.json?after={}&count=25", sub_name, sorting, val), Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
None => format!("https://www.reddit.com/r/{}/{}.json", sub_name, sorting), 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 // SUBREDDIT
async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> { async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
// Build the Reddit JSON API url // 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 // Send a request to the url, receive JSON in response
let req = request(url).await; let req = request(url).await;

View File

@ -14,7 +14,7 @@ struct UserTemplate {
async fn render(username: String, sort: String) -> Result<HttpResponse> { async fn render(username: String, sort: String) -> Result<HttpResponse> {
// Build the Reddit JSON API url // 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 user = user(&username).await;
let posts = fetch_posts(url, "Comment".to_string()).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 // USER
async fn user(name: &String) -> Result<User, &'static str> { async fn user(name: &String) -> Result<User, &'static str> {
// Build the Reddit JSON API url // 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 // Send a request to the url, receive JSON in response
let req = request(url).await; let req = request(url).await;

View File

@ -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 // Make a request to a Reddit API and parse the JSON response
#[allow(dead_code)] #[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 --- // --- actix-web::client ---
// let client = actix_web::client::Client::default(); // let client = actix_web::client::Client::default();
// let res = client // let res = client

View File

@ -284,7 +284,6 @@ a:not(.post_right):hover {
border-radius: 5px; border-radius: 5px;
display: flex; display: flex;
font-size: 15px; font-size: 15px;
padding: 5px;
} }
.comment_left, .comment_right { .comment_left, .comment_right {
@ -377,12 +376,18 @@ a:not(.post_right):hover {
color: aqua; color: aqua;
} }
.deeper_replies {
color: aqua;
margin-left: 1em;
}
::marker { ::marker {
color: aqua; color: aqua;
} }
.replies > .comment { .replies > .comment {
margin-left: -20px; margin-left: -20px;
padding: 5px;
} }
.datetime { .datetime {
@ -456,7 +461,7 @@ td, th {
.replies > .comment { .replies > .comment {
margin-left: -25px; margin-left: -25px;
padding: 10px 10px 10px 5px; padding: 5px 0px;
} }
.datetime { .datetime {

View File

@ -6,7 +6,7 @@
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option> <option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option> <option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</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="&rarr;">
</form> </form>
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">

View File

@ -61,7 +61,7 @@
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option> <option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="controversial" {% if sort == "controversial" %}selected{% endif %}>Controversial</option> <option value="controversial" {% if sort == "controversial" %}selected{% endif %}>Controversial</option>
<option value="old" {% if sort == "old" %}selected{% endif %}>Old</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="&rarr;">
</form> </form>
{% for c in comments -%} {% for c in comments -%}
@ -75,7 +75,11 @@
{% call comment(reply2) %} {% call comment(reply2) %}
<div class="replies"> <div class="replies">
{% for reply3 in reply2.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 }}">&rarr; More replies</a>
{% endif %}
</details></div>
{% endfor %} {% endfor %}
</div></details></div> </div></details></div>
{% endfor %} {% endfor %}

View File

@ -25,7 +25,7 @@
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option> <option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option> <option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</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="&rarr;">
</form> </form>
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">

View File

@ -18,7 +18,7 @@
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option> <option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option> <option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</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="&rarr;">
</form> </form>
{% for post in posts %} {% for post in posts %}
{% if post.title != "Comment" %} {% if post.title != "Comment" %}