2021-01-01 12:54:13 +13:00
|
|
|
// CRATES
|
2021-05-16 08:59:42 +12:00
|
|
|
use crate::utils::{catch_random, error, format_num, format_url, param, setting, template, val, Post, Preferences};
|
2021-05-17 03:53:39 +12:00
|
|
|
use crate::{
|
|
|
|
client::json,
|
|
|
|
subreddit::{can_access_quarantine, quarantine},
|
|
|
|
RequestExt,
|
|
|
|
};
|
2021-01-01 12:54:13 +13:00
|
|
|
use askama::Template;
|
2021-03-18 11:30:33 +13:00
|
|
|
use hyper::{Body, Request, Response};
|
2021-01-01 12:54:13 +13:00
|
|
|
|
|
|
|
// STRUCTS
|
2021-01-03 19:37:54 +13:00
|
|
|
struct SearchParams {
|
|
|
|
q: String,
|
|
|
|
sort: String,
|
|
|
|
t: String,
|
|
|
|
before: String,
|
|
|
|
after: String,
|
|
|
|
restrict_sr: String,
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:22:50 +13:00
|
|
|
// STRUCTS
|
|
|
|
struct Subreddit {
|
|
|
|
name: String,
|
|
|
|
url: String,
|
2021-03-19 17:32:54 +13:00
|
|
|
icon: String,
|
2021-01-15 07:22:50 +13:00
|
|
|
description: String,
|
2021-03-21 11:42:47 +13:00
|
|
|
subscribers: (String, String),
|
2021-01-15 07:22:50 +13:00
|
|
|
}
|
|
|
|
|
2021-01-01 12:54:13 +13:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "search.html", escape = "none")]
|
|
|
|
struct SearchTemplate {
|
|
|
|
posts: Vec<Post>,
|
2021-01-15 07:22:50 +13:00
|
|
|
subreddits: Vec<Subreddit>,
|
2021-01-01 12:54:13 +13:00
|
|
|
sub: String,
|
2021-01-03 19:37:54 +13:00
|
|
|
params: SearchParams,
|
2021-01-09 14:35:04 +13:00
|
|
|
prefs: Preferences,
|
2021-05-10 13:25:52 +12:00
|
|
|
url: String,
|
2021-01-01 12:54:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-05-16 08:59:42 +12:00
|
|
|
let nsfw_results = if setting(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
|
2021-03-18 11:30:33 +13:00
|
|
|
let path = format!("{}.json?{}{}", req.uri().path(), req.uri().query().unwrap_or_default(), nsfw_results);
|
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
2021-05-17 03:53:39 +12:00
|
|
|
let quarantined = can_access_quarantine(&req, &sub);
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if let Ok(random) = catch_random(&sub, "/find").await {
|
|
|
|
return Ok(random);
|
|
|
|
}
|
2021-05-17 03:53:39 +12:00
|
|
|
let query = param(&path, "q").unwrap_or_default();
|
2021-01-15 08:45:04 +13:00
|
|
|
|
2021-05-17 04:11:38 +12:00
|
|
|
let sort = param(&path, "sort").unwrap_or_else(|| "relevance".to_string());
|
2021-01-15 07:22:50 +13:00
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
let subreddits = match param(&path, "restrict_sr") {
|
|
|
|
None => search_subreddits(&query).await,
|
2021-05-17 04:11:38 +12:00
|
|
|
Some(_) => Vec::new(),
|
2021-01-15 08:45:04 +13:00
|
|
|
};
|
2021-01-01 12:54:13 +13:00
|
|
|
|
2021-05-10 13:25:52 +12:00
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
match Post::fetch(&path, String::new(), quarantined).await {
|
2021-02-10 06:38:52 +13:00
|
|
|
Ok((posts, after)) => template(SearchTemplate {
|
|
|
|
posts,
|
|
|
|
subreddits,
|
|
|
|
sub,
|
|
|
|
params: SearchParams {
|
2021-02-21 10:59:16 +13:00
|
|
|
q: query.replace('"', """),
|
2021-02-10 06:38:52 +13:00
|
|
|
sort,
|
2021-05-17 03:53:39 +12:00
|
|
|
t: param(&path, "t").unwrap_or_default(),
|
|
|
|
before: param(&path, "after").unwrap_or_default(),
|
2021-02-10 06:38:52 +13:00
|
|
|
after,
|
2021-05-17 03:53:39 +12:00
|
|
|
restrict_sr: param(&path, "restrict_sr").unwrap_or_default(),
|
2021-02-10 06:38:52 +13:00
|
|
|
},
|
2021-02-25 18:29:23 +13:00
|
|
|
prefs: Preferences::new(req),
|
2021-05-10 13:25:52 +12:00
|
|
|
url,
|
2021-02-10 06:38:52 +13:00
|
|
|
}),
|
2021-05-17 03:53:39 +12:00
|
|
|
Err(msg) => {
|
|
|
|
if msg == "quarantined" {
|
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
|
|
|
quarantine(req, sub)
|
|
|
|
} else {
|
|
|
|
error(req, msg).await
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 12:54:13 +13:00
|
|
|
}
|
|
|
|
}
|
2021-01-15 08:45:04 +13:00
|
|
|
|
2021-02-21 10:59:16 +13:00
|
|
|
async fn search_subreddits(q: &str) -> Vec<Subreddit> {
|
2021-01-15 08:45:04 +13:00
|
|
|
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+"));
|
|
|
|
|
|
|
|
// Send a request to the url
|
2021-05-17 03:53:39 +12:00
|
|
|
match json(subreddit_search_path, false).await {
|
2021-01-15 08:45:04 +13:00
|
|
|
// If success, receive JSON in response
|
|
|
|
Ok(response) => {
|
|
|
|
match response["data"]["children"].as_array() {
|
|
|
|
// For each subreddit from subreddit list
|
|
|
|
Some(list) => list
|
|
|
|
.iter()
|
2021-03-19 17:32:54 +13:00
|
|
|
.map(|subreddit| {
|
|
|
|
// Fetch subreddit icon either from the community_icon or icon_img value
|
|
|
|
let community_icon: &str = subreddit["data"]["community_icon"].as_str().map_or("", |s| s.split('?').collect::<Vec<&str>>()[0]);
|
|
|
|
let icon = if community_icon.is_empty() {
|
|
|
|
val(&subreddit, "icon_img")
|
|
|
|
} else {
|
|
|
|
community_icon.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
Subreddit {
|
|
|
|
name: val(subreddit, "display_name_prefixed"),
|
|
|
|
url: val(subreddit, "url"),
|
|
|
|
icon: format_url(&icon),
|
|
|
|
description: val(subreddit, "public_description"),
|
|
|
|
subscribers: format_num(subreddit["data"]["subscribers"].as_f64().unwrap_or_default() as i64),
|
|
|
|
}
|
2021-01-15 08:45:04 +13:00
|
|
|
})
|
|
|
|
.collect::<Vec<Subreddit>>(),
|
|
|
|
_ => Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the Reddit API returns an error, exit this function
|
|
|
|
_ => Vec::new(),
|
|
|
|
}
|
|
|
|
}
|