More Replies Button
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user