Rewrite + Searching

This commit is contained in:
spikecodes 2020-12-31 15:54:13 -08:00
parent c7282520cd
commit a6dc7ee043
17 changed files with 342 additions and 262 deletions

View file

@ -1,6 +1,6 @@
// CRATES
use crate::utils::{fetch_posts, format_url, nested_val, request, ErrorTemplate, Params, Post, User};
use actix_web::{http::StatusCode, web, HttpResponse, Result};
use crate::utils::{fetch_posts, format_url, nested_val, param, request, ErrorTemplate, Post, User};
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
use askama::Template;
use chrono::{TimeZone, Utc};
@ -14,25 +14,19 @@ struct UserTemplate {
ends: (String, String),
}
async fn render(username: String, sort: Option<String>, t: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
let sorting = sort.unwrap_or("new".to_string());
pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// Build the Reddit JSON API path
let path = format!("{}.json?{}&raw_json=1", req.path(), req.query_string());
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() };
// Build the Reddit JSON API url
let url = match ends.0 {
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
None => match ends.1 {
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
None => format!("user/{}/.json?sort={}&raw_json=1{}", username, sorting, timeframe),
},
};
// Retrieve other variables from Libreddit request
let sort = param(&path, "sort").await;
let username = req.match_info().get("username").unwrap_or("").to_string();
// Request user profile data and user posts/comments from Reddit
let user = user(&username).await;
let posts = fetch_posts(url, "Comment".to_string()).await;
let posts = fetch_posts(path.clone(), "Comment".to_string()).await;
// If there is an error show error page
if user.is_err() || posts.is_err() {
let s = ErrorTemplate {
message: user.err().unwrap().to_string(),
@ -42,12 +36,12 @@ async fn render(username: String, sort: Option<String>, t: Option<String>, ends:
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
} else {
let posts_unwrapped = posts.unwrap();
let s = UserTemplate {
user: user.unwrap(),
posts: posts_unwrapped.0,
sort: (sorting, t.unwrap_or(String::new())),
ends: (before, posts_unwrapped.1)
sort: (sort, param(&path, "t").await),
ends: (param(&path, "after").await, posts_unwrapped.1),
}
.render()
.unwrap();
@ -56,9 +50,9 @@ async fn render(username: String, sort: Option<String>, t: Option<String>, ends:
}
// SERVICES
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
}
// pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
// render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
// }
// USER
async fn user(name: &String) -> Result<User, &'static str> {