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()

View File

@ -48,7 +48,9 @@
<p class="post_title">
<a href="{{ post.permalink }}">{{ post.title }}</a>
{% if post.flair.flair_parts.len() > 0 %}
<small class="post_flair" style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};">{% call utils::render_flair(post.flair.flair_parts) %}</small>
<a href="/r/{{ post.community }}/search/?q=flair_name%3A%22{{ post.flair.text }}%22&restrict_sr=on" class="post_flair" style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};">
{% call utils::render_flair(post.flair.flair_parts) %}
</small>
{% endif %}
{% if post.flags.nsfw %} <small class="nsfw">NSFW</small>{% endif %}
</p>

View File

@ -33,8 +33,8 @@
</form>
{%- endmacro %}
{% macro render_flair(flair) -%}
{% for flair_part in flair %}
{% macro render_flair(flair_parts) -%}
{% for flair_part in flair_parts %}
{% if flair_part.flair_part_type == "emoji" %}<span class="emoji" style="background-image:url('{{ flair_part.value }}');"></span>
{% else if flair_part.flair_part_type == "text" && !flair_part.value.is_empty() %}<span>{{ flair_part.value }}</span>{% endif %}
{% endfor %}
@ -69,7 +69,9 @@
</p>
<p class="post_title">
{% if post.flair.flair_parts.len() > 0 %}
<small class="post_flair" style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};">{% call render_flair(post.flair.flair_parts) %}</small>
<a href="/r/{{ post.community }}/search/?q=flair_name%3A%22{{ post.flair.text }}%22&restrict_sr=on" class="post_flair" style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};">
{% call render_flair(post.flair.flair_parts) %}
</a>
{% endif %}
<a href="{{ post.permalink }}">{{ post.title }}</a>{% if post.flags.nsfw %} <small class="nsfw">NSFW</small>{% endif %}
</p>
@ -87,9 +89,9 @@
</svg>
</a>
{% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "gif" %}
<video class="post_media_video short" src="{{ post.media.url }}" width="{{ post.media.width }}px" height="{{ post.media.height }}px" controls loop><a href={{ post.media.url }}>Video</a></video>
<video class="post_media_video short" src="{{ post.media.url }}" width="{{ post.media.width }}px" height="{{ post.media.height }}px" controls loop autoplay><a href={{ post.media.url }}>Video</a></video>
{% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "video" %}
<video class="post_media_video short" src="{{ post.media.url }}" width="{{ post.media.width }}px" height="{{ post.media.height }}px" poster="{{ post.media.poster }}" preload=none controls><a href={{ post.media.url }}>Video</a></video>
<video class="post_media_video short" src="{{ post.media.url }}" width="{{ post.media.width }}px" height="{{ post.media.height }}px" poster="{{ post.media.poster }}" preload="none" controls autoplay><a href={{ post.media.url }}>Video</a></video>
{% else if post.post_type != "self" %}
<a class="post_thumbnail {% if post.thumbnail.url.is_empty() %}no_thumbnail{% endif %}" href="{% if post.post_type == "link" %}{{ post.media.url }}{% else %}{{ post.permalink }}{% endif %}">
{% if post.thumbnail.url.is_empty() %}