redlib/src/search.rs

100 lines
2.4 KiB
Rust
Raw Normal View History

2020-12-31 15:54:13 -08:00
// CRATES
2021-01-20 18:26:58 -08:00
use crate::utils::{error, fetch_posts, param, prefs, request, val, Post, Preferences, cookie};
2021-01-02 20:50:23 -08:00
use actix_web::{HttpRequest, HttpResponse};
2020-12-31 15:54:13 -08:00
use askama::Template;
// STRUCTS
2021-01-02 22:37:54 -08:00
struct SearchParams {
q: String,
sort: String,
t: String,
before: String,
after: String,
restrict_sr: String,
}
2021-01-14 19:22:50 +01:00
// STRUCTS
struct Subreddit {
name: String,
url: String,
description: String,
subscribers: i64,
}
2020-12-31 15:54:13 -08:00
#[derive(Template)]
#[template(path = "search.html", escape = "none")]
struct SearchTemplate {
posts: Vec<Post>,
2021-01-14 19:22:50 +01:00
subreddits: Vec<Subreddit>,
2020-12-31 15:54:13 -08:00
sub: String,
2021-01-02 22:37:54 -08:00
params: SearchParams,
2021-01-08 17:35:04 -08:00
prefs: Preferences,
2020-12-31 15:54:13 -08:00
}
// SERVICES
2021-01-02 20:50:23 -08:00
pub async fn find(req: HttpRequest) -> HttpResponse {
2021-01-20 18:26:58 -08:00
let nsfw_results = if cookie(&req, "hide_nsfw") != "on" { "&include_over_18=on" } else { "" };
let path = format!("{}.json?{}{}", req.path(), req.query_string(), nsfw_results);
2021-01-14 11:45:04 -08:00
let sub = req.match_info().get("sub").unwrap_or("").to_string();
2021-01-01 12:33:57 -08:00
let sort = if param(&path, "sort").is_empty() {
2020-12-31 15:54:13 -08:00
"relevance".to_string()
} else {
2021-01-01 12:33:57 -08:00
param(&path, "sort")
2020-12-31 15:54:13 -08:00
};
2021-01-14 19:22:50 +01:00
2021-01-14 11:45:04 -08:00
let subreddits = if param(&path, "restrict_sr").is_empty() {
search_subreddits(param(&path, "q")).await
} else {
Vec::new()
};
2020-12-31 15:54:13 -08:00
2021-01-01 15:28:13 -08:00
match fetch_posts(&path, String::new()).await {
2021-01-06 21:27:24 -08:00
Ok((posts, after)) => HttpResponse::Ok().content_type("text/html").body(
2021-01-02 20:50:23 -08:00
SearchTemplate {
2021-01-06 21:27:24 -08:00
posts,
2021-01-14 19:22:50 +01:00
subreddits,
2021-01-01 15:28:13 -08:00
sub,
2021-01-02 22:37:54 -08:00
params: SearchParams {
q: param(&path, "q"),
sort,
t: param(&path, "t"),
before: param(&path, "after"),
2021-01-06 21:27:24 -08:00
after,
2021-01-02 22:37:54 -08:00
restrict_sr: param(&path, "restrict_sr"),
},
2021-01-08 17:35:04 -08:00
prefs: prefs(req),
2021-01-01 15:28:13 -08:00
}
.render()
2021-01-02 20:50:23 -08:00
.unwrap(),
),
Err(msg) => error(msg).await,
2020-12-31 15:54:13 -08:00
}
}
2021-01-14 11:45:04 -08:00
async fn search_subreddits(q: String) -> Vec<Subreddit> {
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+"));
// Send a request to the url
match request(&subreddit_search_path).await {
// If success, receive JSON in response
Ok(response) => {
match response["data"]["children"].as_array() {
// For each subreddit from subreddit list
Some(list) => list
.iter()
.map(|subreddit| Subreddit {
name: val(subreddit, "display_name_prefixed"),
url: val(subreddit, "url"),
description: val(subreddit, "public_description"),
subscribers: subreddit["data"]["subscribers"].as_u64().unwrap_or_default() as i64,
})
.collect::<Vec<Subreddit>>(),
_ => Vec::new(),
}
}
// If the Reddit API returns an error, exit this function
_ => Vec::new(),
}
}