Add hide_awards config

This commit is contained in:
Matthew Esposito 2023-01-01 21:39:38 -05:00
parent 6d8aaba8bb
commit 6a785baa2c
11 changed files with 34 additions and 24 deletions

View file

@ -193,7 +193,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
params: DuplicatesParams { before, after, sort }, params: DuplicatesParams { before, after, sort },
post, post,
duplicates, duplicates,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
num_posts_filtered, num_posts_filtered,
all_posts_filtered, all_posts_filtered,

View file

@ -55,7 +55,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
Ok(response) => { Ok(response) => {
// Parse the JSON into Post and Comment structs // Parse the JSON into Post and Comment structs
let post = parse_post(&response[0]["data"]["children"][0]).await; let post = parse_post(&response[0]["data"]["children"][0]).await;
let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req)); let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req), &req);
let url = req.uri().to_string(); let url = req.uri().to_string();
// Use the Post and Comment structs to generate a website to show users // Use the Post and Comment structs to generate a website to show users
@ -63,7 +63,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
comments, comments,
post, post,
sort, sort,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
single_thread, single_thread,
url, url,
}) })
@ -81,7 +81,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
} }
// COMMENTS // COMMENTS
fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str, filters: &HashSet<String>) -> Vec<Comment> { fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str, filters: &HashSet<String>, req: &Request<Body>) -> Vec<Comment> {
// Parse the comment JSON into a Vector of Comments // Parse the comment JSON into a Vector of Comments
let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned); let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned);
@ -101,7 +101,7 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
// If this comment contains replies, handle those too // If this comment contains replies, handle those too
let replies: Vec<Comment> = if data["replies"].is_object() { let replies: Vec<Comment> = if data["replies"].is_object() {
parse_comments(&data["replies"], post_link, post_author, highlighted_comment, filters) parse_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, req)
} else { } else {
Vec::new() Vec::new()
}; };
@ -169,6 +169,7 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
awards, awards,
collapsed, collapsed,
is_filtered, is_filtered,
prefs: Preferences::new(req),
} }
}) })
.collect() .collect()

View file

@ -105,7 +105,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
restrict_sr: param(&path, "restrict_sr").unwrap_or_default(), restrict_sr: param(&path, "restrict_sr").unwrap_or_default(),
typed, typed,
}, },
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
@ -131,7 +131,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
restrict_sr: param(&path, "restrict_sr").unwrap_or_default(), restrict_sr: param(&path, "restrict_sr").unwrap_or_default(),
typed, typed,
}, },
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,

View file

@ -19,7 +19,7 @@ struct SettingsTemplate {
// CONSTANTS // CONSTANTS
const PREFS: [&str; 11] = [ const PREFS: [&str; 12] = [
"theme", "theme",
"front_page", "front_page",
"layout", "layout",
@ -31,6 +31,7 @@ const PREFS: [&str; 11] = [
"use_hls", "use_hls",
"hide_hls_notification", "hide_hls_notification",
"autoplay_videos", "autoplay_videos",
"hide_awards",
]; ];
// FUNCTIONS // FUNCTIONS
@ -39,7 +40,7 @@ const PREFS: [&str; 11] = [
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> { pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
let url = req.uri().to_string(); let url = req.uri().to_string();
template(SettingsTemplate { template(SettingsTemplate {
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
}) })
} }

View file

@ -109,7 +109,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
posts: Vec::new(), posts: Vec::new(),
sort: (sort, param(&path, "t").unwrap_or_default()), sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), "".to_string()), ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
redirect_url, redirect_url,
is_filtered: true, is_filtered: true,
@ -128,7 +128,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
posts, posts,
sort: (sort, param(&path, "t").unwrap_or_default()), sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), after), ends: (param(&path, "after").unwrap_or_default(), after),
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
redirect_url, redirect_url,
is_filtered: false, is_filtered: false,
@ -153,7 +153,7 @@ pub fn quarantine(req: Request<Body>, sub: String) -> Result<Response<Body>, Str
msg: "Please click the button below to continue to this subreddit.".to_string(), msg: "Please click the button below to continue to this subreddit.".to_string(),
url: req.uri().to_string(), url: req.uri().to_string(),
sub, sub,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
}; };
Ok( Ok(
@ -200,7 +200,7 @@ pub async fn subscriptions_filters(req: Request<Body>) -> Result<Response<Body>,
let query = req.uri().query().unwrap_or_default().to_string(); let query = req.uri().query().unwrap_or_default().to_string();
let preferences = Preferences::new(req); let preferences = Preferences::new(&req);
let mut sub_list = preferences.subscriptions; let mut sub_list = preferences.subscriptions;
let mut filters = preferences.filters; let mut filters = preferences.filters;
@ -313,7 +313,7 @@ pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, String> {
sub, sub,
wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("<h3>Wiki not found</h3>")), wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("<h3>Wiki not found</h3>")),
page, page,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
}), }),
Err(msg) => { Err(msg) => {
@ -351,7 +351,7 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
// ), // ),
sub, sub,
page: "Sidebar".to_string(), page: "Sidebar".to_string(),
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
}), }),
Err(msg) => { Err(msg) => {

View file

@ -56,7 +56,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
sort: (sort, param(&path, "t").unwrap_or_default()), sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), "".to_string()), ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
listing, listing,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
redirect_url, redirect_url,
is_filtered: true, is_filtered: true,
@ -77,7 +77,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
sort: (sort, param(&path, "t").unwrap_or_default()), sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), after), ends: (param(&path, "after").unwrap_or_default(), after),
listing, listing,
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
redirect_url, redirect_url,
is_filtered: false, is_filtered: false,

View file

@ -357,6 +357,7 @@ pub struct Comment {
pub awards: Awards, pub awards: Awards,
pub collapsed: bool, pub collapsed: bool,
pub is_filtered: bool, pub is_filtered: bool,
pub prefs: Preferences,
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]
@ -472,6 +473,7 @@ pub struct Preferences {
pub post_sort: String, pub post_sort: String,
pub subscriptions: Vec<String>, pub subscriptions: Vec<String>,
pub filters: Vec<String>, pub filters: Vec<String>,
pub hide_awards: String,
} }
#[derive(RustEmbed)] #[derive(RustEmbed)]
@ -481,7 +483,7 @@ pub struct ThemeAssets;
impl Preferences { impl Preferences {
// Build preferences from cookies // Build preferences from cookies
pub fn new(req: Request<Body>) -> Self { pub fn new(req: &Request<Body>) -> Self {
// Read available theme names from embedded css files. // Read available theme names from embedded css files.
// Always make the default "system" theme available. // Always make the default "system" theme available.
let mut themes = vec!["system".to_string()]; let mut themes = vec!["system".to_string()];
@ -504,6 +506,7 @@ impl Preferences {
post_sort: setting(&req, "post_sort"), post_sort: setting(&req, "post_sort"),
subscriptions: setting(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(), subscriptions: setting(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
filters: setting(&req, "filters").split('+').map(String::from).filter(|s| !s.is_empty()).collect(), filters: setting(&req, "filters").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
hide_awards: setting(&req, "hide_awards"),
} }
} }
} }
@ -820,7 +823,7 @@ pub async fn error(req: Request<Body>, msg: impl ToString) -> Result<Response<Bo
let url = req.uri().to_string(); let url = req.uri().to_string();
let body = ErrorTemplate { let body = ErrorTemplate {
msg: msg.to_string(), msg: msg.to_string(),
prefs: Preferences::new(req), prefs: Preferences::new(&req),
url, url,
} }
.render() .render()

View file

@ -20,7 +20,7 @@
{% endif %} {% endif %}
<a href="{{ post_link }}{{ id }}/?context=3" class="created" title="{{ created }}">{{ rel_time }}</a> <a href="{{ post_link }}{{ id }}/?context=3" class="created" title="{{ created }}">{{ rel_time }}</a>
{% if edited.0 != "".to_string() %}<span class="edited" title="{{ edited.1 }}">edited {{ edited.0 }}</span>{% endif %} {% if edited.0 != "".to_string() %}<span class="edited" title="{{ edited.1 }}">edited {{ edited.0 }}</span>{% endif %}
{% if !awards.is_empty() %} {% if !awards.is_empty() && prefs.hide_awards != "on" %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
{% for award in awards.clone() %} {% for award in awards.clone() %}
<span class="award" title="{{ award.name }}"> <span class="award" title="{{ award.name }}">

View file

@ -65,7 +65,7 @@
<a class="post_author {{ post.author.distinguished }}" href="/u/{{ post.author.name }}">u/{{ post.author.name }}</a> <a class="post_author {{ post.author.distinguished }}" href="/u/{{ post.author.name }}">u/{{ post.author.name }}</a>
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="created" title="{{ post.created }}">{{ post.rel_time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
{% if !post.awards.is_empty() %} {% if !post.awards.is_empty() && prefs.hide_awards != "on" %}
{% for award in post.awards.clone() %} {% for award in post.awards.clone() %}
<span class="award" title="{{ award.name }}"> <span class="award" title="{{ award.name }}">
<img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/> <img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/>
@ -104,4 +104,4 @@
</footer> </footer>
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -83,6 +83,11 @@
<input type="hidden" value="off" name="hide_hls_notification"> <input type="hidden" value="off" name="hide_hls_notification">
<input type="checkbox" name="hide_hls_notification" id="hide_hls_notification" {% if prefs.hide_hls_notification == "on" %}checked{% endif %}> <input type="checkbox" name="hide_hls_notification" id="hide_hls_notification" {% if prefs.hide_hls_notification == "on" %}checked{% endif %}>
</div> </div>
<div class="prefs-group">
<label for="hide_awards">Hide awards</label>
<input type="hidden" value="off" name="hide_awards">
<input type="checkbox" name="hide_awards" id="hide_awards" {% if prefs.hide_awards == "on" %}checked{% endif %}>
</div>
</fieldset> </fieldset>
<input id="save" type="submit" value="Save"> <input id="save" type="submit" value="Save">
</div> </div>

View file

@ -73,7 +73,7 @@
{% endif %} {% endif %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="created" title="{{ post.created }}">{{ post.rel_time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
{% if !post.awards.is_empty() %} {% if !post.awards.is_empty() && prefs.hide_awards != "on" %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="awards"> <span class="awards">
{% for award in post.awards.clone() %} {% for award in post.awards.clone() %}
@ -178,7 +178,7 @@
<a class="post_author {{ post.author.distinguished }}" href="/u/{{ post.author.name }}">u/{{ post.author.name }}</a> <a class="post_author {{ post.author.distinguished }}" href="/u/{{ post.author.name }}">u/{{ post.author.name }}</a>
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="created" title="{{ post.created }}">{{ post.rel_time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
{% if !post.awards.is_empty() %} {% if !post.awards.is_empty() && prefs.hide_awards != "on" %}
{% for award in post.awards.clone() %} {% for award in post.awards.clone() %}
<span class="award" title="{{ award.name }}"> <span class="award" title="{{ award.name }}">
<img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/> <img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/>