mirror of https://github.com/spikecodes/libreddit
Store subreddit subscriptions and filter in lower-case
This allows us to prevent an API call in subscriptions_filters(). This in turn fixes unsubscribing from banned subreddits. As a side effect, in the settings page subreddit names are also in lower-case, instead of being in their correct casing.
This commit is contained in:
parent
f06203ae61
commit
2009da8881
|
@ -210,37 +210,8 @@ pub async fn subscriptions_filters(req: Request<Body>) -> Result<Response<Body>,
|
|||
let mut sub_list = preferences.subscriptions;
|
||||
let mut filters = preferences.filters;
|
||||
|
||||
// Retrieve list of posts for these subreddits to extract display names
|
||||
let posts = json(format!("/r/{}/hot.json?raw_json=1", sub), true).await?;
|
||||
let display_lookup: Vec<(String, &str)> = posts["data"]["children"]
|
||||
.as_array()
|
||||
.map(|list| {
|
||||
list
|
||||
.iter()
|
||||
.map(|post| {
|
||||
let display_name = post["data"]["subreddit"].as_str().unwrap_or_default();
|
||||
(display_name.to_lowercase(), display_name)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
// Find each subreddit name (separated by '+') in sub parameter
|
||||
for part in sub.split('+').filter(|x| x != &"") {
|
||||
// Retrieve display name for the subreddit
|
||||
let display;
|
||||
let part = if part.starts_with("u_") {
|
||||
part
|
||||
} else if let Some(&(_, display)) = display_lookup.iter().find(|x| x.0 == part.to_lowercase()) {
|
||||
// This is already known, doesn't require separate request
|
||||
display
|
||||
} else {
|
||||
// This subreddit display name isn't known, retrieve it
|
||||
let path: String = format!("/r/{}/about.json?raw_json=1", part);
|
||||
display = json(path, true).await?;
|
||||
display["data"]["display_name"].as_str().ok_or_else(|| "Failed to query subreddit name".to_string())?
|
||||
};
|
||||
|
||||
for part in sub.split('+').filter(|x| x != &"").map(str::to_lowercase) {
|
||||
// Modify sub list based on action
|
||||
if action.contains(&"subscribe".to_string()) && !sub_list.contains(&part.to_owned()) {
|
||||
// Add each sub name to the subscribed list
|
||||
|
|
|
@ -508,7 +508,9 @@ pub struct Preferences {
|
|||
pub disable_visit_reddit_confirmation: String,
|
||||
pub comment_sort: String,
|
||||
pub post_sort: String,
|
||||
/// List of subscribed subreddits in *lower-case*.
|
||||
pub subscriptions: Vec<String>,
|
||||
/// List of filtered subreddits in *lower-case*.
|
||||
pub filters: Vec<String>,
|
||||
pub hide_awards: String,
|
||||
}
|
||||
|
@ -542,8 +544,8 @@ impl Preferences {
|
|||
disable_visit_reddit_confirmation: setting(req, "disable_visit_reddit_confirmation"),
|
||||
comment_sort: setting(req, "comment_sort"),
|
||||
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(),
|
||||
subscriptions: setting(req, "subscriptions").split('+').filter(|s| !s.is_empty()).map(str::to_lowercase).collect(),
|
||||
filters: setting(req, "filters").split('+').filter(|s| !s.is_empty()).map(str::to_lowercase).collect(),
|
||||
hide_awards: setting(req, "hide_awards"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,8 +107,9 @@
|
|||
<div title="{{ sub.active.1 }}">{{ sub.active.0 }}</div>
|
||||
</div>
|
||||
<div id="sub_actions">
|
||||
{% let lowercase_name = sub.name.to_lowercase() %}
|
||||
<div id="sub_subscription">
|
||||
{% if prefs.subscriptions.contains(sub.name) %}
|
||||
{% if prefs.subscriptions.contains(lowercase_name) %}
|
||||
<form action="/r/{{ sub.name }}/unsubscribe?redirect={{ redirect_url }}" method="POST">
|
||||
<button class="unsubscribe">Unsubscribe</button>
|
||||
</form>
|
||||
|
@ -119,7 +120,7 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
<div id="sub_filter">
|
||||
{% if prefs.filters.contains(sub.name) %}
|
||||
{% if prefs.filters.contains(lowercase_name) %}
|
||||
<form action="/r/{{ sub.name }}/unfilter?redirect={{ redirect_url }}" method="POST">
|
||||
<button class="unfilter">Unfilter</button>
|
||||
</form>
|
||||
|
|
|
@ -100,8 +100,9 @@
|
|||
</div>
|
||||
<div id="user_actions">
|
||||
{% let name = ["u_", user.name.as_str()].join("") %}
|
||||
{% let lowercase_name = name.to_lowercase() %}
|
||||
<div id="user_subscription">
|
||||
{% if prefs.subscriptions.contains(name) %}
|
||||
{% if prefs.subscriptions.contains(lowercase_name) %}
|
||||
<form action="/r/{{ name }}/unsubscribe?redirect={{ redirect_url }}" method="POST">
|
||||
<button class="unsubscribe">Unfollow</button>
|
||||
</form>
|
||||
|
@ -112,7 +113,7 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
<div id="user_filter">
|
||||
{% if prefs.filters.contains(name) %}
|
||||
{% if prefs.filters.contains(lowercase_name) %}
|
||||
<form action="/r/{{ name }}/unfilter?redirect={{ redirect_url }}" method="POST">
|
||||
<button class="unfilter">Unfilter</button>
|
||||
</form>
|
||||
|
|
Loading…
Reference in New Issue