Add subreddits to search results

This commit is contained in:
robrobinbin 2021-01-14 19:22:50 +01:00
parent 33c8bdffb9
commit a85a4278f6
4 changed files with 102 additions and 1 deletions

View File

@ -27,6 +27,12 @@ async fn favicon() -> HttpResponse {
.body(include_bytes!("../static/favicon.ico").as_ref()) .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] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let mut address = "0.0.0.0:8080".to_string(); let mut address = "0.0.0.0:8080".to_string();
@ -66,6 +72,7 @@ async fn main() -> std::io::Result<()> {
// GENERAL SERVICES // GENERAL SERVICES
.route("/style.css/", web::get().to(style)) .route("/style.css/", web::get().to(style))
.route("/favicon.ico/", web::get().to(favicon)) .route("/favicon.ico/", web::get().to(favicon))
.route("/thumbnail.svg/", web::get().to(thumbnail))
.route("/robots.txt/", web::get().to(robots)) .route("/robots.txt/", web::get().to(robots))
// SETTINGS SERVICE // SETTINGS SERVICE
.route("/settings/", web::get().to(settings::get)) .route("/settings/", web::get().to(settings::get))

View File

@ -1,5 +1,5 @@
// CRATES // 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 actix_web::{HttpRequest, HttpResponse};
use askama::Template; use askama::Template;
@ -13,10 +13,19 @@ struct SearchParams {
restrict_sr: String, restrict_sr: String,
} }
// STRUCTS
struct Subreddit {
name: String,
url: String,
description: String,
subscribers: i64,
}
#[derive(Template)] #[derive(Template)]
#[template(path = "search.html", escape = "none")] #[template(path = "search.html", escape = "none")]
struct SearchTemplate { struct SearchTemplate {
posts: Vec<Post>, posts: Vec<Post>,
subreddits: Vec<Subreddit>,
sub: String, sub: String,
params: SearchParams, params: SearchParams,
prefs: Preferences, prefs: Preferences,
@ -31,11 +40,42 @@ pub async fn find(req: HttpRequest) -> HttpResponse {
param(&path, "sort") param(&path, "sort")
}; };
let sub = req.match_info().get("sub").unwrap_or("").to_string(); let sub = req.match_info().get("sub").unwrap_or("").to_string();
let mut subreddits: Vec<Subreddit> = 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 { match fetch_posts(&path, String::new()).await {
Ok((posts, after)) => HttpResponse::Ok().content_type("text/html").body( Ok((posts, after)) => HttpResponse::Ok().content_type("text/html").body(
SearchTemplate { SearchTemplate {
posts, posts,
subreddits,
sub, sub,
params: SearchParams { params: SearchParams {
q: param(&path, "q"), q: param(&path, "q"),

View File

@ -347,6 +347,45 @@ input[type="submit"]:hover { color: var(--accent); }
background: var(--foreground); 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 */ /* Post */
.thread { .thread {

View File

@ -19,6 +19,21 @@
{% call utils::options(params.t, ["hour", "day", "week", "month", "year", "all"], "all") %} {% call utils::options(params.t, ["hour", "day", "week", "month", "year", "all"], "all") %}
</select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;"> </select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;">
</form> </form>
{% if subreddits.len() > 0 %}
<div id="search_subreddits">
{% for subreddit in subreddits %}
<a href="{{ subreddit.url }}" class="search_subreddit">
<p class="search_subreddit_header">
<span class="search_subreddit_name">{{ subreddit.name }}</span>
<span class="dot">&bull;</span>
<span class="search_subreddit_members">{{ subreddit.subscribers }} Members</span>
</p>
<p class="search_subreddit_description">{{ subreddit.description }}</p>
</a>
{% endfor %}
</div>
{% endif %}
{% for post in posts %} {% for post in posts %}
{% if post.flags.nsfw && prefs.hide_nsfw == "on" %} {% if post.flags.nsfw && prefs.hide_nsfw == "on" %}