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 },
post,
duplicates,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
num_posts_filtered,
all_posts_filtered,

View file

@ -55,7 +55,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
Ok(response) => {
// Parse the JSON into Post and Comment structs
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();
// 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,
post,
sort,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
single_thread,
url,
})
@ -81,7 +81,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
}
// 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
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
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 {
Vec::new()
};
@ -169,6 +169,7 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
awards,
collapsed,
is_filtered,
prefs: Preferences::new(req),
}
})
.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(),
typed,
},
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
is_filtered: true,
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(),
typed,
},
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
is_filtered: false,
all_posts_filtered,

View file

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

View file

@ -109,7 +109,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
posts: Vec::new(),
sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
redirect_url,
is_filtered: true,
@ -128,7 +128,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
posts,
sort: (sort, param(&path, "t").unwrap_or_default()),
ends: (param(&path, "after").unwrap_or_default(), after),
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
redirect_url,
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(),
url: req.uri().to_string(),
sub,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
};
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 preferences = Preferences::new(req);
let preferences = Preferences::new(&req);
let mut sub_list = preferences.subscriptions;
let mut filters = preferences.filters;
@ -313,7 +313,7 @@ pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, String> {
sub,
wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("<h3>Wiki not found</h3>")),
page,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
}),
Err(msg) => {
@ -351,7 +351,7 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
// ),
sub,
page: "Sidebar".to_string(),
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
}),
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()),
ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
listing,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
redirect_url,
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()),
ends: (param(&path, "after").unwrap_or_default(), after),
listing,
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
redirect_url,
is_filtered: false,

View file

@ -357,6 +357,7 @@ pub struct Comment {
pub awards: Awards,
pub collapsed: bool,
pub is_filtered: bool,
pub prefs: Preferences,
}
#[derive(Default, Clone)]
@ -472,6 +473,7 @@ pub struct Preferences {
pub post_sort: String,
pub subscriptions: Vec<String>,
pub filters: Vec<String>,
pub hide_awards: String,
}
#[derive(RustEmbed)]
@ -481,7 +483,7 @@ pub struct ThemeAssets;
impl Preferences {
// 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.
// Always make the default "system" theme available.
let mut themes = vec!["system".to_string()];
@ -504,6 +506,7 @@ impl Preferences {
post_sort: setting(&req, "post_sort"),
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(),
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 body = ErrorTemplate {
msg: msg.to_string(),
prefs: Preferences::new(req),
prefs: Preferences::new(&req),
url,
}
.render()

View file

@ -20,7 +20,7 @@
{% endif %}
<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 !awards.is_empty() %}
{% if !awards.is_empty() && prefs.hide_awards != "on" %}
<span class="dot">&bull;</span>
{% for award in awards.clone() %}
<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>
<span class="dot">&bull;</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() %}
<span class="award" title="{{ award.name }}">
<img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/>
@ -104,4 +104,4 @@
</footer>
{% endif %}
</div>
{% endblock %}
{% endblock %}

View file

@ -83,6 +83,11 @@
<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 %}>
</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>
<input id="save" type="submit" value="Save">
</div>

View file

@ -73,7 +73,7 @@
{% endif %}
<span class="dot">&bull;</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="awards">
{% 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>
<span class="dot">&bull;</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() %}
<span class="award" title="{{ award.name }}">
<img alt="{{ award.name }}" src="{{ award.icon_url }}" width="16" height="16"/>