2021-01-01 12:54:13 +13:00
|
|
|
// CRATES
|
2021-01-01 18:03:44 +13:00
|
|
|
use crate::utils::{error, fetch_posts, param, Post};
|
|
|
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
2021-01-01 12:54:13 +13:00
|
|
|
use askama::Template;
|
|
|
|
|
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[template(path = "search.html", escape = "none")]
|
|
|
|
struct SearchTemplate {
|
|
|
|
posts: Vec<Post>,
|
|
|
|
query: String,
|
|
|
|
sub: String,
|
|
|
|
sort: (String, String),
|
|
|
|
ends: (String, String),
|
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
2021-01-01 18:03:44 +13:00
|
|
|
pub async fn find(req: HttpRequest) -> Result<HttpResponse> {
|
2021-01-01 12:54:13 +13:00
|
|
|
let path = format!("{}.json?{}", req.path(), req.query_string());
|
2021-01-02 09:33:57 +13:00
|
|
|
let q = param(&path, "q");
|
|
|
|
let sort = if param(&path, "sort").is_empty() {
|
2021-01-01 12:54:13 +13:00
|
|
|
"relevance".to_string()
|
|
|
|
} else {
|
2021-01-02 09:33:57 +13:00
|
|
|
param(&path, "sort")
|
2021-01-01 12:54:13 +13:00
|
|
|
};
|
|
|
|
let sub = req.match_info().get("sub").unwrap_or("").to_string();
|
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
match fetch_posts(&path, String::new()).await {
|
|
|
|
Ok(posts) => {
|
|
|
|
let s = SearchTemplate {
|
|
|
|
posts: posts.0,
|
|
|
|
query: q,
|
|
|
|
sub,
|
|
|
|
sort: (sort, param(&path, "t")),
|
|
|
|
ends: (param(&path, "after"), posts.1),
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
},
|
|
|
|
Err(msg) => error(msg.to_string()).await
|
2021-01-01 12:54:13 +13:00
|
|
|
}
|
|
|
|
}
|