Fix search pages

This commit is contained in:
spikecodes
2021-01-02 22:37:54 -08:00
parent 5d26b5c764
commit a3384cbaa6
3 changed files with 33 additions and 18 deletions

View File

@ -4,20 +4,26 @@ use actix_web::{HttpRequest, HttpResponse};
use askama::Template;
// STRUCTS
struct SearchParams {
q: String,
sort: String,
t: String,
before: String,
after: String,
restrict_sr: String,
}
#[derive(Template)]
#[template(path = "search.html", escape = "none")]
struct SearchTemplate {
posts: Vec<Post>,
query: String,
sub: String,
sort: (String, String),
ends: (String, String),
params: SearchParams,
}
// SERVICES
pub async fn find(req: HttpRequest) -> HttpResponse {
let path = format!("{}.json?{}", req.path(), req.query_string());
let q = param(&path, "q");
let sort = if param(&path, "sort").is_empty() {
"relevance".to_string()
} else {
@ -29,10 +35,15 @@ pub async fn find(req: HttpRequest) -> HttpResponse {
Ok(posts) => HttpResponse::Ok().content_type("text/html").body(
SearchTemplate {
posts: posts.0,
query: q,
sub,
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), posts.1),
params: SearchParams {
q: param(&path, "q"),
sort,
t: param(&path, "t"),
before: param(&path, "after"),
after: posts.1,
restrict_sr: param(&path, "restrict_sr"),
},
}
.render()
.unwrap(),

View File

@ -97,7 +97,7 @@ pub struct ErrorTemplate {
// Grab a query param from a url
pub fn param(path: &str, value: &str) -> String {
let url = Url::parse(format!("https://reddit.com/{}", path).as_str()).unwrap();
let url = Url::parse(format!("https://libredd.it/{}", path).as_str()).unwrap();
let pairs: std::collections::HashMap<_, _> = url.query_pairs().into_owned().collect();
pairs.get(value).unwrap_or(&String::new()).to_owned()
}