2021-01-01 12:54:13 +13:00
|
|
|
// CRATES
|
2021-02-10 06:38:52 +13:00
|
|
|
use crate::utils::{cookie, error, fetch_posts, param, prefs, request, template, val, Post, Preferences};
|
2021-01-01 12:54:13 +13:00
|
|
|
use askama::Template;
|
2021-02-10 06:38:52 +13:00
|
|
|
use tide::Request;
|
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,
|
|
|
|
description: String,
|
|
|
|
subscribers: i64,
|
|
|
|
}
|
|
|
|
|
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-01-01 12:54:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
2021-02-10 06:38:52 +13:00
|
|
|
pub async fn find(req: Request<()>) -> tide::Result {
|
2021-01-31 18:43:46 +13:00
|
|
|
let nsfw_results = if cookie(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
|
2021-02-10 06:38:52 +13:00
|
|
|
let path = format!("{}.json?{}{}", req.url().path(), req.url().query().unwrap_or_default(), nsfw_results);
|
|
|
|
let sub = req.param("sub").unwrap_or("").to_string();
|
2021-02-21 10:59:16 +13:00
|
|
|
let query = param(&path, "q");
|
2021-01-15 08:45:04 +13:00
|
|
|
|
2021-01-02 09:33:57 +13:00
|
|
|
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
|
|
|
};
|
2021-01-15 07:22:50 +13:00
|
|
|
|
2021-01-15 08:45:04 +13:00
|
|
|
let subreddits = if param(&path, "restrict_sr").is_empty() {
|
2021-02-21 10:59:16 +13:00
|
|
|
search_subreddits(&query).await
|
2021-01-15 08:45:04 +13:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2021-01-01 12:54:13 +13:00
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
match fetch_posts(&path, String::new()).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,
|
|
|
|
t: param(&path, "t"),
|
|
|
|
before: param(&path, "after"),
|
|
|
|
after,
|
|
|
|
restrict_sr: param(&path, "restrict_sr"),
|
|
|
|
},
|
|
|
|
prefs: prefs(req),
|
|
|
|
}),
|
2021-01-14 16:53:52 +13:00
|
|
|
Err(msg) => error(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-01-23 22:48:33 +13:00
|
|
|
match request(subreddit_search_path).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()
|
|
|
|
.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(),
|
|
|
|
}
|
|
|
|
}
|