Filter by flair. Closes #124

This commit is contained in:
spikecodes 2021-02-20 13:59:16 -08:00
parent e1f7b6d0c0
commit 9cfab348eb
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
7 changed files with 26 additions and 15 deletions

View file

@ -90,6 +90,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
post["data"]["author_flair_richtext"].as_array(),
post["data"]["author_flair_text"].as_str(),
),
text: val(post, "link_flair_text"),
background_color: val(post, "author_flair_background_color"),
foreground_color: val(post, "author_flair_text_color"),
},
@ -112,6 +113,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
post["data"]["link_flair_richtext"].as_array(),
post["data"]["link_flair_text"].as_str(),
),
text: val(post, "link_flair_text"),
background_color: val(post, "link_flair_background_color"),
foreground_color: if val(post, "link_flair_text_color") == "dark" {
"black".to_string()
@ -186,6 +188,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
data["author_flair_richtext"].as_array(),
data["author_flair_text"].as_str(),
),
text: val(&comment, "link_flair_text"),
background_color: val(&comment, "author_flair_background_color"),
foreground_color: val(&comment, "author_flair_text_color"),
},

View file

@ -36,6 +36,7 @@ pub async fn find(req: Request<()>) -> tide::Result {
let nsfw_results = if cookie(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
let path = format!("{}.json?{}{}", req.url().path(), req.url().query().unwrap_or_default(), nsfw_results);
let sub = req.param("sub").unwrap_or("").to_string();
let query = param(&path, "q");
let sort = if param(&path, "sort").is_empty() {
"relevance".to_string()
@ -44,7 +45,7 @@ pub async fn find(req: Request<()>) -> tide::Result {
};
let subreddits = if param(&path, "restrict_sr").is_empty() {
search_subreddits(param(&path, "q")).await
search_subreddits(&query).await
} else {
Vec::new()
};
@ -55,7 +56,7 @@ pub async fn find(req: Request<()>) -> tide::Result {
subreddits,
sub,
params: SearchParams {
q: param(&path, "q"),
q: query.replace('"', "&quot;"),
sort,
t: param(&path, "t"),
before: param(&path, "after"),
@ -68,7 +69,7 @@ pub async fn find(req: Request<()>) -> tide::Result {
}
}
async fn search_subreddits(q: String) -> Vec<Subreddit> {
async fn search_subreddits(q: &str) -> Vec<Subreddit> {
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+"));
// Send a request to the url

View file

@ -102,7 +102,7 @@ pub async fn subscriptions(req: Request<()>) -> tide::Result {
// Redirect back to subreddit
// check for redirect parameter if unsubscribing from outside sidebar
let redirect_path = param(format!("/?{}", query).as_str(), "redirect");
let redirect_path = param(&format!("/?{}", query), "redirect");
let path = if !redirect_path.is_empty() {
format!("/{}/", redirect_path)
} else {
@ -166,7 +166,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, String> {
title: val(&res, "title"),
description: val(&res, "public_description"),
info: rewrite_urls(&val(&res, "description_html").replace("\\", "")),
icon: format_url(icon.as_str()),
icon: format_url(&icon),
members: format_num(members),
active: format_num(active),
wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(),

View file

@ -64,7 +64,7 @@ async fn user(name: &str) -> Result<User, String> {
Ok(User {
name: name.to_string(),
title: about("title"),
icon: format_url(about("icon_img").as_str()),
icon: format_url(&about("icon_img")),
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
banner: about("banner_img"),

View file

@ -16,6 +16,7 @@ use time::{Duration, OffsetDateTime};
// Post flair with content, background color and foreground color
pub struct Flair {
pub flair_parts: Vec<FlairPart>,
pub text: String,
pub background_color: String,
pub foreground_color: String,
}
@ -428,6 +429,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
data["author_flair_richtext"].as_array(),
data["author_flair_text"].as_str(),
),
text: val(post, "link_flair_text"),
background_color: val(post, "author_flair_background_color"),
foreground_color: val(post, "author_flair_text_color"),
},
@ -454,6 +456,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
data["link_flair_richtext"].as_array(),
data["link_flair_text"].as_str(),
),
text: val(post, "link_flair_text"),
background_color: val(post, "link_flair_background_color"),
foreground_color: if val(post, "link_flair_text_color") == "dark" {
"black".to_string()