diff --git a/src/main.rs b/src/main.rs index b5520c5..0c3870f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,12 @@ async fn favicon() -> HttpResponse { .body(include_bytes!("../static/favicon.ico").as_ref()) } +async fn thumbnail() -> HttpResponse { + HttpResponse::Ok() + .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") + .body(include_bytes!("../static/thumbnail.svg").as_ref()) +} + #[actix_web::main] async fn main() -> std::io::Result<()> { let mut address = "0.0.0.0:8080".to_string(); @@ -66,6 +72,7 @@ async fn main() -> std::io::Result<()> { // GENERAL SERVICES .route("/style.css/", web::get().to(style)) .route("/favicon.ico/", web::get().to(favicon)) + .route("/thumbnail.svg/", web::get().to(thumbnail)) .route("/robots.txt/", web::get().to(robots)) // SETTINGS SERVICE .route("/settings/", web::get().to(settings::get)) diff --git a/src/search.rs b/src/search.rs index bf0ae51..583b832 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, param, prefs, Post, Preferences}; +use crate::utils::{error, fetch_posts, param, prefs, Post, Preferences, request, val}; use actix_web::{HttpRequest, HttpResponse}; use askama::Template; @@ -13,10 +13,19 @@ struct SearchParams { restrict_sr: String, } +// STRUCTS +struct Subreddit { + name: String, + url: String, + description: String, + subscribers: i64, +} + #[derive(Template)] #[template(path = "search.html", escape = "none")] struct SearchTemplate { posts: Vec, + subreddits: Vec, sub: String, params: SearchParams, prefs: Preferences, @@ -31,11 +40,42 @@ pub async fn find(req: HttpRequest) -> HttpResponse { param(&path, "sort") }; let sub = req.match_info().get("sub").unwrap_or("").to_string(); + let mut subreddits: Vec = Vec::new(); + + if param(&path, "restrict_sr") == "" { + let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", param(&path, "q")); + let res; + let subreddit_list; + + // Send a request to the url + match request(&subreddit_search_path).await { + // If success, receive JSON in response + Ok(response) => { + res = response; + subreddit_list = res["data"]["children"].as_array(); + } + // If the Reddit API returns an error, exit this function + Err(_msg) => {subreddit_list = None;} + } + + // For each subreddit from subreddit list + if !subreddit_list.is_none() { + for subreddit in subreddit_list.unwrap() { + subreddits.push(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, + }); + } + } + } match fetch_posts(&path, String::new()).await { Ok((posts, after)) => HttpResponse::Ok().content_type("text/html").body( SearchTemplate { posts, + subreddits, sub, params: SearchParams { q: param(&path, "q"), diff --git a/static/style.css b/static/style.css index 1cdaec4..0d0cf00 100644 --- a/static/style.css +++ b/static/style.css @@ -347,6 +347,45 @@ input[type="submit"]:hover { color: var(--accent); } background: var(--foreground); } +#search_subreddits { + border-radius: 5px; + background: var(--post); + box-shadow: var(--shadow); + transition: 0.2s all; + border: 1px solid var(--highlighted); + margin-bottom: 20px; +} + +.search_subreddit { + padding: 16px 20px; + display: block; +} + +a.search_subreddit:hover { + text-decoration: none; + background: var(--foreground); +} + +.search_subreddit:not(:last-child) { + border-bottom: 1px solid var(--highlighted); +} + +.search_subreddit_header { + margin-bottom: 8px; +} + +.search_subreddit_name { + font-weight: bold; + font-size: 16px; +} + +.search_subreddit_description { + text-overflow: ellipsis; + overflow: hidden; + white-space: nowrap; + opacity: 0.5; +} + /* Post */ .thread { diff --git a/templates/search.html b/templates/search.html index fdb5689..4bab330 100644 --- a/templates/search.html +++ b/templates/search.html @@ -19,6 +19,21 @@ {% call utils::options(params.t, ["hour", "day", "week", "month", "year", "all"], "all") %} {% endif %} + + {% if subreddits.len() > 0 %} + + {% endif %} {% for post in posts %} {% if post.flags.nsfw && prefs.hide_nsfw == "on" %}