From 05ea0fe1fd6ee023934a5021a2d34edb2b978cd2 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 17 Nov 2020 16:03:28 -0800 Subject: [PATCH] Fixed Sorting --- src/main.rs | 3 --- src/popular.rs | 10 ++++------ src/post.rs | 14 ++++++-------- src/subreddit.rs | 19 ++++++++++--------- src/user.rs | 14 ++++++-------- src/utils.rs | 9 +++++++++ templates/popular.html | 6 +++--- templates/post.html | 10 +++++----- templates/subreddit.html | 6 +++--- templates/user.html | 6 +++--- 10 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9e969bb..21ade6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,13 +32,10 @@ async fn main() -> std::io::Result<()> { // POST SERVICES .service(post::short) .service(post::page) - .service(post::sorted) // SUBREDDIT SERVICES .service(subreddit::page) - .service(subreddit::sorted) // POPULAR SERVICES .service(popular::page) - // .service(popular::sorted) // USER SERVICES .service(user::page) }) diff --git a/src/popular.rs b/src/popular.rs index fc882d1..a5d1991 100644 --- a/src/popular.rs +++ b/src/popular.rs @@ -1,12 +1,15 @@ // CRATES use actix_web::{get, web, HttpResponse, Result}; use askama::Template; -use serde::Deserialize; #[path = "subreddit.rs"] mod subreddit; use subreddit::{posts, Post}; +#[path = "utils.rs"] +mod utils; +use utils::{Params}; + // STRUCTS #[derive(Template)] #[template(path = "popular.html", escape = "none")] @@ -15,11 +18,6 @@ struct PopularTemplate { sort: String, } -#[derive(Deserialize)] -pub struct Params { - sort: Option, -} - // RENDER async fn render(sub_name: String, sort: String) -> Result { let posts: Vec = posts(sub_name, &sort).await; diff --git a/src/post.rs b/src/post.rs index 5abb9a1..9e2cfd0 100644 --- a/src/post.rs +++ b/src/post.rs @@ -6,7 +6,7 @@ use pulldown_cmark::{html, Options, Parser}; #[path = "utils.rs"] mod utils; -use utils::{Comment, Flair, Post, val}; +use utils::{Params, Comment, Flair, Post, val}; // STRUCTS #[derive(Template)] @@ -42,13 +42,11 @@ async fn short(web::Path(id): web::Path) -> Result { } #[get("/r/{sub}/comments/{id}/{title}/")] -async fn page(web::Path((_sub, id)): web::Path<(String, String)>) -> Result { - render(id.to_string(), "confidence".to_string()).await -} - -#[get("/r/{sub}/comments/{id}/{title}/{sort}")] -async fn sorted(web::Path((_sub, id, _title, sort)): web::Path<(String, String, String, String)>) -> Result { - render(id.to_string(), sort).await +async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query) -> Result { + match ¶ms.sort { + Some(sort) => render(id, sort.to_string()).await, + None => render(id, "confidence".to_string()).await, + } } // UTILITIES diff --git a/src/subreddit.rs b/src/subreddit.rs index 243ea08..190c066 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -5,7 +5,7 @@ use chrono::{TimeZone, Utc}; #[path = "utils.rs"] mod utils; -pub use utils::{Flair, Post, Subreddit, val}; +pub use utils::{Params, Flair, Post, Subreddit, val}; // STRUCTS #[derive(Template)] @@ -33,21 +33,20 @@ async fn render(sub_name: String, sort: String) -> Result { // SERVICES #[allow(dead_code)] #[get("/r/{sub}")] -async fn page(web::Path(sub): web::Path) -> Result { - render(sub, String::from("hot")).await -} - -#[allow(dead_code)] -#[get("/r/{sub}/{sort}")] -async fn sorted(web::Path((sub, sort)): web::Path<(String, String)>) -> Result { - render(sub, sort).await +async fn page(web::Path(sub): web::Path, params: web::Query) -> Result { + match ¶ms.sort { + Some(sort) => render(sub, sort.to_string()).await, + None => render(sub, "hot".to_string()).await, + } } // SUBREDDIT async fn subreddit(sub: &String) -> Subreddit { + // Make a GET request to the Reddit's JSON API for the metadata of this subreddit let url: String = format!("https://www.reddit.com/r/{}/about.json", sub); let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap(); + // Parse the response from Reddit as JSON let data: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON"); let icon: String = String::from(data["data"]["community_icon"].as_str().unwrap()); //val(&data, "community_icon"); @@ -64,9 +63,11 @@ async fn subreddit(sub: &String) -> Subreddit { // POSTS pub async fn posts(sub: String, sort: &String) -> Vec { + // Make a GET request to the Reddit's JSON API for the content of this subreddit let url: String = format!("https://www.reddit.com/r/{}/{}.json", sub, sort); let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap(); + // Parse the response from Reddit as JSON let popular: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON"); let post_list = popular["data"]["children"].as_array().unwrap(); diff --git a/src/user.rs b/src/user.rs index a41eaca..890d441 100644 --- a/src/user.rs +++ b/src/user.rs @@ -5,7 +5,7 @@ use chrono::{TimeZone, Utc}; #[path = "utils.rs"] mod utils; -use utils::{Flair, Post, User, val, nested_val}; +use utils::{Params, Flair, Post, User, val, nested_val}; // STRUCTS #[derive(Template)] @@ -26,13 +26,11 @@ async fn render(username: String, sort: String) -> Result { // SERVICES #[get("/u/{username}")] -async fn page(web::Path(username): web::Path) -> Result { - render(username, "hot".to_string()).await -} - -#[get("/u/{username}/{sort}")] -async fn sorted(web::Path((username, sort)): web::Path<(String, String)>) -> Result { - render(username, sort).await +async fn page(web::Path(username): web::Path, params: web::Query) -> Result { + match ¶ms.sort { + Some(sort) => render(username, sort.to_string()).await, + None => render(username, "hot".to_string()).await, + } } // USER diff --git a/src/utils.rs b/src/utils.rs index 9791a71..4ab9634 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,8 @@ +#[allow(dead_code)] // Post flair with text, background color and foreground color pub struct Flair(pub String, pub String, pub String); +#[allow(dead_code)] // Post containing content, metadata and media pub struct Post { pub title: String, @@ -42,6 +44,7 @@ pub struct Subreddit { pub icon: String, } +#[allow(dead_code)] // val() function used to parse JSON from Reddit APIs pub async fn val(j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("")) @@ -51,4 +54,10 @@ pub async fn val(j: &serde_json::Value, k: &str) -> String { // nested_val() function used to parse JSON from Reddit APIs pub async fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String { String::from(j["data"][n][k].as_str().unwrap()) +} + +// Parser for query params, used in sorting (eg. /r/rust/?sort=hot) +#[derive(serde::Deserialize)] +pub struct Params { + pub sort: Option, } \ No newline at end of file diff --git a/templates/popular.html b/templates/popular.html index 5afedcd..ac0b293 100644 --- a/templates/popular.html +++ b/templates/popular.html @@ -18,9 +18,9 @@
- - - + + +
{% for post in posts %}
diff --git a/templates/post.html b/templates/post.html index 85ae836..06398bb 100644 --- a/templates/post.html +++ b/templates/post.html @@ -43,11 +43,11 @@
{% for comment in comments %}
diff --git a/templates/subreddit.html b/templates/subreddit.html index 0213f4e..b852e7f 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -29,9 +29,9 @@
- - - + + +
{% for post in posts %}
diff --git a/templates/user.html b/templates/user.html index 1c7bf37..cb40237 100644 --- a/templates/user.html +++ b/templates/user.html @@ -29,9 +29,9 @@
- - - + + +
{% for post in posts %}